Jump to content

Archived

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

Branden Wagner

for loop help

Recommended Posts

[sub][/sub]Ok, im having a problem with my for loop, and i dont know what im doing wrong.
im building my own Customer  administration center....
the array $customers is multidimensional
[code]
$num= mysql_num_rows($result);
        if($num > 0)
        {
                $list = array();
                for($i=0;$i<count($num); $i++)
                {
                        $customer= array(
                                        mysql_result($result,$i,"CustomerID"),
                                        mysql_result($result,$i,"FirstName"),
                                        mysql_result($result,$i,"LastName")
                                );
                        array_push($list, $customer);
                }
                $searchresult = $list;
        }
        else
        {
                $searchresult = 0;
        }
[/code]


$customer[$i] = list of customers
$customer[$i][$i2] = id, firstname, lastname (in that order)

[code]
echo "Total:". count($customers);

echo "<table width=\"100%\" border=\"1\">";
echo "<tr align=\"center\">";
echo "<th scop=\"col\">ID</td>";
echo "<th scop=\"col\">First Name</td>";
echo "<th scop=\"col\">Last Name</td>";
echo "</tr>";

for($i=0; $i <= count($customers); $i++)
{
        echo "<tr>"; 
        for($i2=0; $i2 <= count($customers[$i]) ; $i2++)
        {
                echo "<td>". $customers[$i][$i2] ."</td>";
        }
        echo "</tr>";
}
echo "</table>";
[/code]

Share this post


Link to post
Share on other sites
sorry..
there is no error message, but it only displays the first entry.
i do a search for smith... and it shows:

1 Bob Smith


i do a search for john

2 John Smith

there are 2 entries for smith but yet it only shows one

Share this post


Link to post
Share on other sites
Does echo "Total:". count($customers); return 2 or 1?

Share this post


Link to post
Share on other sites
[quote author=Branden Wagner link=topic=101487.msg401780#msg401780 date=1153599322]there are 2 entries for smith but yet it only shows one[/quote]
The parameter $num - you pass to count() - is not of type array or object. So the return  value would always be 1 - independent of how many rows are affected.

It would be much easier to use the mysql_fetch_*()-functions instead of mysql_result().

-> http://php.net/mysql_fetch_assoc

Share this post


Link to post
Share on other sites
well heres what i want to do...

$customers[$i] = each customers record
$customer[$i][$i2] = 0: CustomerID  1: FirstName  2: LastName

Example:
$customer[0][0] = 100
$customer[0][1] = Bob
$customer[0][2] = Smith

$customer[1][0] = 101
$customer[1][1] = John
$customer[1][2] = Smith

How would i accomplish using mysql_fetch_array,assoc,row?


Share this post


Link to post
Share on other sites
[quote author=Branden Wagner link=topic=101487.msg401809#msg401809 date=1153604913]How would i accomplish using mysql_fetch_array,assoc,row?[/quote]

[code=php:0]while ($row = mysql_fetch_assoc($result)) {
    $customer[] = array(
                    $row['CustomerID'],
                    $row['FirstName'],
                    $row['LastName']
                  );
}[/code]

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.