Jump to content

Help fixing a multicolumn table/db array results


JsusSalv

Recommended Posts

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>

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!

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.