Creating dynamic links in a multi column table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Davey Gale
Forum Newbie
Posts: 2
Joined: Thu Aug 26, 2004 12:43 pm

Creating dynamic links in a multi column table

Post by Davey Gale »

I have a mySQL db which has the fields id, name, street, and description in it. On my website i have a table which dynamically lists all of the "name" entries found in the db, one after the other in Ascending order across multiple columns. This acts as a menu "landing" page.

Here is the code for this:

Code: Select all

<?php

$columns = 3;

// includes
include("config.php");
include("functions.php");

// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// generate and execute query
$query = "SELECT id, name, street, description FROM clubs ORDER BY name ASC";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());

$num_rows = mysql_num_rows($result);

//we are going to set a new variables called $rows
$rows = ceil($num_rows / $columns);

//to do this display, we will need to run another loop
//this loop will populate an array with all our values
while($row = mysql_fetch_array($result)) {
$data[] = $row['name'];
}

echo "<TABLE BORDER="0">\n";

//here we changed the condition to $i < $rows
for($i = 0; $i < $rows; $i++) {

echo "<TR>\n";

//here will run another loop for the amount of columns
for($j = 0; $j < $columns; $j++) {
if(isset($data[$i + ($j * $rows)])) {
echo "<TD>" . $data[$i + ($j * $rows)] . "</TD>\n";
}
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>
What I would like to happen is when the user clicks on one of the names listed, it generates a new page which displays all of the fields e.g. name, description, image.

In a more simplistic code where i've just had the 1 column listing down the page the code for this was:

Code: Select all

?> 
<strong><a href="clubs.php?id=<? echo $row['id']; ?>" class="nav1"><? echo $row['name']; ?></a></strong></font>
<?
I dont know how to incorporate this into the above code. I'm sure it involves editing the code:

Code: Select all

$data[] = $row['name'];
Can anyone show me the way, or give some pointers? Many thanks.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

Code: Select all

<?php
//to do this display, we will need to run another loop
//this loop will populate an array with all our values
while($row = mysql_fetch_array($result)) {
$name[] = $row['name'];
$id[]=$row['id'];
}
//farther down where creating the table

$count=count($name);
for($x=0; $x<$count; $x++)
{
  
echo "<strong><a href="clubs.php?id=".$id[$x]." class="nav1">".$name[$x]."</a></stong>\n";

}
?>
important part is the echo and creating the two arrays. I know it is possible to do it all in one go but this is just to give you the idea of what to do.
Post Reply