Jump to content

HTML in While Loop


RAFC_1990

Recommended Posts

Hey im quite new to php but im hoping you can help me sort the following code out please.

 

while ($row = mysql_fetch_array($sql_result)) 
{
$id1 = $row["id"];
$username1 = $row["username"];
$password1 = $row["password"];
$organisation1 = $row["organisation"];
$delete = echo '<a href="delete.php<a href="delete.php?id=<?php echo $row->id; ?>">Delete</a>">Delete</a>';

print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>";

 

Im trying to get a delete button at the end of every row, but i cant seem to get the right syntax for it to create the html button and loop it on every row :(

 

Please could someone help, Thanks.

Link to comment
https://forums.phpfreaks.com/topic/231501-html-in-while-loop/
Share on other sites

You just echo out the variable, no need to add the <?php ?> tags. You only use those if perhaps you've had a closing tag before and your writing static HTML.

 

while ($row = mysql_fetch_array($sql_result)) 
{
$id1 = $row["id"];
$username1 = $row["username"];
$password1 = $row["password"];
$organisation1 = $row["organisation"];
$delete = echo '<a href="delete.php<a href="delete.php?id=' . $row->id . '>Delete</a>">Delete</a>';

print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>";

Link to comment
https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191349
Share on other sites

You just echo out the variable, no need to add the <?php ?> tags. You only use those if perhaps you've had a closing tag before and your writing static HTML.

 

while ($row = mysql_fetch_array($sql_result)) 
{
$id1 = $row["id"];
$username1 = $row["username"];
$password1 = $row["password"];
$organisation1 = $row["organisation"];
$delete = echo '<a href="delete.php<a href="delete.php?id=' . $row->id . '>Delete</a>">Delete</a>';

print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>";

 

 

Note that the above code still has an extra echo that isn't needed. Also, I would imagine that $row->id is an invalid variable. Based on the code, kenrbnsn's solution should work though.

Link to comment
https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191374
Share on other sites

Thanks guys, worked a treat.

 

But now the problem is that the delete.php code doesnt seem to work for me :(

 

if (isset ($_GET['id']) && !empty ($_GET['id'])) {
   mysql_query ('delete from $tbl_name where id='.intval ($_GET['id']).' limit 1');
   header ('Location:adminadd.php');
} else {
   echo ("Error deleting account from database!");
}
exit;

Link to comment
https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191666
Share on other sites

Assuming that you switch to the solution that kenrbnsn mentioned:

 

<?php
...

$delete = "<a href='delete.php?id=$id1'>Delete</a>";

...
?>

 

 

Do the delete links contain the ID? If so, what happens when you try to display $_GET['id'] on delete.php; does an ID get displayed?

Link to comment
https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191690
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.