Jump to content

Problems with adding a link to one position in a 'foreach' loop, please help


beumont2

Recommended Posts

Hello!

 

Iam pretty new to programming and I wanna create a table with users from an array. It works just fine but in one table row I wanna add a link to it so I can click on the username to edit it later. This is my code right now:

 

	$keys = mysql_query("SELECT * FROM users", $connection);
	confirm_query($keys);
	$result = mysql_query("SELECT * FROM users", $connection);
	if (!$result) 
	confirm_query($keys);

	echo "<table id=\"usertable\">";
	echo "<tr id=\"usertablemain\">";
	$a = array_keys(mysql_fetch_assoc($keys));
	foreach($a as $b) { echo "<td>" . $b . "</td>";}
	echo "</tr>";
	while ($rows = mysql_fetch_assoc($result)) 
	{	
		echo "<tr>";
		foreach ($rows as $row)
			{

			echo "<td>".$row."</a>"."</td>";
			}
		echo "</tr>";
	}
	echo "</tr>";
	echo "</table>";

 

I tried to nest a IF statement but it always give in some way true and all positions in the table get a <a href> tag. The array contains a position called 'username' which is in the second row. Please help me out  :confused:

Ive got it :)

 

	$keys = mysql_query("SELECT * FROM users", $connection);
confirm_query($keys);
$result = mysql_query("SELECT * FROM users", $connection);
if (!$result) 
confirm_query($keys);

echo "<table id=\"usertable\">";
echo "<tr id=\"usertablemain\">";
$a = array_keys(mysql_fetch_assoc($keys));
foreach($a as $b) { echo "<td>" . $b . "</td>";}
echo "</tr>";
	while ($rows = mysql_fetch_assoc($result)) {
		echo "<tr>";
			$count = 0;
			while($count <=9){
				foreach ($rows as $row){
				if($count == 2) 
				{
				echo "<td><a href=#>".$row."</a>"."</td>";
				}
				else 
				{
				echo "<td>".$row."</a>"."</td>";
				}
			$count++;
			}
		}
	}

	echo "</tr>";
	echo "</table>";

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.