Jump to content


Photo

for loop help


  • Please log in to reply
7 replies to this topic

#1 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 22 July 2006 - 07:05 PM

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
$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;
        }


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

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>";


#2 QuietWhistler

QuietWhistler
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationThe Netherlands

Posted 22 July 2006 - 07:31 PM

What's the error message PHP gives you?
Greetings, Shady EL Gewily

#3 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 22 July 2006 - 08:15 PM

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



#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 22 July 2006 - 09:01 PM

Does echo "Total:". count($customers); return 2 or 1?
Why doesn't anyone ever say hi, hey, or whad up world?

#5 JaGeK

JaGeK
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationNRW, Germany

Posted 22 July 2006 - 09:01 PM

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

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

#6 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 22 July 2006 - 09:48 PM

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?




#7 JaGeK

JaGeK
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationNRW, Germany

Posted 22 July 2006 - 10:04 PM

How would i accomplish using mysql_fetch_array,assoc,row?


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


#8 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 22 July 2006 - 10:14 PM

oh wow thanks thats like 30 lines shorter then the way i was doing it.. and it works!!! lol thanks again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users