Current location: Hot Scripts Forums » Programming Languages » PHP » using a loop to display mysql results?


using a loop to display mysql results?

Reply
  #1 (permalink)  
Old 01-29-04, 05:15 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
using a loop to display mysql results?

Ok guys, this has been bugging me all day. What im trying to do is, pull information from a DB table, and display them in an html table that has more then one <td> per <tr>. The catch is, im using a template system, so it's kinda over my head. Basically this is how i want my info to look on the page :


http://www.sk4nk.org/images/loopexample.gif

Am i thinking right, that i have to use a loop? and if anyones willing to write up a simple code, that i can work with to get everything how i want it, i'd be so happy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-29-04, 05:36 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Whenever I need to do this, I usually use 2 for loops (i.e. nested) - the outer keeps track of num_rows and the inner <td>'s per <tr>.

But with the given info it's hard to say if this fits your particular template engine.

Good night.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 07:45 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
any tutorials? im only a php hobbiest
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-29-04, 01:44 PM
Goran's Avatar
Goran Goran is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
I got an error the first time I tried to see the image. But I'll take a guess at an answer.

If I'm just trying to display things and I don't need to know or care how many rows get shown I usually just place things inside of an open loop.


$query = "SELECT 1st_item, 2nd_item, 3rd_item FROM mydatabase";
$result=mysql_query($query) or die("Could not run the database query!");
while ($r = mysql_fetch_array($result)) {
extract ($r);
echo"<tr>\n";
echo"<td>".$1st_item."</td>\n";
echo"<td>".$2nd_item."</td>\n";
echo"<td>".$3rd_item."</td>\n";
echo"</tr>\n";
}

The "\n" acts like a typewriter carrige return starts a new line for the next item to be displayed in the source code. It makes it eaiser to read when debugging.


Hope it helps.

Goran


Added:

I just got in and saw your image. Sorry. What I passed would only work for a row to row match. The post after mine will give you what you want.

G

Last edited by Goran; 01-29-04 at 02:31 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 01-29-04, 02:12 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Here's an example with while-for loop nest:

PHP Code:

echo '<table>';


// Note that $res is mysql result id.
// $i keeps track of the number of <td>'s fetched.
while ($i mysql_num_rows($res)) {
    
    
// $j keeps track of the number of rows.
    // In this example there will be 3 <td>'s per <tr>
    
for ($j 0$j 3$j++) {
        
$row mysql_fetch_array($res);
        
// Fetch output. I used the first field of each row, but this depends on your script.
        
$rows .= '<td>'.$row[0].'</td>';
        
// Increment $i.
        
$i++;
    }
    
    
// Output this row.
    
echo '<tr>'.$rows.'</tr>';

}

echo 
'</table>'
Please read the comments carefully.
Good luck!
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 01-30-04, 06:40 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
ohh thanks blaw! i'll go see if i can work with that
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 01-30-04, 07:04 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
You're welcome. =)

Correction:

$i: Keeps track of the number of <tr>'s (rows)
$j: Keeps track of the number of <td>'s (columns)

I put them the other way around and told you to read it carefully... Excuse me.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 01-30-04, 08:35 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
mmkay, i finally got it to work. ...but now i want to add to it ...i want 12 items to appear even if their isnt 12 rows in the database.

So just say there's 5 items in the databse ...i want them to pull up, and i also want another 7 items saying "submit yours" or whatever ..i just tried a few things then, but all it did was duplicate the items in the DB ..any advice?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 01-30-04, 09:55 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
hmmm ok, i just noticed, it's not limiting the amount of of <td>'s per column.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 01-30-04, 02:43 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there, sorry about the limiting thing. You need to reset the fetched <td>s each time, which I forgot to add. But together with your request, here it is:

PHP Code:

echo '<table>';


// Note that $res is mysql result id.
// $i keeps track of the number of rows (<tr>).
// In this example, you will get 12 items/cells in total = $i(4) * $j(3).
for ($i 0$i 4$i++) {
    
    
// New row starts.
    
echo '<tr>';
    
    
// $j keeps track of the number of cols (<td>).
    // In this example there will be 3 <td>'s per <tr>
    
for ($j 0$j 3$j++) {
        
        
// See if there is still a fetchable row in the result.
        
if ($row mysql_fetch_array($res)) {
            echo 
'<td>'.$row[0].'</td>';
        }
        
// If not, show the dummy link.
        
else {
            echo 
'<td><a href="submit.php">Submit Yours</a</td>';
        }
    
    }
    
    
// This row ends.
    
echo "</tr>\n";

}

echo 
'</table>'
Note that I'm echoing right away instead of fetching it. Also, this time I tested it, so there should be no problem. If you still want to use the output-once-in-the-end approach, try reseting the fetched columns after echoing it.

Hope this helps. Sorry about the hassle.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
results page to display prev/next php-learner PHP 11 01-12-04 10:40 AM
Paginating mysql results. simone PHP 6 12-26-03 03:25 PM
Which way to display results from mysql Peter_web PHP 2 09-13-03 06:39 PM


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