Jump to content

Archived

This topic is now archived and is closed to further replies.

InterDevelop

2 Columns plus 6-10 rows

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 name2
name3 name4

etc...

[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</TR>\n";
}
echo "</TABLE>";
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.