genzedu777 Posted October 11, 2010 Share Posted October 11, 2010 Hi, I was thinking of creating a table for this current code and place only 3 options in a row, however I do not know how to execute a command which will allow me to do so, I have attached 2 pictures to describe what I will like to achieve. Below is my code as well. Any advice? Thanks $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysql_error()); while($data = mysqli_fetch_array($sql)) { echo'<table>'; echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>'; echo'<table>'; } Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/ Share on other sites More sharing options...
Spleshmen Posted October 11, 2010 Share Posted October 11, 2010 $count = 0; $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysql_error()); while($data = mysqli_fetch_array($sql)) { [$count++; echo'<table>'; echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>'; echo'<table>'; if ($count==3) { echo '<br>'; $count=0; } } Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121039 Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Hi Spleshmen, I would like to do it in table format, meaning to say I will also need to use <tr> and <td>, in columns of 3. Where can I add <tr> and <td>? Thanks $count++; echo'<table>'; echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>'; echo'<table>'; if ($count==3) { echo '<br>'; $count=0; Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121076 Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Hi, I did this codes, but it did not turn out to be what I wanted, I have attached the picture of rows which I would like to achieve. Any suggestion <?php $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error()); $count = 0; $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysql_error()); while($data = mysqli_fetch_array($sql)) { $count++; echo'<table>'; echo '<tr>'; echo '<td><input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>'; echo '<tr>'; echo'<table>'; if ($count==3) { echo '<br />'; $count = 0; } } ?> The HTML code turns out to be this... <table><tr><td><input name="district" type="checkbox" id="1" value="1"><label for="1">North</label></td> <tr><table><table><tr><td><input name="district" type="checkbox" id="2" value="2"><label for="2">North West</label></td> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121083 Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Hi, I have rectified my codes, but it still doesnt work. Any suggestions? <?php $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase') or die(mysqli_error()); $count = 0; $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysqli_error()); while($data = mysqli_fetch_array($sql)) { echo'<table>'; $count++; echo '<tr><td><input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>'; echo '</tr>'; if ($count==3) { echo '</tr></table><br />'; $count = 0; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121086 Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2010 Share Posted October 11, 2010 Will it always be the same 6 values for these checkboxes? Are multiple selections allowed? Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121094 Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Hi Pikachu2000, Multiple selections are allowed. It should be always the same value, as I am directly calling it from my database table. Any suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121112 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 Since it's always the same, just construct it all within the while loop. There's no need for a counter checking how many elements have been retrieved since you already know how many there will be. Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121317 Share on other sites More sharing options...
genzedu777 Posted October 13, 2010 Author Share Posted October 13, 2010 Hi, Currently, I have an issue with the alignment, attached is the picture as an example. In the picture, the one circled in red is what I want to achieve (rows of 3), however come to the 2nd line, which is circled in blue, it became (rows of 4) and misaligned, below is my coding. Can someone tell me where it went wrong? Thanks <?php $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase') or die(mysqli_error()); $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysqli_error()); echo'<table><tr>'; while($data = mysqli_fetch_array($sql)) { $count++; echo '<td><input name="district[]" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>'; if ($count==3) { echo '</tr></table>'; $count = 0; } } ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121727 Share on other sites More sharing options...
litebearer Posted October 13, 2010 Share Posted October 13, 2010 close your table OUTSIDE your loop Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121728 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 This is a common problem for PHP newcomers; x records per x columns. It is in this situation that you make use of PHP's modulus operator It is this operator that allows you to divide a number by X, but only get the remainder of it. Combine this with a counter to three and you have your solution. For example, using your current code... modified a bit. $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase') or die(mysqli_error()); $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysqli_error()); echo'</pre> <table>'; // Start your table outside the loop... and your first row $count = 0; // Start your counter while($data = mysqli_fetch_array($sql)) { /* Check to see whether or not this is a *new* row If it is, then end the previous and start the next and restart the counter. */ if ($count % 3 == 0) { echo ""; $count = 0;} echo ''; echo ''.$data['district_name'].''; $count++ //Increment the count } echo "</table>"; //Close your last row and your table, outside the loop<br>?&g Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121733 Share on other sites More sharing options...
genzedu777 Posted October 13, 2010 Author Share Posted October 13, 2010 Hi Zanus, It works!!! Finally! Thanks I have one last question. Currently, ID 1 - 15 in my database belongs to 'North' zone. How do I specify in my command, if I'm not wrong, it is got to do with 'LIMIT' command? $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); --> what should I add in here? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121788 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 if I'm not wrong, it is got to do with 'LIMIT' command? You are absolutely right! SELECT * FROM tutor_preferred_district ORDER BY district_id ASC LIMIT 15 Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121798 Share on other sites More sharing options...
genzedu777 Posted October 13, 2010 Author Share Posted October 13, 2010 Thanks. But beside 'North' zone, I have 'West', 'East' and 'South' North starts from 1 - 15 West 16 - 25 etc. So how do I perform the right coding for North and West? SELECT * FROM tutor_preferred_district ORDER BY district_id ASC LIMIT 1 TO 15? SELECT * FROM tutor_preferred_district ORDER BY district_id ASC LIMIT 16 TO 25? Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121801 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 LIMIT 1, 15 LIMIT 16, 25 etcetera Quote Link to comment https://forums.phpfreaks.com/topic/215614-loop-that-will-allow-3-records-to-appear-in-a-row-only/#findComment-1121803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.