Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Databases _ Displaying MYSQL database table into PHP table

Posted by: joyful Mar 30 2011, 11:43 PM

Hey,

I have set up a MYSQL database and added a table with some data. I want to display this in a table in PHP that is 3 wide, like this:

CODE
<table width="100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>item1</td>
    <td>item2</td>
    <td>item3</td>
  </tr>
  <tr>
    <td>item4</td>
    <td>item5</td>
    <td>item6</td>
  </tr>
  <tr>
    <td>item7</td>
    <td>item8</td>
    <td>item9</td>
  </tr>
</table>

and so on...
Now, I have been using this PHP:
CODE
<?php
//connect to the server
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

//connect to the database
mysql_select_db(product_index);

//query the database
$query = mysql_query("SELECT * FROM products WHERE type = 'bracelets'");

//fetch the results / convert results into an array

        WHILE($rows = mysql_fetch_array($query)):
        
            $product_name = $rows['product_name'];
            $id = $rows['id'];
            $description = $rows['description'];
            $price = $rows['price'];
            $image_large = $rows['image_large'];
            $image_thumb = $rows['image_thumb'];
            $page_link = $rows['page_link'];
            $purchase_link = $rows['purchase_link'];
        
        echo "$product_name<br>$description<br>$price<br>$image_large<br>$image_thumb<br>$page_link<br>$purchase_link<br><br><br>";
        
        endwhile;
?>

This works great, but I what to display this on a table that is 3 wide and repeats displaying on to a another line after 3 and so on.
I found (what I want) this forum post but could not figure out how to apply it to mine: http://php.bigresource.com/Track/php-C1yROxlq/ Please note, obviously I do not want the checkbox in the forum, I just the table they use.
Thanks in advance.

--

Posted by: Frederiek Mar 31 2011, 01:47 AM

The following article may be of help: http://devzone.zend.com/node/view/id/641 .

Posted by: joyful Mar 31 2011, 02:06 PM

Hey,

Thanks Frederiek that page was very helpful... I was able to put the MYSQL data into a table in php with this code:

CODE
<?php

// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "product_index";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM products";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_row($result)) {
        echo "<tr>";
        echo "<td>"."<center>".$row[7]."<br>".$row[2]."<br>".$row[0]."<br>".$row[4]."<br>".$row[8]."</center>"."</td>";
        echo "<td>" . $row."</td>";
        echo "<td>".$row."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

It looks like this:
IPB Image

The only issue now is: I want to display this (my data) from left to right up to 3 columns wide and then have it go to a new line.
How can I have it automatically organize it in this way? It seems to my that the guy on http://php.bigresource.com/Track/php-C1yROxlq/ forum managed to do this (I just do not understand it).

Thanks a lot

--

Posted by: Brian Chandler Mar 31 2011, 10:49 PM

I really think you need to go and read some tutorials on elementary programming, and/or elementary html.

It's not clear quite what html output you want, and it's not clear what you don't understand about php either. Well, OK, I'll guess...

You have something like (pseudocode: you have to fill in the details)

CODE

while ( new-item-from-database)
echo '<tr><td>', details-of-item, '</td></tr>';


But you want to start a new row of the table only every three items?? So you need to keep track of the column.

CODE

define('COLS', 3); // number of columns
$col = 0; // number of the last column filled

echo '<tr>'; // start first row

while ( new-item-from-database)
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 0;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td>', details-of-item, '</td>';
}

echo '</tr>'; // end last row


Posted by: joyful Apr 1 2011, 01:19 AM

Hey, Thanks a lot for the help/code Brian.

Though I do know HTML well, I am new to php and am striving to learn more (thru projects and tutorials). I highly appreciate the patience and generosity of all I encounter in the forum. As for the code... I tried it out (thanks by the way, its exactly what I want), the only issue was: it did not display the 3rd item on the 1st line:
IPB Image
This is how I did it (with your great code!):

CODE
<?php

// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "product_index";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM products";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

define('COLS', 3); // number of columns
$col = 0; // number of the last column filled

echo '<table border="5px">';
echo '<tr>'; // start first row

while ($rows = mysql_fetch_array($result))
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 0;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td>', '<center>', '<img src="', $rows[7], '" width="150px"/>', '<br>', $rows[2],'<br>', $rows[0],'<br>', $rows[4],'<br>', $rows[8], '</center>', '</td>';
}

echo '</tr>'; // end last row
echo "</table>";
// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

Is this issue something I did? What is causing this (skipping from the 2nd item on the 1st line to the 1st item on the 2nd line)?

Thanks a lot for your patience and help.

--

Posted by: Brian Chandler Apr 1 2011, 11:17 AM

Probably I made a mistake. So the thing to do is debug the program -- print out diagnostic info, (such as the value of $col ) so you can see what's happening.

Posted by: joyful Apr 1 2011, 12:07 PM

I know this a very lame question... How do you do this (print diagnostics)? Is it like this?:

CODE
define('COLS', 3); // number of columns
$col = 0; // number of the last column filled

echo '<table border="5px">';
echo '<tr>'; // start first row

while ($rows = mysql_fetch_array($result))
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 0;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td>', '<center>', '<img src="', $rows[7], '" width="150px"/>', '<br>', $rows[2],'<br>', $rows[0],'<br>', $rows[4],'<br>', $rows[8], '</center>', '</td>';
  print ($col);
}

echo '</tr>'; // end last row
echo "</table>";

Is that the right place? If so is the right thing to print? When I added this it just said:

120120

Is this referring to the way the items in each row (col)? If this is the right thing, what does this mean the issue is?

Thanks a lot, Sorry about the dumb questions.

--

Posted by: Darin McGrew Apr 1 2011, 12:56 PM

Here's what I see, based on the what the print command produced.

The first time through the loop:
- $col is 0
- $col is incremented to 1
- $col is not 3 so you don't start a new row

The second time through the loop:
- $col is 1
- $col is incremented to 2
- $col is not 3 so you don't start a new row

The third time through the loop:
- $col is 2
- $col is incremented to 3
- $col is 3 so you do start a new row

Do you see the problem?

Posted by: joyful Apr 1 2011, 01:01 PM

QUOTE(Darin McGrew @ Apr 1 2011, 01:56 PM) *

Here's what I see, based on the what the print command produced.

The first time through the loop:
- $col is 0
- $col is incremented to 1
- $col is not 3 so you don't start a new row

The second time through the loop:
- $col is 1
- $col is incremented to 2
- $col is not 3 so you don't start a new row

The third time through the loop:
- $col is 2
- $col is incremented to 3
- $col is 3 so you do start a new row

Do you see the problem?


Yes, Darin thanks this seems right.
The only difference from what you said is: after line one it is fine (I tried going further) so I think it might be like this:

The first time through the loop:
- $col is 0
- $col is 1
- $col is 3

The second time through the loop:
- $col is 1
- $col is 2
- $col is 3

The third time through the loop:
- $col is 1
- $col is 2
- $col is 3

How can we fix this issue?

--

Posted by: Brian Chandler Apr 1 2011, 02:19 PM

(Thanks Darin!)

Yes, this is a very common sort of error -- "off-by-one". Well, the problem really in all of these formatting things is that you have a "telegraph-pole arrangement": you only need to output the separator bit at the internal points, not the beginning and end (or equivalently, the beginning and end bits are just different). Say you count columns from 1, modulo 3 (1-2-3-1-2-3) then you want to start a new row when the current column is 1 modulo 3, but not when this 1 is the first one in the list.

Grr. I sort of wrote an indication of the error myself: $col is the last column filled. So start it at 0 as above. Then on each output operation test if the last column filled is *already* 3. Right? Thus:

CODE


  if ($col == COLS) // last time filled the last column
  { $col = 1; // so now we're filling first column
    echo '</tr><tr>'; // start new row
  } else  $col++;  // only increment otherwise


I think this is right. Of course there is no warranty. I have not tested it, just argued eloquently that it *must* be right. Or as Charlie Brown would put it, "How can we lose if we're so sincere?"

Posted by: Darin McGrew Apr 1 2011, 04:04 PM

As Brian pointed out, this is an off-by-one error, and it's a very common type error. (If it weren't a common type of error, we probably wouldn't have a name for it.) Anyway, let me see if I can explain it more clearly:

The 1st time through the loop:
- $col starts as 0
- $col is incremented to 1
- $col is not 3 so you don't start a new row
- you echo markup for a td element (the 1st td of the 1st tr)

The 2nd time through the loop:
- $col starts as 1
- $col is incremented to 2
- $col is not 3 so you don't start a new row
- you echo markup for a td element (the 2nd td of the 1st tr)

The 3rd time through the loop:
- $col starts as 2
- $col is incremented to 3
- $col is 3 so you set $col to 0 and start a new row
- you echo markup for a td element (the 1st td of the 2nd tr)

The 4th time through the loop:
- $col starts as 0
- $col is incremented to 1
- $col is not 3 so you don't start a new row
- you echo markup for a td element (the 2nd td of the 2nd tr)

The 5th time through the loop:
- $col starts as 1
- $col is incremented to 2
- $col is not 3 so you don't start a new row
- you echo markup for a td element (the 3rd td of the 2nd tr)

The 6th time through the loop:
- $col starts as 2
- $col is incremented to 3
- $col is 3 so you set $col to 0 and start a new row
- you echo markup for a td element (the 1st td of the 3rd tr)

Do you see the problem?

The easiest fix would be to increment $col only after you test to see if it is equal to 3. That would make the 3rd time through the loop look like this:

The 3rd time through the loop:
- $col starts as 2
- $col is not 3 so you don't start a new row
- $col is incremented to 3
- you echo markup for a td element (the 3rd td of the 1st tr)

Does this make sense?

Posted by: joyful Apr 1 2011, 04:53 PM

Hey,

Thanks a lot. I now see the problem and have fixed it.
This is the code that worked:

CODE
define('COLS', 4); // number of columns
$col = 0; // number of the last column filled

echo '<table border="5px">';
echo '<tr>'; // start first row

while ($rows = mysql_fetch_array($result))
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 1;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td>', '<center>', '<img src="', $rows[7], '" width="150px"/>', '<br>', $rows[2],'<br>', $rows[0],'<br>', $rows[3],'<br>', $rows[4],'<br>', $rows[8], '</center>', '</td>';
  print ($col);
}

echo '</tr>'; // end last row
echo "</table>";

I had to change it to "define('COLS', 4);" as well.

Thank you both for help, I really appreciate it.

--

Posted by: Brian Chandler Apr 1 2011, 11:57 PM

QUOTE
I had to change it to "define('COLS', 4);" as well.


That is Not Good! Obviously "COLS" is supposed to mean "the number of columns", so you shouldn't make it be the number of columns plus 1. The whole point of defining a constant is that you can reuse the code/page with a different number of columns, and if you want 5 you will write 5, not 6.

You asked about debug printing: simply use echo, with an easily spotted format, and don't indent to line up with the real program. So e.g. insert:

echo "<br>**** col: $col ***";


Posted by: joyful Apr 2 2011, 10:50 AM

Hey, I do not quite under stand were I should put this:

QUOTE
echo "<br>**** col: $col ***";

I tried this:
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++;
  if ($col == COLS) // have filled the last row
  { $col = 1;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td class="sub_table">', '<center>', '<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>';
}

echo '</tr>'; // end last row
echo "</table>";

With this, the $col echo looked like this

12
12
12

There is just 2 per line when I do this. Were else should I try "debug printing" to find the issue? Also, Is mend to be like this?:

123
123
123

Or this?:

012
012
012

What can I do to fix this?

Thanks a lot.

--

Posted by: Brian Chandler Apr 2 2011, 12:10 PM

You seem to have copied the "wrong" version again. It says

CODE

$col++;
  if ($col == COLS) // have filled the last row


As I pointed out above, this is wrong: if $col means "the last column filled", which it does, then incrementing, *then* testing for equality to the number of columns, is wrong. Isn't it? If the last column done was 2, and I increment to get 3, this is now the number of the *current* column, and now is not the time to start a new row.

About debugging: in html it's much messier than normal programming, because for example if you are inside a table, simple output will not appear in the right place, unless you are careful to put it inside a <td>. One answer to this is to debug your programs by always looking at the source output, but it's partly personal choice.

Incidentally, your echo format is rather odd (not "wrong"). You have things like:

echo '<td class="sub_table">', '<center>', '<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>';

where each html element is a separate string. But there's no reason to do this: just

echo '<td class="sub_table"><center><img src="', $rows[7], ...

for example. If you split it into several distinct echos, you may be able to insert a separate debugging echo in between: thus

echo '<td class="sub_table"><center>';

echo "<br>DEBUG: col = $col ";

echo '<img src="', $rows[7], ...

For example. Hope this helps. Don't worry too much about the *html* syntax when you are producing debugging output; just so long as you can see what's happening.

Posted by: joyful Apr 3 2011, 02:47 PM

Hey,
Tell me if I am completely misunderstanding how to do the debugging thing:

CODE
while ($rows = mysql_fetch_array($result))
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 0;
    echo '</tr><tr>'; // start a new one
  }
  echo '<td class="sub_table"><center>';
  echo "<br>DEBUG: col = $col ";
  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>';
}

I know that I put $col to 0 put I am not sure (at this point) what to put there.
This now what it says after "debug" for each item:

line 1: 12

line 2: 012

line 3: 012

line 4: 012

Also, I don't understand what is wrong here:
CODE
$col++;
  if ($col == COLS) // have filled the last row

This is how it was to begin with.

Thanks.

--

Posted by: Darin McGrew Apr 3 2011, 07:12 PM

The first time through the loop, $col starts as 0. The second time through the loop, $col starts as 1. The third time through the loop, $col starts as 2.

The question is, when do you increment $col, and when do you test to see if $col is 3?

Here is what happens the third time through the loop now:

QUOTE
- $col starts as 2
- $col is incremented to 3
- test $col: $col is 3 so you set $col to 0 and start a new row
- you echo markup for a td element (the 1st td of the 2nd tr)

Here is what should happen the third time through the loop:
QUOTE
- $col starts as 2
- test $col: $col is not 3 so you don't start a new row
- $col is incremented to 3
- you echo markup for a td element (the 3rd td of the 1st tr)

Do you see the difference?

Posted by: joyful Apr 3 2011, 09:16 PM

Yes, I (now) understand, But what can I do to fix this? What do I have to change? You have explained the problem very well (thank you). How can we fix this?

Thanks

--

Posted by: Darin McGrew Apr 3 2011, 10:36 PM

Move the "$col is incremented" step after the "test $col" step.

Posted by: joyful Apr 4 2011, 04:14 PM

Hey, Thanks so much, this worked.
Sorry it took me a little to understand.
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))
{ $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>";


Thanks again.

--

Posted by: Brian Chandler Apr 5 2011, 12:22 AM

QUOTE(joyful @ Apr 5 2011, 06:14 AM) *

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

Posted by: joyful Apr 5 2011, 12:06 PM

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.

--

Posted by: Brian Chandler Apr 5 2011, 12:55 PM

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.

Posted by: joyful Apr 6 2011, 12:15 AM

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. smile.gif

--

Posted by: Brian Chandler Apr 6 2011, 10:58 AM

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>."

Posted by: joyful Apr 6 2011, 12:54 PM

QUOTE(Brian Chandler @ Apr 6 2011, 11:58 AM) *
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.

--

Posted by: Darin McGrew Apr 6 2011, 08:32 PM

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

Posted by: joyful Apr 6 2011, 09:38 PM

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

--

Posted by: Darin McGrew Apr 6 2011, 10:59 PM

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

Posted by: joyful Apr 6 2011, 11:11 PM

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

--

Posted by: Brian Chandler Apr 7 2011, 12:57 AM

QUOTE(joyful @ Apr 7 2011, 01:11 PM) *

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?

Posted by: joyful Apr 7 2011, 01:18 AM

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.

--

Posted by: Brian Chandler Apr 8 2011, 02:24 AM

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.

Posted by: joyful Apr 8 2011, 01:11 PM

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! smile.gif

--

Posted by: Brian Chandler Apr 9 2011, 01:38 AM

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

Posted by: joyful Apr 9 2011, 02:26 PM

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!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)