Current location: Hot Scripts Forums » Programming Languages » PHP » Print MySQL resultset to five columns HTML-table


Print MySQL resultset to five columns HTML-table

Reply
  #1 (permalink)  
Old 05-03-04, 03:25 AM
morrowind morrowind is offline
New Member
 
Join Date: May 2004
Location: Letala
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Print MySQL resultset to five columns HTML-table

I think my problem is a simple one, but it is difficult enough for me.So, I would like to print my MySQL resultset to the HTML table like this: 5 columns and two rows. I need to retrieve LIMIT 10 records.

Besides I need to calculate how many times must be printed default image to the cells if there are not 10 records in the table (for instance it must be printed 3 times default image to the HTML table if I have 7 thumbnails in the table). This calculation seems to work fine, but my HTML table output is wrong. The output is this kind now:

|default.jpg | default.jpg | default.jpg |


|1 thumbnail | 2 thumbnail| 3 thumbnail | 4 thumbnail |

| 5 thumbnail | 6 thumbnail | 7 thumbnail |


3 rows with 3 + 4 + 3 items and this is not pretty, isn't.

I have some confusion with my loops...could someone tell what is the correct way to print MySQL resultset to HTML table with five columns?
PHP Code:

 <?php 
 
require_once "SQL_conn.php";
$LIMITED=10//how many records showing
$columns 5//wanting 5 columns and two rows, but this does not work
 
$i 1//$i is needed to calculate how many times 'default.jpg' image must be printed to the table
 
$query "SELECT id,thumb_image FROM tbl_cars ORDER BY id DESC LIMIT $LIMITED";
$result mysql_query($query);
 
//how many records found 
$lkm mysql_num_rows($result);
 
        
$num_rows = @mysql_query("SELECT COUNT(id) FROM tbl_cars ");
         
$num_rows = @mysql_result($num_rows0"COUNT(id)");
 
 
 
//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
 
echo "<br>";
 
echo 
"\n<TABLE BORDER=\"0\" cellspacing=\"1\" cellpadding=\"1\">\n";
echo 
"\n<TR>";
 
//If I have for instance 7 records, so printing 3 times image default.jpg to the table
for ($i 0;$i < ($LIMITED $lkm);$i++){
 
 
 
     echo 
"\n<TD>";    
echo 
"<img src=images/default.jpg width=95 height=79>";    
    echo 
"\n</TD>"
 
while(
$row mysql_fetch_array($result)) { 
 
$data[] = "<a href=show_car.php?id={$row['id']} Target=_self><img src=".$row['thumb_image']." border=0></a>";
}
 
}
 
 
//here we changed the condition to $i < $rows
for($i 0$i $rows$i++) {
 
//New line below 'default.jpg'image
 
    
echo "\n</TR>\n<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 
"\n<TD>" $data[$i + ($j $rows)] . "</TD>\n";
 
 
         }
 
 
        }
    echo 
"\n</TR>\n";

 
echo 
"\n</TABLE>\n";
 
echo 
"\n</center>";
echo 
"\n</body>";
echo 
"\n</html>"
 
?>

Last edited by morrowind; 05-03-04 at 03:28 AM.
Reply With Quote
  #2 (permalink)  
Old 05-03-04, 08:13 AM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hey Again,

Your gonna love me and my tutorials lol. http://codewalkers.com/tutorials.php?show=15 is where i suggest for this adventure. Gives some good advice and a realli good tutorial on makin coloums. Hope you find it useful.

-Peter
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Upload Script Problem!!! seanknighton Perl 0 03-21-04 09:54 PM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 10:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.