Current location: Hot Scripts Forums » Programming Languages » PHP » Help with echo'ing rows of (td's) from database entries


Help with echo'ing rows of (td's) from database entries

Reply
  #1 (permalink)  
Old 04-20-04, 04:01 AM
paulj000 paulj000 is offline
Bull in a china shop
 
Join Date: Jul 2003
Location: California, USA
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Help with echo'ing rows of (td's) from database entries

I want to make rows in HTML of the entries in the database. I need them to format like this based on different amount of entries based of ID's:

(if 9 entries in database):
<tr><td> 1 </td><td> 2 </td><td> 3 </td><td> 4 </td></tr>
<tr><td> 5 </td><td> 6 </td><td> 7 </td><td> 8 </td></tr>
<tr><td> 9 </td><td> </td><td> </td><td> </td></tr>

(if 4 entries in database):
<tr><td> 1 </td><td> 2 </td><td> 3 </td><td> 4 </td></tr>

(if 7 entries in database):
<tr><td> 1 </td><td> 2 </td><td> 3 </td><td> 4 </td></tr>
<tr><td> 5 </td><td> 6 </td><td> 7 </td><td> </td></tr>

Thanks as I have been going nuts trying to get this to work properly!
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 04-20-04, 04:20 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
Hello paulj000,

you would need some code like this:

for($i=0;$i <= mysql_num_rows($query);$i++){
echo "<td>$i<td>";
}

Or well at least I hope that could should work for ya, give it a try and if it don't work out perhaps you could experiment until you get it right?

Peter
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
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 04-20-04, 04:25 AM
paulj000 paulj000 is offline
Bull in a china shop
 
Join Date: Jul 2003
Location: California, USA
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
howdy, I have it going that far my real problem is I can't get it to stop at 4 columns (ID 1 - ID 4) and then start a NEW row with ID 5 - ID 9. etc etc

make sense? I need a new row drawn out for every 4 entries...
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 04-20-04, 04:30 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,

yeah sorry, i noticed that upon posting, oops! anyhow I went back through some of my code I have written and I came accross this that might help ya:

$columns = 4;
$rows = mysql_num_rows($items);
echo "<table width='90%' cellspacing=0 cellpadding=4 align='center'>";
for($i = 0; $i < $rows; $i++) {
$row = mysql_fetch_array($items);
if($i % $columns == 0) {
echo "<tr>";
}
//WHAT YOU WANT TO ECHO HERE eg "<td>1</td>";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $rows) {
echo "</tr>";
}
}
echo "</table>";

sorry if it's a bit all over the place, but it's pretty easy to follow when you realli think about it. Hope it helps

Peter
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
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 04-20-04, 04:57 AM
paulj000 paulj000 is offline
Bull in a china shop
 
Join Date: Jul 2003
Location: California, USA
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Peter!

that actually works!!!
<?php
$items = mysql_query("SELECT * FROM table;");

$columns = 4;

$rows = mysql_num_rows($items);
echo "<table width='90%' cellspacing=0 cellpadding=4 align='center'>";

for($i = 0; $i < $rows; $i++) {
$row = mysql_fetch_array($items, MYSQL_ASSOC);

if($i % $columns == 0) {
echo "\n<tr>";
}

echo "<td>$row[ID]</td>";

if(($i % $columns) == ($columns - 1) || ($i + 1) == $rows) {
echo "</tr>";
}
}
echo "</table>";


?>


There is one part I do not understand: $i % $columns
So I can learn, what does that mean/do??

Thanks again
- paul
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 04-20-04, 07:46 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,

Basically it gets the current number going through and checkes it against the chosen amount eg 0 / 4 is 0 so it prints the start of a new row. Sometimes I don't really get the script either but it does what I wanted it to do lol.

Peter

EDIT: I decided to do my h/w and stop cutting off short and no, its not what i came up with above. What the % actually does is calculate the remandier.

0 % 4 = 0
4 % 4 = 0
8 % 4 = 0

and then the closeing tags look like

3 % 4 = 3
7 % 4 = 3
11 & 4 = 3

and so on, so its just devision but instead of giving the devised number gives you the remainder. Hope that helps.

Peter
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/

Last edited by the_mole001; 04-20-04 at 08:19 AM. Reason: Wrong Information Given Sorry
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 04-20-04, 10:32 AM
dcooldude_1's Avatar
dcooldude_1 dcooldude_1 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Sidney
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Just an added thought to Moles post, put can come in REAL handy in a lot of situations. For example, I'm working on a stupid Black Jack game in J# for school. Well, since computers think mainly in numbers, you almost HAVE to use the % operator.

What I'm saying is you have 52 cards, 1-52, right? Well, you randomly pick a card, and if it's 25, you need to know what suit it's in and what it's value is. So after setting the rules, it's easy with that operator.
__________________
===================================
Dcooldude_1

Site 1: http://ih.sidneyps.com/hs/spokesman/
Site 2: http://www.webhex.net/

Go to http://www.chatzy.com/?132627185966 this is my chat area...
the password is: progtalk
===================================
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 Form to update a MySQL database? Scoobler PHP 9 09-04-08 02:41 AM
can't edit multiple rows at the same time conundrum PHP 2 04-01-04 01:50 AM
Share database over the internet nitinkedia PHP 0 07-11-03 01:22 AM
Share database over the Internet nitinkedia New Members & Introductions 1 07-10-03 03:50 PM


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