JsusSalv Posted September 26, 2008 Share Posted September 26, 2008 Hello Everyone: I finally got my code working except for one last little bit. My original code would create one long vertical column. I've changed that and now am able to get the table to split into four rows and four columns using the modulo operator. However, the code doesn't like having a do{} loop within the while{} loop. Is there a way to modify this code so that it creates a 4 X 4 table WITH the proper db array results? Currently the array only returns the first database record and fills each table cell with this value. I'd like for all of the categories to fill the appropriate table cells. Thank you! <table> <tr> <th><input type="checkbox" onclick="checkAllFields(1);" id="checkAll" /> Check/UnCheck</th> <td><input type="button" name="button" class="selectCount" id="removeChecked" value="[0] Selected" /></td> </tr> <tr> <?php while ($cat = mysql_fetch_assoc($catList)) { // define number of columns in table define('COLS', 4); // initialize cell counter outside loop $pos = 0; do { ?> <td> <input type="checkbox" name="category[]" id="category[]" onclick="checkAllFields(2);" value="<?php echo $cat['category']; ?>" <?php $catcheck = explode(',',$cat['category']); foreach($catcheck as $catchecked) $check = explode(',',$row['category']); foreach($check as $checked) if ($catchecked == $checked) {echo 'checked="checked"';} ?> /><?php echo $cat['category']; ?> </td> <?php $row = mysql_fetch_assoc($catList); // increment counter after next row extracted $pos++; // if at end of row and records remain, insert tags if ($pos%COLS === 0 && is_array($row)) { echo '</tr><tr>'; } } while($row); // end of loop // new loop to fill in final row while ($pos%COLS) { echo '<td> </td>'; $pos++; } ?> <?php } ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/ Share on other sites More sharing options...
JsusSalv Posted September 26, 2008 Author Share Posted September 26, 2008 Am I executing the do whilw loop incorrectly? Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/#findComment-651476 Share on other sites More sharing options...
JsusSalv Posted September 27, 2008 Author Share Posted September 27, 2008 Any ideas?? Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/#findComment-651628 Share on other sites More sharing options...
F1Fan Posted September 27, 2008 Share Posted September 27, 2008 So wait, do you just have four columns per row coming out of a database, and you want to display all four columns per row on a table? List your query, and list an example of what you want the output to LOOK like. Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/#findComment-651722 Share on other sites More sharing options...
JsusSalv Posted September 27, 2008 Author Share Posted September 27, 2008 I would like the categories table cell to look like this: [] = checkbox Categories |----------------------------------------------------------| | []Accessories | []Art Gallery | []Clothing | []Costume | |----------------------------------------------------------| | []Shoes | [] Sports | []Dining | []Books | |----------------------------------------------------------| | []Candy | []Silverware | []Museum | []Mall | |----------------------------------------------------------| | []Movies | []Food | []Balloons | []Park | |----------------------------------------------------------| Don't worry about the categories...they are just stand-ins for the time being. The code snippet I've posted creates the above gridview. However, instead of having all the wonderful categories in their respective table cells I get this: Categories |-------------------------------------------------------------------| | []Accessories | []Accessories | []Accessories | []Accessories | |-------------------------------------------------------------------| | []Accessories | []Accessories | []Accessories | []Accessories | |-------------------------------------------------------------------| | []Accessories | []Accessories | []Accessories | []Accessories | |-------------------------------------------------------------------| | []Accessories | []Accessories | []Accessories | []Accessories | |-------------------------------------------------------------------| I know the do{} is doing its job by performing the first expression but I can't figure out why it won't execute the rest of the categories from the database. Please help. Thank you! Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/#findComment-651777 Share on other sites More sharing options...
F1Fan Posted September 28, 2008 Share Posted September 28, 2008 I do this kinda thing all the time. If I understand what you're trying to do, model your code off of something like this: <table> <tr> <?php $count = 1; $columns = 4; // This is how many columns you want to have. Change this as you wish while ($row = mysql_fetch_assoc($result)){ if ($count==$columns){ echo "</tr><tr>"; $count = 1; } echo "<td> <input type=\"checkbox\" name=\"category[]\" id=\"category[]\" onclick=\"checkAllFields(2);\" value=\"{$cat['category']}\"; $catcheck = explode(',',$cat['category']); foreach($catcheck as $catchecked) $check = explode(',',$row['category']); foreach($check as $checked) if ($catchecked == $checked) echo 'checked="checked"'; echo "/>{$cat['category']}</td>"; $count++; } while ($count<$columns) { echo "<td> </td>"; // This is to fill in the last row with blank cells $count++; } </tr> </table> Didn't check the syntax real closely, but that should be close. Link to comment https://forums.phpfreaks.com/topic/125974-help-fixing-a-multicolumn-tabledb-array-results/#findComment-652155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.