Darkmatter5 Posted April 3, 2009 Share Posted April 3, 2009 I'm not quite sure how I should go about doing this. I have 2 variables to construct this grid of numbers. $totsys is the number of items in a particular array. $totrows is the ceiling of $totsys/4 columns. So $totsys=mysql_num_rows($result); $totrows=ceil($totsys/4); Now if $totsys=17 which now makes $totrows=5 how can I build code to generate a grid from 0-16 with 4 columns and no more then 5 rows? Example desired output: 0 5 10 15 1 6 11 16 2 7 12 3 8 13 4 9 14 I think this is by using a for loop, but not really sure. Any help is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
benphp Posted April 3, 2009 Share Posted April 3, 2009 Here's a grid generator. You can pick it apart and use it to your needs. <form action="" method="post"> <input type="text" name="cols" value="4" size="2" maxlength="2"> <input type="submit" value="Go" name="btnGo"> </form> <?php if (isset($_POST['btnGo']) && $_POST['cols'] > 0 && $_POST['cols'] != "") { $cols = $_POST['cols']; //number of columns to use } else { $cols = 4; //number of columns to use } print "\n<form action=\"\" method=\"post\">"; print "\n<input type=\"text\" name=\"cols\" value=\"$cols\" size=\"2\" maxlength=\"2\">"; print "\n<input type=\"submit\" value=\"Go\" name=\"btnGo\">"; print "\n</form> \n"; $anyOldArray = array("1. Nemo","2. Marlin","3. Coral","4. Dory","5. Phil","6. Bob","7. Bloat","8. Gurgle","9. Pearl","10. Bruce","11. Chum","12. Nigel","13. Gill","14. Jacque"); print "<table border=\"1\"> \n<tr>"; //print the table $colCount = 1; //counts the column number being printed, starting at 1 - you're going to have at least one column. $numElements = count($anyOldArray); //count number of elements in array $i = 0; //loop counter //This while loop does two things: it prints a cell when an element exists, and it starts a new row if //there are more elements and it has reached the end of a row. while($i < $numElements) { //Do this while loop counter is less than total array element count. //Find the last cell in the row. If $colCount==$cols then this is the last column in the row, so end it and start another //The $i < $numElements-1 means DON'T start another row, because there aren't any more elements. For example, //you don't want to start a new row when you have 4 columns and 8 elements. if(($colCount==$cols) && ($i < $numElements-1) ) { print "\n\t<td>$anyOldArray[$i] </td> \n</tr> \n<tr>"; $colCount = 1; //reset the column counter to 1, because you're starting a new row, so the first column is $colCount 1 } elseif($numElements > 0) { //if it isn't the last cell, just print a regular cell print "\n\t<td>$anyOldArray[$i] </td> "; $colCount++; //add to your $colCount, because you just printed another element in a column. } $i++; //increment your loop counter. } //The above loop will write only cells that have elements to display in them. So when you run out of elements, what then? //The following loop fills in the remaining cells when your elements run out - so you don't get a table shaped like an F. //While fill out the remaining cells with a space [ number of columns is greater than the current row count ] //$colCount is set by the above script and is stuck at the last column printed. So if you have 14 elements in 4 columns, and you're on your last //row, the last printed element was in column #2 while ($colCount <= $cols){ //keep printing cells until you reach the number of columns you want printed. print "\n\t<td> </td>"; $colCount++; //keeps incrementing the number of columns printed - picks up where the first WHILE statement left off. } print "\n</tr> \n</table> \n"; //close the row and table. ?> Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 3, 2009 Share Posted April 3, 2009 <?php $totsys=mysql_num_rows($result); $totrows=ceil($totsys/4); $arr = array(); $counter=0; for($col=0;$col<4;$col++) { for($row=0;$row<$totrows;$row++) { $arr[$row][$col] = $counter++; if ($counter > $totsys) continue 2; } } print_r($arr); ?> that should do what you need it to. Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted April 6, 2009 Author Share Posted April 6, 2009 Here's my code: $query="SELECT system_id, name FROM systems ORDER BY name ASC"; $result=mysql_query($query) or die(mysql_error()); $totsys=mysql_num_rows($result); $totrows=ceil($totsys/4); $row=mysql_fetch_array($result,MYSQL_NUM); echo "<tr><td colspan='2'><fieldset><legend>System(s)</legend>"; $counter=0; for($rowi=0;$rowi<$totrows;$rowi++) { for($coli=0;$coli<4;$coli++) { if($counter>($totsys-1)) continue; echo "$row[$counter] "; $counter=$counter+$totrows; } echo "<br>"; $counter=$counter-($counter-($rowi+1)); } echo "</fieldset></td></tr>"; Here's the result of the query: system_idname 9Gameboy Advanced 11GameCube 17Mac 12NES 3Nintendo 64 10Nintendo DS 14PC 4Playstation 2 5Playstation 3 8Playstation One 13PSP 2Super Nintendo 1UNKNOWN 16Wii 18Wii (VC) 7Xbox 6Xbox 360 I've never really specified an array type within a mysql_fetch_array command, I usually can just do something like: while($row=mysql_fetch_array($result) { echo "$row[name]<br>"; } But I'm having to display array elements out of sequence as I'm displaying my elements in columns not rows. In my first post is an example of my desired element output. The numbers represent the array element id I need displayed at that location of the grid. Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted April 6, 2009 Author Share Posted April 6, 2009 From what I've been reading I should be able to do this, still with $row=mysql_fetch_array($result) and still reference each element but echo "$row[$counter][name] "; But doing $row=mysql_fetch_array($result); doesn't seem do return anything but the first element and that's it. Quote Link to comment 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.