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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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