Jump to content

echo records in table


s4salman

Recommended Posts

Hello

 

i have a table called cpg_albums.It has fields:

 

aid ( album id)

title (album name)

 

There are so many records in this table.What i want is to print the title name with its link, for example :

 

<a href="http://www.domain.com/thumbnails.php?album=row[aid]">Britney spears</a>

i want to display this way in a table of 6 colums.I mean when 1st row display 6 records, a new row automatically create and this row display 6 next records and so on.

Please any one could help.

Link to comment
https://forums.phpfreaks.com/topic/125042-echo-records-in-table/
Share on other sites

You would need a counter to tell you if you have reached 6 columns yet. Something like this:

 

//Perform query
$query = mysql_query("SELECT * FROM cpg_albums") or die(mysql_error());

//Make sure we have at least 1 returned record.
if(mysql_num_rows($query) == 0){
echo "No records to display.";
}else{

//Start showing the table...
echo "<table border='0' width='100%' cellpadding='1' cellspacing='0'>";
echo "<tr>";

//This is the counter, start it at 0
$i = 0;

//Begin the loop
while($r = mysql_fetch_assoc($query)){

	//Check if i == 6, if it is we insert a new row
	if($i == 6){
		echo "</tr><tr>";
		//And we need to reset i back to 0
		$i = 0;
	}

	//Show the current record
	echo "<td><a href='thumbnails.php?album=".$r['aid']."'>".$r['title']."</a></td>";

	//Increment i by 1
	$i++;
}

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

}

 

Probably shouldn't have written it for you but I commented so you can understand, hopefully it does the job. :)

Thanks, it woked for me.

 

You would need a counter to tell you if you have reached 6 columns yet. Something like this:

 

//Perform query
$query = mysql_query("SELECT * FROM cpg_albums") or die(mysql_error());

//Make sure we have at least 1 returned record.
if(mysql_num_rows($query) == 0){
echo "No records to display.";
}else{

//Start showing the table...
echo "<table border='0' width='100%' cellpadding='1' cellspacing='0'>";
echo "<tr>";

//This is the counter, start it at 0
$i = 0;

//Begin the loop
while($r = mysql_fetch_assoc($query)){

	//Check if i == 6, if it is we insert a new row
	if($i == 6){
		echo "</tr><tr>";
		//And we need to reset i back to 0
		$i = 0;
	}

	//Show the current record
	echo "<td><a href='thumbnails.php?album=".$r['aid']."'>".$r['title']."</a></td>";

	//Increment i by 1
	$i++;
}

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

}

 

Probably shouldn't have written it for you but I commented so you can understand, hopefully it does the job. :)

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.