s4salman Posted September 20, 2008 Share Posted September 20, 2008 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 More sharing options...
JasonLewis Posted September 20, 2008 Share Posted September 20, 2008 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. Link to comment https://forums.phpfreaks.com/topic/125042-echo-records-in-table/#findComment-646206 Share on other sites More sharing options...
F1Fan Posted September 20, 2008 Share Posted September 20, 2008 If you only wanted a certain number of results per page, you can use the LIMIT and OFFSET functions (at least in PostgreSQL), or pagination. Link to comment https://forums.phpfreaks.com/topic/125042-echo-records-in-table/#findComment-646208 Share on other sites More sharing options...
s4salman Posted September 20, 2008 Author Share Posted September 20, 2008 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. Link to comment https://forums.phpfreaks.com/topic/125042-echo-records-in-table/#findComment-646223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.