Jump to content


Photo

2 Columns plus 6-10 rows


  • Please log in to reply
6 replies to this topic

#1 InterDevelop

InterDevelop
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationGreenville, SC

Posted 13 April 2006 - 03:25 PM

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:
          <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>

I am new to PHP and I know my code looks pretty messy, but I'm open to suggestions.

Thanks,

Jeff

#2 InterDevelop

InterDevelop
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationGreenville, SC

Posted 13 April 2006 - 06:12 PM

***bump***

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 06:50 PM

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...

<?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 ' ';
}
?>

Once you have the data displaying in two column, you can add the presentation back in.

Ken

#4 InterDevelop

InterDevelop
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationGreenville, SC

Posted 13 April 2006 - 09:25 PM

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:
$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?>
    }
}
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.

#5 InterDevelop

InterDevelop
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationGreenville, SC

Posted 14 April 2006 - 12:50 PM

***Bump*** anyone?

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 14 April 2006 - 10:30 PM

This the code I use for multi-column output from a db table
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>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 15 April 2006 - 02:55 AM

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]
Don




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users