InterDevelop Posted April 13, 2006 Share Posted April 13, 2006 Help!!! I'm trying to walk through an array and present the information back to the screen in two columns and about 6-10 rows. Here's the code I'm working with, but I keep getting 6-10 of each array element. What am I missing here:[code] <table border="0" cellpadding="3" cellspacing="15" width="100%"><?php$db_results = array("Jeff", "Bobbi-Jo", "Carol", "John", "Fred", "Barney", "Bubba", "Mary-Lou");foreach($db_results as $values){ for($rows=0; $rows<8; $rows++) { echo "<tr class=\"admin\" bgcolor=\"#E2D09E\">\n"; for($cols=0; $cols<2; $cols++) { echo "<td id=\"form_searchB\">\n"; echo " <table align=\"left\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"; echo " <tr valign=\"top\">\n"; echo " <td class=\"form11\">\n"; echo " <b>$values \n"; echo " </td>\n"; echo " </tr>\n"; echo " </table>\n"; echo " </td>\n"; } echo "</tr>\n"; }}echo "</table>\n";?> </td> </tr> </table>[/code]I am new to PHP and I know my code looks pretty messy, but I'm open to suggestions.Thanks,Jeff Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/ Share on other sites More sharing options...
InterDevelop Posted April 13, 2006 Author Share Posted April 13, 2006 ***bump*** Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-26688 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2006 Share Posted April 13, 2006 Let's ignore the table formating for now and just concentrate on getting your data in to two columns.The following code will create two columns with the following format:name1 name2name3 name4etc...[code]<?php$db_results = array("Jeff", "Bobbi-Jo", "Carol", "John", "Fred", "Barney", "Bubba", "Mary-Lou");for($i=0;$i<count($db_results);$i++){ echo $db_results[$i]; if (($i+1) % 2 == 0) echo '<br>'; else echo ' ';}?>[/code]Once you have the data displaying in two column, you can add the presentation back in.Ken Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-26699 Share on other sites More sharing options...
InterDevelop Posted April 13, 2006 Author Share Posted April 13, 2006 Ken,Thanks for the info. That worked perfectly. In the future, I'm thinking of using an MySQL database to build the array. Would I use the same code or is there something else I need to do to pull the various elements out of a MySQL contact table The reason I ask, is that here's another chunk of code someone developed for me:[code]$sql = "SELECT * FROM $Table WHERE MemberID = $MemberID";$result = mysql_query($sql, $conn);if ($result && (mysql_num_rows($result) > 0)) { while ($row = mysql_fetch_assoc($result)) { <would I insert your code here to create the two columns? ... & replace $db_results with $row?> }}[/code]Is there a better way to build a 2 column list of contact info from members in a MySQL table?btw... thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-26757 Share on other sites More sharing options...
InterDevelop Posted April 14, 2006 Author Share Posted April 14, 2006 ***Bump*** anyone? Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-26943 Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 This the code I use for multi-column output from a db table[code]define ("NUMCOLS",3); // set as required$res = mysql_query("SELECT col1, col2 FROM mytable");$count = 0;echo "<TABLE border=1>";while (list($col1, $col2) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>$col1<br>$col2</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row}# end row if not already endedif ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n";}echo "</TABLE>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-27104 Share on other sites More sharing options...
doni49 Posted April 15, 2006 Share Posted April 15, 2006 All you do is pass the query result to this function.It highlights every other row.[a href=\"http://www.phpfreaks.com/quickcode/Table-Generator/171.php?higlight=table\" target=\"_blank\"]http://www.phpfreaks.com/quickcode/Table-G...?higlight=table[/a] Quote Link to comment https://forums.phpfreaks.com/topic/7320-2-columns-plus-6-10-rows/#findComment-27151 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.