Hello, Can you please correct the syntax error in this code if(mysqli_num_rows($sql) > 0) { while($row = mysqli_fetch_array($sql)) { $result .=' <tr> <td>'.$row["title"].'</td> <td>'.$row["name"].'</td> if($row["type"]==2) { echo "<td>type2</td>";} else if($row["type"]==1) {echo"<td>type1</td>";} else if($row["type"]==3) {echo"<td>type3</td>";} else if($row["type"]==4) {echo"<td>type4</td>";} <td>'.$row["add_date"].'</td> </tr>'; } } else Code (markup):
this should do the trick if(mysqli_num_rows($sql) > 0) { while($row = mysqli_fetch_array($sql)) { $result .= "<tr> <td>{$row['title']}</td> <td>{$row['name']}</td> <td>type{$row['type']}</td> <td>{$row['add_date']}</td> </tr>"; } } else PHP: If you need to add more logic around $row['type'] then use switch.
Thank you for your reply but if i want to change the echo "message" and type anything else like echo"Hello world" instead of "type 1" this solution didn't work for me.
The response that sarahk provided is how you want to build the content. I think the part that the OP is confused about, is how to handle the IF statements for his switches. for instance, you should set the data to a variable, and add upon it based on the if statements, like such if(mysqli_num_rows($sql) > 0) { while($row = mysqli_fetch_array($sql)) { $output = ''; $output .='<tr>'; $output .= '<td>'. $row["title"]. '</td>'; $output .= '<td>'. $row["name"]. '</td>'; if ($row["type"]==2) { $output .= "<td>type2</td>"; } else if ($row["type"]==1) { $output .= "<td>type1</td>"; } else if ($row["type"]==3) { $output .= "<td>type3</td>"; } else if ($row["type"]==4){ $output .= "<td>type4</td>"; } $output .= '<td>'. $row["add_date"]. '</td>'; $output .= '</tr>'; echo $output; } } else PHP: You could also convert the multiple if/else if into a case statement, as that would be a fairly simple switch, and if you want to build the entire page, you can remove the clearing of $ouput at the beginning of the while loop, and move the echo to outside the while, but I believe that this configuration is more to what the OP is looking for.