just me and php Posted October 30, 2006 Share Posted October 30, 2006 This Code Is What I Was Looking For , But How Do I Make It Display Differently[code]<table cellspacing="3" cellpadding="3"><?php$query = "SELECT product FROM selling_items ORDER BY prod_id";$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0){ $i = 0; $max_columns = 3; while($row = mssql_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>";} ?></tr></table>[/code]It Displays Like This1 2 34 5 67 8 910 11And I Would Like It To Display Like This1 5 92 6 103 7 114 8 Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 30, 2006 Share Posted October 30, 2006 I think you'll have to rewrite it. You can write the code to retrieve the data into an array. Here's a way to do what you're asking for after the data is in the array.[code]<?php$maxrows = 4;//build an array for testing purposesfor ($i=0;$i<21;$i++){ $arr[$i]=$i;}$k = 0;$columns = count($arr)/$maxrows;echo "<table>";for($i=0;$i<$maxrows;$i++){ $row="<tr>"; for($j=0;$j<$columns;$j++){ if($k<count($arr)){ $row .= "<td>" . $arr[$k] . "</td>"; $k++; } } echo $row . "</tr>\n";}echo "</table>";?>[/code] Quote Link to comment Share on other sites More sharing options...
sinisake Posted October 30, 2006 Share Posted October 30, 2006 This should do the work:[code]<table border='1'><tr><?php$host='localhost';$dbuser='';$dbpass='';$database='';$link = mysql_connect($host, $dbuser,$dbpass)or die ("Could not connect to MySQL");mysql_select_db ($database)or die ("Could not select database");$query = "SELECT * FROM products";$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());$i = 0;$max_rows = 4;$num_rows=mysql_num_rows($result);while($row = mysql_fetch_array($result)){$i++;$br++;if($i == 1){echo "<td valign='top'>\n";echo "<table>\n";}if($i<=$max_rows){echo "<tr><td>$row[ID]</td></tr>\n";}if($i==$max_rows||$br==$num_rows){echo "</table>\n</td>\n";$i=0;}} ?></tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
just me and php Posted October 30, 2006 Author Share Posted October 30, 2006 thanks doni49 For Your Help , But I Used sinisake Code And Mod It To What I Was Looking For And It Worked Great :)Thanks Again Guys For Your Help Here And Thanks For This Forum 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.