Current location: Hot Scripts Forums » Programming Languages » PHP » display mysql data in columns?


display mysql data in columns?

Reply
  #1 (permalink)  
Old 02-17-05, 05:24 PM
todayscoffee todayscoffee is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
display mysql data in columns?

Okay i'm fairly new to php/mysql so here's what i'm trying to do:
I have data i'm trying to pull from a mysql db and i'd like to display it 4 across and then however many needed down. Like so:

item 1 id...........|..item 2 id...........|..item 3 id...........|..item 4 id...........|
item 1 title........|..item 2 title........|..item 3 title........|..item 4 title........|
item 1 descrip....|..item 2 descrip....|..item 3 descrip....|..item 4 descrip....|
--------------------------------------------------------------------------
item 5 id...........|..item 6 id...........|..no more data.....|..no more data.....|
item 5 title........|..item 6 title........|..so make cell.......|..so make cell......|
item 5 descrip....|..item 6 descrip....|..empty..............|..empty......... ......|

from the little info/examples i've gathered i think it's somethign to do with counting and looping, but i have no idea how the code would look, so if anyone can provide some examples i'd really appreciate it.

Slainte
__________________
One may say "They're losing their marbles." I would ask, "Who gave them the marbles in the first place?"

Last edited by todayscoffee; 02-17-05 at 05:28 PM.
Reply With Quote
  #2 (permalink)  
Old 02-17-05, 05:48 PM
phprogramming phprogramming is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
heres an example that i used in my gallery script to display thumbnails in rows of 5, u just have to change $array[] to suit ur needs:

PHP Code:

$max=5;

                
$query="SELECT cid,category,images FROM m2_category";
                
$result=@mysql_query($query);
                
$num=mysql_num_rows($result);
                
                if(empty(
$num)){
                    
$final="\t<tr><td><center>No Categories to display, click <a href=\"create.php\">here</a> to create one.</center></td></tr>\n";
                }else{
                    while(
$row=mysql_fetch_array($result,MYSQL_NUM)){
                        
$array[]="<a href=\"gallery.php?c=".$row[0]."\">".$row[1]."(".$row[2].")</a>";
                    }
                    
                
mysql_free_result($result);
                
                    
$final="<tr>\n";
                    
                    foreach(
$array as $thumbnail){
                    if(
$counter==$max){
                        
$counter=1;
                        
$final.="\n<tr>\n";
                    }else 
$counter++;
                        
$final.="\t<td width=\"100\" height=\"75\"><center>".$thumbnail."</center></td>\n";
            
                    }
                    
                        if(
$counter){
                            if(
$max-$counter){
                                
$final .= "\t<td width=\"100\" height=\"75\">&nbsp;</td>\n"
                            }
                            
                            
$final.="</tr>";
                            }
                        } 
then somewhere in your script echo out $final, i chose to do it in a table, just play around till u find something that works

if this script doesnt work just add or remove some } at the bottom, i just copy and pasted it out of my script, but besides that there shouldnt be errors.
__________________
Source Code Talk - Programming Discussion, Tutorials, and More!
Reply With Quote
  #3 (permalink)  
Old 02-17-05, 06:10 PM
todayscoffee todayscoffee is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
That is AWESOME! thank you very very much! That is a great code! Grazie.

Slainte, to you and yours.
__________________
One may say "They're losing their marbles." I would ask, "Who gave them the marbles in the first place?"
Reply With Quote
  #4 (permalink)  
Old 02-17-05, 07:30 PM
phprogramming phprogramming is offline
Wannabe Coder
 
Join Date: Nov 2004
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
lol np man, glad i could help, and im glad u could understand it
__________________
Source Code Talk - Programming Discussion, Tutorials, and More!
Reply With Quote
  #5 (permalink)  
Old 03-11-09, 10:05 PM
franck franck is offline
New Member
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Let's put it in a function

Hi, phprogramming ... I've seen your code and tried to put it all into a function so it can be a little better maintainable, but I can't seem to figure out where the error is. I only get 1 result printed.

Here is the code:

PHP Code:

/*

 * $object can be an array or object
 * $text is the format of the text you want to print (in this case mixed with php variables)
 * $columns is the number of columns
*/
function printInColumns($object=null$text=null$columns=4){
    
//opening the tags
    
$final.="\n<table><ul><tr>\n";

    
//building the table
    
for ($i=0$i<count($object); $i++){
        if(
$counter==$columns){
            
$counter=1;
            
$final.="\n</tr><tr>\n";
        }else 
$counter++;
        eval(
"\$text = \"$text\";");
        
$final.="\t<td>
                
$text
                </td>\n"
;
    }
    
    
//closing the tags 
    
if($counter){
        if(
$columns-$counter)
            
$final .= "\t<td>&nbsp;</td>\n"
        
$final.="</tr>";
    }
$final.="\n</ul></table>\n";
    
    
//returning the html code
    
return "$final";
}

/*
 * And here we call the function
*/
echo printInColumns($bookList'<li><input type=\'checkbox\' name=\'book\' value=\'".$object[$i]->idbook."\'/>".$object[$i]->bookname."</li>',4); 
In the object $bookList I have 10 elements. But the script just prints the first element, 10 times! so I think there is a problem with the $i variable but I can't seem to find the solution.

Hope you can help me get this done

Greetings.
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
Grab data from mysql resualt and How to make this eregi work ? websnow PHP 12 01-08-05 12:44 PM
Php Form + mysql + display data sent needed TheRaider Script Requests 3 10-07-04 01:30 AM
problem printing mysql data by "x rows/page" abtimoteo PHP 1 07-30-04 07:55 PM
Print MySQL resultset to five columns HTML-table morrowind PHP 1 05-03-04 08:13 AM


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