Branden Wagner Posted July 22, 2006 Share Posted July 22, 2006 [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 More sharing options...
QuietWhistler Posted July 22, 2006 Share Posted July 22, 2006 What's the error message PHP gives you? Link to comment https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62158 Share on other sites More sharing options...
Branden Wagner Posted July 22, 2006 Author Share Posted July 22, 2006 sorry..there is no error message, but it only displays the first entry.i do a search for smith... and it shows:1 Bob Smithi do a search for john2 John Smiththere are 2 entries for smith but yet it only shows one Link to comment https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62171 Share on other sites More sharing options...
corbin Posted July 22, 2006 Share Posted July 22, 2006 Does echo "Total:". count($customers); return 2 or 1? Link to comment https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62184 Share on other sites More sharing options...
JaGeK Posted July 22, 2006 Share Posted July 22, 2006 [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 More sharing options...
Branden Wagner Posted July 22, 2006 Author Share Posted July 22, 2006 well heres what i want to do...$customers[$i] = each customers record$customer[$i][$i2] = 0: CustomerID 1: FirstName 2: LastNameExample:$customer[0][0] = 100$customer[0][1] = Bob$customer[0][2] = Smith$customer[1][0] = 101$customer[1][1] = John$customer[1][2] = SmithHow 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 More sharing options...
JaGeK Posted July 22, 2006 Share Posted July 22, 2006 [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 More sharing options...
Branden Wagner Posted July 22, 2006 Author Share Posted July 22, 2006 oh wow thanks thats like 30 lines shorter then the way i was doing it.. and it works!!! lol thanks again Link to comment https://forums.phpfreaks.com/topic/15343-for-loop-help/#findComment-62203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.