samoht Posted August 20, 2007 Share Posted August 20, 2007 Hello Again, I have a new issue with a display problem. I have a list of features pulling from a feature table. The list is organized by Type and when ever I get a new Type I start a new column (actually a new table) some of my Types have 3 or 4 features in the list and some 40-50 - I would like to be able to display in the HTML equal columns - say for example 15 entries per column. Is there an easy way to do this?? Here is my do while loop <table border="0" cellspacing="0" cellpadding="0" align="left" style="padding-right:5px"> <?php $LastType = ''; do { # If $FeatureExists is true, then the checkbox will be activated, else it will be disabled (but still visible). $FeatureExists = false; do { if ($row_rsFeatures[FeatureId] == $row_rsAllFeatures[FeatureId]) $FeatureExists = true; } while ($row_rsFeatures = mysql_fetch_assoc($rsFeatures)); mysql_data_seek($rsFeatures,0); $row_rsFeatures = mysql_fetch_assoc($rsFeatures); # If $LastType is different, a new column will be started and the heading for that column will be displayed. if ($LastType != $row_rsAllFeatures[Type]) { $LastType = $row_rsAllFeatures[Type]; echo "</table><table border='0' cellspacing='0' cellpadding='0' align='left' style='padding-right:5px'>"; echo "<tr><td> </td></tr><tr><td class='sb'>$row_rsAllFeatures[Type]</td><tr>"; }?> <tr> <td nowrap="nowrap" style="font-size:9px"><input onclick="changeColor(this.id, '#009900');" type="checkbox" name="FeatureId[]" id="FeatureId<?php echo $row_rsAllFeatures[FeatureId]; ?>" value="<?php echo $row_rsAllFeatures[FeatureId]; ?>" <?php foreach($ItemsFeatures as $FeatureId){ if($FeatureId==$row_rsAllFeatures[FeatureId]) echo 'checked'; } if (!$FeatureExists || $row_rsAllFeatures[Type]=='Material') echo ' disabled'; ?> tabindex="18" /> <label id="LabelFeatureId<?php echo $row_rsAllFeatures[FeatureId]; ?>" for="FeatureId<?php echo $row_rsAllFeatures[FeatureId]; ?>" <?php $Found = false; foreach($ItemsFeatures as $FeatureId){ if($FeatureId==$row_rsAllFeatures[FeatureId]) $Found = true; } $var = !$FeatureExists ? "Disabled" : $row_rsAllFeatures[Type]; switch ($var) { case('Disabled'): case('Material'): echo $Found ? 'style="color:#009900;cursor:default"' : 'style="color:#666;cursor:default"'; break; default: echo $Found ? 'style="color:#009900"' : 'style="color:#000000"'; } ?>><?php echo $row_rsAllFeatures['Name']; ?></label></td> </tr> <?php } while ($row_rsAllFeatures = mysql_fetch_assoc($rsAllFeatures)); ?> </table></td> Quote Link to comment https://forums.phpfreaks.com/topic/65842-solved-do-while-limit-number-per-row-help/ Share on other sites More sharing options...
Psycho Posted August 20, 2007 Share Posted August 20, 2007 It was too confusing to go through your code topick out the relevant pieces. here is a mock up that should show one method of accomplishing what you want. hope it helps: <?php $max_per_col = 15; $current_type = ""; $feature_num = 0; echo "<table><tr>"; while ($row = mysql_fetch_assoc($result)) { //If new type show new type header if ($current_type != $row['type']) { //If not first type close previous type if ($current_type != "") { echo "</td>\n" } $current_type = $row['type']; echo "<td><b>$current_type:</b><br>\n"; } echo $row['feature'] . "<br>\n"; $feature_num++; //If the 15th item close td and start new one if ($feature_num >= $max_per_col) { echo "</td><td>\n"; $feature_num = 0; } } echo "</td></tr></table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65842-solved-do-while-limit-number-per-row-help/#findComment-329232 Share on other sites More sharing options...
samoht Posted August 21, 2007 Author Share Posted August 21, 2007 Sorry about the ugly code - its from an old page someone else wrote (I think using Dreamweaver - thats why all the absurd variable names) Anyway with your suggestion I came up with: <?php $max_per_col = 15; $current_type = ""; $feature_num = 0; echo '<table align="left"><tr>'; while ($row = mysql_fetch_assoc($rsAllFeatures)) { //If new type show new type header if ($current_type != $row['Type']) { //If not first type close previous type if ($current_type != "") { echo "</table><table align='left'>\n"; } $current_type = $row['Type']; echo "<tr><td><b>$current_type:</b></td><tr>\n"; } echo "<tr><td style=\"font-size:9px\"><input onClick=\"changeColor(this.id, \"#009900\");\" type=\"checkbox\" name=\"FeatureId[]\" id=\"FeatureId$row[FeatureId]\" value=\"$row[FeatureId]\""; foreach($ItemsFeatures as $FeatureId){ if($FeatureId==$row_rsAllFeatures[FeatureId]) echo "checked"; } echo ">"; echo "<label id=\"LabelFeatureId".$row['FeatureId']."\" for=\"FeatureId".$row['FeatureId']."\">".$row['Name']."</label></td></tr>\n"; //echo $row['Name'] . "<br>\n"; $feature_num++; //If the 15th item close td and start new one if ($feature_num >= $max_per_col) { echo "</table><table align='left'>\n";//"</td><td>\n"; $feature_num = 0; } } echo "</td></tr></table>\n"; Which works OK but it is counting the $feature_num all together. I would like to count the $feature_num inside or for each "Type" so that if there is less than 15 then it goes to the next Type normally but if there is more than 15 the remainder start a new row. New Types already start a new row - they should not be tacted on to the end of the previous Type. The problem currently is that for my first column I have 12 entries so the way the code is set up my second column will only have 3 entries because I started a new type but $feature_num is going to kick me into a new row once I hit 15 I only want to kick into a new row if the current_type has more than 15 - does that make sense?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/65842-solved-do-while-limit-number-per-row-help/#findComment-329457 Share on other sites More sharing options...
Psycho Posted August 21, 2007 Share Posted August 21, 2007 All you should need to do is move the $feature_num = 0; to inside the if statement to determine if it is a new header. <?php $max_per_col = 15; $current_type = ""; echo '<table align="left"><tr>'; while ($row = mysql_fetch_assoc($rsAllFeatures)) { //If new type show new type header if ($current_type != $row['Type']) { //If not first type close previous type if ($current_type != "") { echo "</table><table align='left'>\n"; } $current_type = $row['Type']; echo "<tr><td><b>$current_type:</b></td><tr>\n"; $feature_num = 0; } echo "<tr><td style=\"font-size:9px\"><input onClick=\"changeColor(this.id, \"#009900\");\" type=\"checkbox\" name=\"FeatureId[]\" id=\"FeatureId$row[FeatureId]\" value=\"$row[FeatureId]\""; foreach($ItemsFeatures as $FeatureId){ if($FeatureId==$row_rsAllFeatures[FeatureId]) echo "checked"; } echo ">"; echo "<label id=\"LabelFeatureId".$row['FeatureId']."\" for=\"FeatureId".$row['FeatureId']."\">".$row['Name']."</label></td></tr>\n"; //echo $row['Name'] . "<br>\n"; $feature_num++; //If the 15th item close td and start new one if ($feature_num >= $max_per_col) { echo "</table><table align='left'>\n";//"</td><td>\n"; $feature_num = 0; } } echo "</td></tr></table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/65842-solved-do-while-limit-number-per-row-help/#findComment-329541 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.