Displaying MYSQL database table into PHP table |
Displaying MYSQL database table into PHP table |
Brian Chandler |
Apr 5 2011, 12:22 AM
Post
#21
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Hey, Thanks so much, this worked. Well, you still have it wrong, which is silly, since I showed you what I believe is a correct version above. QUOTE CODE define('COLS', 3); // number of columns $col = 0; // number of the last column filled echo '<table class="inner_table">'; echo '<tr>'; // start first row while ($rows = mysql_fetch_array($result)) { $col++; echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span><br>', $col, '</center></td>'; if ($col == COLS) // have filled the last row { $col = 0; echo '</tr><tr>'; // start a new one } } echo '</tr>'; // end last row echo "</table>"; You are inserting the "linebreak" sequence </tr><tr> at the right place, basically: (1) At the beginning of the loop, $col is the last column filled (almost! ...from 0 to 2, where 0 means 3 was filled and followed by a linebreak) (2) First you increment $col, so it is now the column that is being filled (3) You output the current <td> (4) If the current column is 3, you output the linebreak, and reset the column to "0" *** *** But step (4) is wrong. You do not want to output the linebreak until you know whether there is another item coming! With this program, if there is a multiple of 3 columns, there will be a blank row at the end. So you must output the linebreak -- if required -- *before* outputting the current <td>. CODE // --- at the beginning $col = 0; // last column filled (none so far!) // --- before outputting the current <td> if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 1; // so now we're filling first column } else $col++; // increment otherwise The reason this is fiddly is that although you are putting out one linebreak every three items, the number of linebreaks per item is always less than 3 (1:0, 2:0, 3:0, 4:1, etc) HTH |
joyful |
Apr 5 2011, 12:06 PM
Post
#22
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey, Is this right (I do not think so):
CODE define('COLS', 3); // number of columns $col = 0; // number of the last column filled echo '<table class="index_php_table">'; echo '<tr>'; // start first row while ($rows = mysql_fetch_array($result)) { $col++; echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span>', '</center></td>'; if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 0; // so now we're filling first column } else $col++; // increment otherwise } echo '</tr>'; // end last row echo "</table>"; I tried to apply what you said... Also, you are right, when I checked the source code output (of the last try) there was a extra <td></td> at the end. The way my php looks now, it comes out all wrong (so obviously I did not totally understand). Which part in the above code did I fail to understand? Thanks a lot. -- |
Brian Chandler |
Apr 5 2011, 12:55 PM
Post
#23
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
I can't understand what you can't understand. I said you are outputting the "linebreak" at the right place -- I meant that your version puts linebreaks between the 3n-th and (3n+1)-th <td>s, which is correct -- but you have the bit of code for doing it in the wrong place. You need to output the linebreak -- if required -- *before* outputting the current <td>. (Otherwise, if the last <td> is the 3n-th, there will be a blank row at the end.
I don't understand where a spare <td></td> would come from, but again, you have to investigate these things by adding debugging to identify which line is producing it. |
joyful |
Apr 6 2011, 12:15 AM
Post
#24
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Sorry, I meant to say "a extra <tr></tr>" (not <td></td>).
Sorry, I fell that I continuously fail to understand what you mean. When you say: QUOTE you are outputting the "linebreak" at the right place And then: QUOTE but you have the bit of code for doing it in the wrong place. I understand want you are saying, I just don't know how to apply it. Do you think You could (with code) show me how to do this? I can't understand the order of how the code should go. Thank you so much for your patience. -- |
Brian Chandler |
Apr 6 2011, 10:58 AM
Post
#25
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Sorry, I'm not going to write your program for you.
Here is the part of your program that loops through the items, very slightly edited: CODE while ($rows = mysql_fetch_array($result)) { $col++; echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span>', '</center></td>'; if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 0; // so now we're filling first column } else $col++; // increment otherwise } (1) Question: why do you call the variable $rows? What does it represent? It is quite standard to call one entry from the database a "row" -- does this variable represent a row? It might seem mean to keep going on about names of identifiers etc, but in the end if you use well-chosen ones, your program can be read easily, corrected easily, and updated easily. (2) I have divided the bit of code inside the loop into three lumps (by adding two blank lines). What are these lumps? What does each one do? And why? (3) I am really finding this hard: I say "but you have the bit of code for doing it in the wrong place." You say you understand this, but do not know how to "apply" it? Um, perhaps by putting it in the right place... Here, I told you before: "You need to output the linebreak -- if required -- *before* outputting the current <td>." |
joyful |
Apr 6 2011, 12:54 PM
Post
#26
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Um, perhaps by putting it in the right place... Well, I did not know were the right place was. Also, this "very slightly edited" version does not work right... It gives me 2 lines like this: _____________ | item1 | item2 | _____________ | item3 | item4 | _____________ | item5 | item6 | _____________ etc. What do you think is causing this now? Thanks. -- |
Darin McGrew |
Apr 6 2011, 08:32 PM
Post
#27
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
That looks like a different off-by-one error. Without seeing the current code, my guess is that you're doing something like this:
$cols is 1, increment $cols to 2, $cols is not 3 continue row $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 1 $cols is 1, increment $cols to 2, $cols is not 3 continue row $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 1 $cols is 1, increment $cols to 2, $cols is not 3 continue row $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 1 |
joyful |
Apr 6 2011, 09:38 PM
Post
#28
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Here is the current php:
CODE define('COLS', 3); // number of columns $col = 0; // number of the last column filled echo '<table class="inner_table">'; echo '<tr>'; // start first row while ($rows = mysql_fetch_array($result)) { $col++; echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span>', '</center></td>'; if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 0; // so now we're filling first column } else $col++; // increment otherwise } echo '</tr>'; // end last row echo "</table>"; How can we fix this issue? Is it "off-by-one"? If so how can we resolve it? Thanks -- |
Darin McGrew |
Apr 6 2011, 10:59 PM
Post
#29
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
You're incrementing $col in two places. The result looks like this:
$cols is 0, increment $cols to 1, $cols is not 3 so increment $cols to 2 $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 0 $cols is 0, increment $cols to 1, $cols is not 3 so increment $cols to 2 $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 0 $cols is 0, increment $cols to 1, $cols is not 3 so increment $cols to 2 $cols is 2, increment $cols to 3, $cols is 3 so start new row and set $cols to 0 |
joyful |
Apr 6 2011, 11:11 PM
Post
#30
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey,
If I remove "else $col++;" from the bottom it becomes 3 wide, but it still was the extra <tr></tr> at the bottom (in source code). Were should I move/remove the "incrementing of $col"? Thanks -- |
Brian Chandler |
Apr 7 2011, 12:57 AM
Post
#31
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Hey, If I remove "else $col++;" from the bottom it becomes 3 wide, but it still was the extra <tr></tr> at the bottom (in source code). Were should I move/remove the "incrementing of $col"? Quote me, a few posts up: " You need to output the linebreak -- if required -- *before* outputting the current <td>." I asked you some questions: I "slightly edited" the bit of code to try to help you see what I mean by "three lumps", but here we are again: Lump 1 CODE $col++; Lump 2 CODE echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span>', '</center></td>'; Lump 3 CODE if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 0; // so now we're filling first column } else $col++; // increment otherwise Quote me, from above: (2) I have divided the bit of code inside the loop into three lumps (by adding two blank lines). What are these lumps? What does each one do? And why? |
joyful |
Apr 7 2011, 01:18 AM
Post
#32
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Sorry if I am being repetitive.
Let me try to understand what each lump does: Lump 1: Incrementing $col Lump 2: displays and formats (<br>'s <center> etc) the data. Lump 3 determines whether it is up to 3 $col's if so: echos new <tr></tr> so then $col would = 0. There are 2 parts of this I do not quite understand: 1) in lump 1, what is being incremented? Are we at 0 already? 2) in lump 3,why is it being incremented if we already made a <tr></tr>. What is there to increment? Maybe I don't understand incrementeding. Thanks. -- |
Brian Chandler |
Apr 8 2011, 02:24 AM
Post
#33
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
We seem to be making slight progress now...
QUOTE Maybe I don't understand incrementing. "Increment" just means "add 1". You are outputting <td>s ("lump 2": no problems!), and while doing it you are supposed to be counting which column you are in -- therefore for each <td> you need to add 1 to the column, i.e. increment it. Except that after column 3 (or "COLS", for generality) we need to go back to column 1. Then the big mystery is why there are two lumps -- 1 and 3. I said before that you need to output the "linebreak" sequence only when you know there is at least one <td> to follow, or you get a blank row at the end (which you are getting). Therefore you need to put the mystery lumps 1 and 3 both before lump 2, and sort out the duplicate incrementing. Please try doing that first. |
joyful |
Apr 8 2011, 01:11 PM
Post
#34
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey, Thanks so much Brian (and Darin) for your help (and patience).
I (think) that we have sorted out the issue. here is the code: CODE define('COLS', 3); // number of columns $col = 0; // number of the last column filled echo '<table class="inner_table">'; echo '<tr>'; // start first row while ($rows = mysql_fetch_array($result)) { if ($col == COLS) // last time filled the last column { echo '</tr><tr>'; // start new row -- "linebreak" sequence $col = 0; // so now we're filling first column } else $col++; // increment otherwise echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span>', '</center></td>'; } echo '</tr>'; // end last row echo "</table>"; Tell me if you still see any issues, but there is not a extra "<tr></tr>" at the bottom. Once again thanks! -- |
Brian Chandler |
Apr 9 2011, 01:38 AM
Post
#35
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Have you tested this with seven items being output?
Look at my post #10 above, and spot the difference... If there are 3 things, C programmers instinctively number them 0, 1, 2, but here we have written "$col is the number of the column", so it ought to be 1, 2, 3. Indeed we ask "Does $col equal COLS?" to test whether the last column was number 3 (COLS), so it certainly ought to be 1 2 or 3. So when we update the column number ($col) we are setting it to the *current* column number, in order that next time round the loop it represents the *last* column number (you know: six-year-olds discover with glee that tomorrow's yesterday is actually today...) HTH |
joyful |
Apr 9 2011, 02:26 PM
Post
#36
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Thanks Brian,
Yes when I try 7 items it made a line with 4. So, I changed $col to 1 (like you said): CODE $col = 1; Now it works great! Thanks again! |
Lo-Fi Version | Time is now: 19th April 2024 - 06:27 AM |