Jump to content

for loop help


Branden Wagner

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]
Link to comment
https://forums.phpfreaks.com/topic/15343-for-loop-help/
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
Link to comment
https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62185
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?


Link to comment
https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62197
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]
Link to comment
https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62201
Share on other sites

Archived

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

×
×
  • Create New...

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.