1

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

Edited by AndreRet

6
Contributors
11
Replies
13
Views
4 Years
Discussion Span
Last Post by AndreRet
Featured Replies
  • **@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 … Read More

  • First fix your table head <th colspan="2">Select a feature</th> Read More

  • put echo /table out of while loop (see line 24,25,26 they shold look like following) } echo "</table>"; mysql_close($con); Read More

  • 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. Read More

  • **@AndreRet** You downvoted me for an example? In the future I will not help at all. Read More

6

@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);
Votes + Comments
To Rectify what some retard did to LastMitch
Nope, I have Up Voted :)
0

@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... ;)

0

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.

1

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

}
echo "</table>";
mysql_close($con);
Votes + Comments
Just caught that, thanx bro.
0

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);
1

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.

0

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

0

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

Edited by EvolutionFallen

4

@AndreRet

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

0

@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 :)

Edited by AndreRet

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.