0

I've looked through some of the other answers on this, but they don't seem to solve my particular issue (or I am coding them wrong, which is also possible).

I know my SQL code works right, as I get all 19 rows of output, but when I output it to a PHP page, the page shorts me by 5 rows.

If I don't try to show the results in three columns by whatever (and just use one column) everything shows. When I try to do three by 'X', is when it starts cutting off results.

Here is my old, not working PHP code:

$fleet_query_results = @mysql_query($fleet_query, $connection) or die(mysql_error());

while($row = mysql_fetch_array($fleet_query_results)) {
    $display_fleet .= "<tr>";

    for($i = 1; $i < 4 && $row = mysql_fetch_array($fleet_query_results); $i++) {
        $ship_info = $row['ship_info'];
        $mem_info = $row['mem_info'];
        $ship_link = $row['ship_link'];

        $display_fleet .= "
            <td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
        ";
    }

    $display_fleet .= "</tr>";
}

Any ideas? It's cutting off the first, fourth, ninth, thirteenth and seventeenth results.

Example page results:

<table border="1">
  <tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
  <tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
  <tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
</table>

New code that works, but is UGLY:

    $fleet_query_results = @mysql_query($fleet_query, $connection) or die(mysql_error());

while($row = mysql_fetch_array($fleet_query_results)) {
    $display_fleet .= "<tr>";
    $ship_info = $row['ship_info'];
    $mem_info = $row['mem_info'];
    $ship_link = $row['ship_link'];

    $display_fleet .= "
        <td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
    ";

    for($i = 1; $i < 3 && $row = mysql_fetch_array($fleet_query_results); $i++) {
        $ship_info = $row['ship_info'];
        $mem_info = $row['mem_info'];
        $ship_link = $row['ship_link'];

        $display_fleet .= "
            <td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
        ";
    }

    $display_fleet .= "</tr>";
}
4
  • what does $i < 4 mean in your loop? Commented Dec 26, 2014 at 20:04
  • 2
    #1 Stop supressing potential errors with @. #2 Stop using the old deprecated MySQL extension, and switch to MySQLi or PDO #3 Why are you fetching a result in your while loop, and again in your for loop? Commented Dec 26, 2014 at 20:07
  • 1
    you're fetching rows in outerloop, which are not displayed, this is why some rows are missing Commented Dec 26, 2014 at 20:07
  • @MarkBaker - Thanks for the heads up. Didn't realize that it had been depreciated. I am now working to retrofit my code. Commented Dec 26, 2014 at 21:57

1 Answer 1

1

Your while was eating some of the data rows.

$fleet_query_results = @mysql_query($fleet_query, $connection) or die(mysql_error());
$rows=1;
while($rows) {
    $display_fleet .= "<tr>";

    for($i = 1; $i < 4 && $row = mysql_fetch_array($fleet_query_results); $i++) {
        $rows=$row;
        if (!$row) break;
        $ship_info = $row['ship_info'];
        $mem_info = $row['mem_info'];
        $ship_link = $row['ship_link'];

        $display_fleet .= "
            <td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
        ";
    }

    $display_fleet .= "</tr>";
}

or, as discussed below:

$i=0
while($row = mysql_fetch_array($fleet_query_results)) {

    if( $i % 4 = 0 )
        $display_fleet .= "<tr>";

    $ship_info = $row['ship_info'];
    $mem_info = $row['mem_info'];
    $ship_link = $row['ship_link'];

    $display_fleet .= "
            <td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
        ";

    if( ++$i % 4 = 0 )
        $display_fleet .= "</tr>\n";

}

// clean add the missing cells to last row.
if( $i % 4 != 0 )
{
    while($i++ %4)
      $display_fleet .= "<td></td>";
    $display_fleet .= "</tr>\n";
}
Sign up to request clarification or add additional context in comments.

8 Comments

This looks like it will work, but I suspect it would be cleaner to remove the inner loop entirely.
yeah, it;s a mess. As you say the inner loop needs re-tooling. The inner loop formats the data into 4 columns, the columns seem to be arbitrary, unless there's something special about the query. probably a better solution woild be to just use fixed-size divs with style display:inline-block and let the brouser organise them rows and columns.
Or using modulus (%) to work out when to finish a table row and start a new one.
I am just getting back into coding... haven't done it in years (and reusing my old code), so if you have better ways of doing it, I am open. And I tried the modifications, but it doesn't display anything.
yeah, that sort of thing. I usually use a counter which I reset, but the same effect,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.