I have the following code which works just fine...

$result = mysql_query("SELECT * FROM test_prefixvehiclefeatures ORDER BY Feature ASC");
$count = 0;

  echo "<table border='1'>
<tr>
<th>Select a Feature</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
      $count = $count + 1;
  echo "<tr>";
  echo "<td>" . $row['Feature'] . "</td>";
  echo "<td><img src='images/unchecked.png' height='30px' width='30px' id='features".$count."' alt='alttext' onmousedown='change".$count."()' style='cursor: pointer;'></td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

My problem is that it only loads 1 record per row as to where I would like to have 2 rows next to each other. I'm not sure how to achieve this, any ideas?

This is what I get -

Airconditioner mycheckboxhere
Climate Control 2ndcheckboxhere

What I would like is -

Airconditioner mycheckboxhere    Climate Control  2ndcheckboxhere

Recommended Answers

All 11 Replies

Member Avatar for LastMitch

@AndreRet

My problem is that it only loads 1 record per row as to where I would like to have 2 rows next to each other. I'm not sure how to achieve this, any ideas?

You can try this:

$result = mysql_query("SELECT * FROM test_prefixvehiclefeatures ORDER BY Feature ASC");
$count = 0;
$i = 1;  
$numofcols = 2;

echo "<table border='1'>
<tr>
<th>Select a Feature</th>
</tr>";
while($row = mysql_fetch_array($result)){
if( $i % $numofcols == 1 ){
echo '<tr>'; //Open row
}
$count = $count + 1;
echo "<td>" . $row['Feature'] . "</td>";
echo "<td><img src='images/unchecked.png' height='30px' width='30px' id='features".$count."' alt='alttext' onmousedown='change".$count."()' style='cursor: pointer;'></td>";
if( $i % $numofcols == 0) {
echo '</tr>'; //Close Row.
}
$i = $i + 1;
if( ($i % $numofcols) > 0){
echo '</tr>';
}
echo "</table>";
}
mysql_close($con);
commented: Nope, I have Up Voted :) +0
commented: To Rectify what some retard did to LastMitch +0

First fix your table head

<th colspan="2">Select a feature</th>
commented: Thanx. +12

@LastMitch,

Thanx for the quick reply. It loads the records next to each other now as I would like it. It does however breaks out of the "grid" where only the first record is encased in the table border, all the rest is just piling up on empty space. :)

Any ideas. I'm playing with the code now as we speak, I think I can grasp what you did... ;)

Check the HTML that is being printed out to make sure it looks correct.
Every row starts with a <tr>. A </tr> ends the row. Based on your request, each row should have 4 columns in it, i.e., four pairs of <td>...</td>. If this is not the case, something in the loop is printing out the HTML incorrectly. If you can, post the HTML here so we can help debug.

Also, as per doubleN's suggestion above, you now need to adjust the colspan attribute on your th to 4, since you are working with four <td>s, not two.

put echo /table out of while loop (see line 24,25,26 they shold look like following)

}
echo "</table>";
mysql_close($con);
commented: Just caught that, thanx bro. +12

Thanx Evolution,

This is the code (basically exactly the same as LastMitch), for the life of me I can't get it to work... Maybe I'm brain dead tonight. ;)

Any ideas please?

$result = mysql_query("SELECT * FROM test_prefixvehiclefeatures ORDER BY Feature ASC");
$count = 0;
$i = 1;  
$numofcols = 2;

echo "<table border='1'>
<tr>
<th colspan='4'>Select a Feature</th>
</tr>";
    while($row = mysql_fetch_array($result)){
        if( $i % $numofcols == 1 ){
            echo '<tr>'; //Open row
        }

            $count = $count + 1;

            echo "<td>" . $row['Feature'] . "</td>";
            echo "<td><img src='images/unchecked.png' height='30px' width='30px' id='features".$count."' alt='alttext' onmousedown='change".$count."()' style='cursor: pointer;'></td>";

        if( $i % $numofcols == 0) {
            echo '</tr>'; //Close Row.
        }

    $i = $i + 1;

    if( ($i % $numofcols) > 0){
        echo '</tr>';
    }

    echo "</table>";
}
mysql_close($con);

Should just need to remove the echo "</table>"; from within your while loop, put it just fater your closing bracket as urtrivedi suggested. What's happening now if you view sorce you'll see that at the end of each row there will be an </table> and no new table started.

As urtrivedi and Glider mentioned, I just caught that a minute ago. Thanx guys. All working fine now!!

<del>So all's well now that the </table> has been moved?</del>
Sorry nevermind, just saw thread is solved.

Member Avatar for LastMitch

@AndreRet

You downvoted me for an example? In the future I will not help at all.

@LastMitch,

Sorry my man. I am sure that I have UPVOTED... must have been the exitement on getting the solution. ;) I have fixed it now. If you saw the comment you would have noticed that it said thank you. I even gave you a skill endorsement. I'm sure a thank you and down vote does not go hand in hand. Sorry again. :)

I see you gave me a down vote as well :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.