-2

I'm new to PHP and HTML, I have a function that is on a different class for where I fetch the database value and make the radio button set to selected if it contains the value.

    public function getstatusradio () {
    $sql = "SELECT ticketstatus FROM ticketpractice WHERE id = 6";
    $result = $this->connect()->query($sql);
    if ($result-> rowCount() > 0) {
        while ($row = $result->fetch()){
               echo "<input type='radio' name='status' <?php if($row['ticketstatus']=='new') {echo 'checked';} ?> value='new '>new";
               echo "<input type='radio' name='status' <?php if($row['ticketstatus']=='processing') {echo 'checked';} ?> value='processing'>processing";
            }
        }
    }

and here is as I declare the code inside my HTML page

<div>
<?php
  $object = new myclass;
  $object->getstatusradio();
?>
</div>

this is the result that I want to achieve where the correct radio button is selected according to the database values "new" and "processing." in this example the database value is "processing.

image"

As of now this the result I have

"Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)"

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ryan Cat
  • 25
  • 4

3 Answers3

0

You wrong here :

echo "<input type='radio' name='status' <?php if($row['ticketstatus']=='new') {echo 'checked';} ?> value='new '>new";
echo "<input type='radio' name='status' <?php if($row['ticketstatus']=='processing') {echo 'checked';} ?> value='processing'>processing";

You don't need do open again <?php for use if, because you are already on php tag

So the code will be:

if($row['ticketstatus']=='new'{
echo "<input type='radio' name='status' value='new ' checked>";
}elseif($row['ticketstatus']=='processing'{
echo "<input type='radio' name='status' value='processing' checked>";
}
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
0

You can change your code to below:

 public function getstatusradio () {
    $sql = "SELECT ticketstatus FROM ticketpractice WHERE id = 6";
    $result = $this->connect()->query($sql);
    if ($result-> rowCount() > 0) {
        while ($row = $result->fetch()){
               $newCheck = $row['ticketstatus']=='new' ? 'checked' : '';
               $processingCheck = $row['ticketstatus']=='processing' ? 'checked' : '';
               echo "<input type='radio' name='status' $newCheck value='new '>new";
               echo "<input type='radio' name='status' $processingCheck value='processing'>processing";
            }
        }
    }

but if you want to use php tag inside "", use htmlspecialchars()

$phpCode = '<? if($condtion){ ?>';
echo htmlspecialchars($phpCode);
Sok Hai
  • 546
  • 4
  • 12
0

You don't need to add <?php ?> tags again inside a php code. You also don't need to do echo again inside one echo.

The example below can be simpler, but I want you to understand what is going on.

<?php
    public function getstatusradio () {
    $sql = "SELECT ticketstatus FROM ticketpractice WHERE id = 6";
    $result = $this->connect()->query($sql);
        if ($result-> rowCount() > 0) {
            $output1="";
            $output2="";
            while ($row = $result->fetch()){
               $output1 .= "<input type='radio' name='status'";
               $output1 .= $row['ticketstatus']=='new'?'checked':"";
               $output1 .= " value='new '>new";
               $output2 .= "<input type='radio' name='status'";
               $output2 .= $row['ticketstatus']=='processing'?'checked':"";
               $output2 .= " value='processing'>processing";
            }
            // do whatever you like to the output. You may return it or `echo` it later or ...
            echo $output1;
            echo $output2;
        }
    }
?>
Nawras
  • 181
  • 1
  • 12