ohdang888 Posted December 26, 2007 Share Posted December 26, 2007 hey. heres the code...pretty simple. <?php mysql_connect("localhost", "root", "pancakes1") or die(mysql_error()); mysql_select_db("related") or die(mysql_error()); $result = mysql_query("SELECT * FROM action ORDER by name ASC") or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row[0]; echo '<br>'; } ?> how do i create a results table and put 10 results in each column?? Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/ Share on other sites More sharing options...
tinker Posted December 26, 2007 Share Posted December 26, 2007 How do you want it? 1 2 3 4 or 1 3 2 4 Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423695 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 From the snippet forum http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423701 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 i want it 1 3 2 4 this is what that link is telling me too add after i carry ou tthe query if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($product != "" && $product != null) echo "<td>$product</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> but i'm confused as too where i specify the max amount of rows in the column. Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423707 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 Since you posted this, that code shows you how to do it with columns. how do i create a results table and put 10 results in each column?? Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423711 Share on other sites More sharing options...
tinker Posted December 26, 2007 Share Posted December 26, 2007 You don't, when you write a table you do it row by row, adding columns each time. Therefore it just makes as many rows as needed. However if you wanted a max then you'd use a variable to count within the while loop and when the max count is reached it exits the while. Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423714 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 ohhhhhhhhh ok. thanks tinker! Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423732 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 but i still don't understand what i am supposed to do to make that code work. Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423736 Share on other sites More sharing options...
tinker Posted December 26, 2007 Share Posted December 26, 2007 i want it 1 3 2 4 If you want it this way and to teeter out then you got even more work to do cos you gotta either know the max rows (and cols) and end up with... 1 3 2 4 again, or you need to know as before but truncate and miss data to produce... 1 5 2 6 best way to think about it is to write it down and consider how you did it (but also try to think like the code is written!). It might all sound confusing, but with a little thought it's all really simple... all else fails, try some trial and error! Quote Link to comment https://forums.phpfreaks.com/topic/83281-putting-results-in-columns/#findComment-423738 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.