0

I need to change a value depending a result. In this case, if my row "status" is "Active" I want to put something like that in a table:

  <td>'.$row['register_status'].'</td>
        <td>'
        if($row['status']=='Active')
        {
        echo '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
        }
 '</td>

What is wrong on code?

if(mysql_num_rows($result) > 0)
{
    $number = 1;
    while($row = mysql_fetch_assoc($result))
    {
            $data .= '<tr>
            <td>'.$row['register_status'].'</td>
            <td>'
            if($row['status']=='Active')
            {
            echo '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
            }
            '</td>
        </tr>';
            $number++;
    }
}
else
{
    // records now found 
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;

In logs, the error is:

PHP Parse error: syntax error, unexpected T_IF in /var/www/html/form/ajax/readRecords.php on line 32, referer: http:/localhostform/

when the line 32 is:

            if($row['status']=='Active')
  • UPDATE *

Finally I update the code as:

    while($row = mysql_fetch_assoc($result))
    {   
        $data .= '<tr>
                        <td>'.$row['register_status'].'</td>
                        <td>';

                        if($row['register_status']=='Active') {
                                $data .= '<span class="glyphicon glyphicon-true" aria-hidden="true" style="color:green"></span>';
                        }
                        else if($row['register_status']=='Inactive')
                        {       
                                $data .= '<span class="glyphicon glyphicon-time-remove" aria-hidden="true" style="color:red"></span>';
                        }
                        else    
                        {       
                                $data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
                        }

                        $data .= '</td>
                        <td>'.$row['first_name'].'</td>
                  </tr>';   
    }
}
else
{
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;

But always execute the last else and only print the result into

$data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';

Why do not put a different icon according to the different cases?

Guif If
  • 535
  • 2
  • 7
  • 18

3 Answers3

0

You're missing a semi colon here:

$data .= '<tr>
  <td>'.$row['register_status'].'</td>
<td>'

Should be:

$data .= '<tr>
  <td>'.$row['register_status'].'</td>
<td>';

Always look to the previous statement whenever something like "syntax error, unexpected" pops up.

Jaime
  • 1,402
  • 7
  • 15
0

This is more complete correction

$number = 1;
while($row = mysql_fetch_assoc($result))
{
        $data .= '<tr>
        <td>'.$row['register_status'].'</td>
        <td>';
        if($row['status']=='Active')
        {
            $data .= '<span class="glyphicon glyphicon-time"
         aria-hidden="true" style="color:orange"></span>';
        }
        $data .= '</td></tr>';
        $number++;
}
halojoy
  • 225
  • 2
  • 7
0

There are a couple errors in your code. I fixed and commented the errors/suggestions. Try this:

NOTE: Use MySQLi instead of MySQL. I think it's sufficient here to say that the i in MySQLi stands for improved. MySQL is also deprecated and will no longer be available in future PHP versions.

// Assuming you already have $data = '<table>';

if(mysql_num_rows($result) > 0)
{
    $number = 1;
    while($row = mysql_fetch_assoc($result))
    {
        $data .= '<tr>
                      <td>'.$row['register_status'].'</td>
                      <td>';       // Close $data .=, do if, open $data .= again
                          if($row['status']=='Active')
                          {
                               $data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
                          }
                     $data .= '</td>
                  </tr>';
        $number++;          // I don't see a use for $number
    }
}
else
{
    // records now found 
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;
Ivan86
  • 5,695
  • 2
  • 14
  • 30