forumnz Posted January 25, 2008 Share Posted January 25, 2008 Can someone please show me a simple way of adding 1, each time a while loop is performed? Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/ Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 $i++ or $i += 1 Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449248 Share on other sites More sharing options...
cooldude832 Posted January 25, 2008 Share Posted January 25, 2008 <?php $i = 0; while($i < 50){ echo "Hello World<br />\n"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449250 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 I'm testing something and using the code provided by cooldude832 (modified), but I just can't make it work for me. Basically what I'm trying to do is pull rows from a db (sorted), and place them in columns like: 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15 How do I do that? Please help this is important (and frustrating ) Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449263 Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 You want to use a while loop and a mysql_fetch function. You don't need to use a +1 variable for that. Post the code where you read the DB and display the data. Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449267 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 <?php $cat = $_GET['cat']; include('../connectdb.php'); $sql = mysql_query("SELECT * FROM ffcats WHERE pid='$cat' ORDER BY id ASC"); if ($cat == "") { include('nonechosen.php'); } else { echo "<table><tr><td>"; while($row = mysql_fetch_array($sql)) { $no = $name = $row['name']; $a = $row['id']; echo "<a href=?cat=" . $a . ">" . $name . "</a><br>"; } echo "</td></tr></table><br />"; } ?> At the moment I get: 1 2 3 4 5 6 7 8 etc. How can I get what I previously stated? Thanks heaps! Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449270 Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 And let's see your DB table layout. Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449272 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Ok, Database db1 Table structure for table ffcats Field Type Null Default id int(4) Yes NULL pid int(4) Yes name varchar(25) Yes Dumping data for table ffcats 92 69 Papakura District 91 69 North Shore City 90 69 Manukau City 89 69 Franklin 88 69 Auckland City 213 83 Pakaraka 212 83 Paihia 85 68 Whangarei 84 68 Kaipara 83 68 Far North 82 0 Southland Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449274 Share on other sites More sharing options...
Zhadus Posted January 25, 2008 Share Posted January 25, 2008 Try changing while($row = mysql_fetch_array($sql)) { $no = $name = $row['name']; $a = $row['id']; echo "<a href=?cat=" . $a . ">" . $name . "</a><br>"; } echo "</td></tr></table><br />"; } to this: $count = 0; while($row = mysql_fetch_array($sql)) { $name[$count] = $row['name']; $a[$count] = $row['id']; $count++; } for($num==0;$num<$count;$num++) { $count2 = 0; while(($count2<3)and($a[$count]!=null)) { echo "<a href=?cat=" . $a[$count] . ">" . $name[$count] . "</a> "; $count++; $count2++; } echo "<br>"; } echo "</td></tr></table><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449284 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Nothing shows up? Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449286 Share on other sites More sharing options...
Zhadus Posted January 25, 2008 Share Posted January 25, 2008 Haha, sorry, my mistake $count = 0; while($row = mysql_fetch_array($sql)) { $name[$count] = $row['name']; $a[$count] = $row['id']; $count++; } for($num==0;$num<$count;$num++) { $count2 = 0; while(($count2<3)and($a[$num]!=null)) { echo "<a href=?cat=" . $a[$num] . ">" . $name[$num] . "</a> "; $num++; $count2++; } echo "<br>"; } echo "</td></tr></table><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449289 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Nice try... still doesn't work though. Now, I get: K W Instead of: Far North Kaipara Whangarei (so the first one is missing and they aren't going in columns. Should be: 1 6 2 7 3 etc 4 5 not: 1 2 3 4 5 6 etc Any ideas? Thanks heaps! Big help! Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449294 Share on other sites More sharing options...
Zhadus Posted January 25, 2008 Share Posted January 25, 2008 A little manipulation should fix it I think O.o $count = 0; while($row = mysql_fetch_array($sql)) { $namex[$count] = $row['name']; $a[$count] = $row['id']; $count++; } for($num==0;$num<$count;$num++) { $count2 = 0; while(($count2<3)and($a[$num]!=null)) { echo "<a href=?cat=" . $a[$num+$count2] . ">" . $namex[$num+$count2] . "</a> "; $count2++; } echo "<br>"; } echo "</td></tr></table><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449298 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Awesome... looking better. Problem: Instead of getting: Far North Kaipara Whangarei I get: Kaipara Whangarei Whangarei (so Whangarei is repeating and Far North is not displaying) We must be close now? Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449300 Share on other sites More sharing options...
KrisNz Posted January 25, 2008 Share Posted January 25, 2008 You're going to need to read all your db rows into an array before you start trying to draw your table because of the way you're displaying your data. This example is pretty basic and inflexible but hopefully it will point you in the right direction. <?php $rows[] = "row 1"; $rows[] = "row 2"; $rows[] = "row 3"; $rows[] = "row 4"; $rows[] = "row 5"; $rows[] = "row 6"; $rows[] = "row 7"; $rows[] = "row 8"; $rows[] = "row 9"; $rows[] = "row 10"; $rows[] = "row 11"; $rows[] = "row 12"; $rows[] = "row 13"; $rows[] = "row 14"; $rows[] = "row 15"; $numColumns = 3; $rowCount = count($rows); $numRows = (int) $rowCount /$numColumns; ?> <table> <tr> <th>col 1</th> <th>col 2</th> <th>col 3</th> </tr> <?php $j = 0; for ($i=0; $i<$numRows ; $i++) : ?> <tr> <td> <?php echo $rows[$j] ?> </td> <td> <?php echo $rows[$j+5] ?> </td> <td> <?php echo $rows[$j+10] ?> </td> </tr> <?php $j++ ; endfor; ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449301 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.