Re: Loop issues

From: Date: Fri, 16 Nov 2007 20:37:44 +0000
Subject: Re: Loop issues
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-264726@lists.php.net to get a copy of this message
Yeah...see...I'm confused apparently! My while loop is getting all of the names correctly, apparently my problem is...once i get them, how do I echo each one out seperately? Based off of the print_r($cs_row) I can see all of the names. What should I be doing differently to output each name into my table?
What you're doing wrong: Assume mssql_fetch_array($cs_type) has two rows: request_type = 'type1', first_name = 'Bob', last_name = 'Consumer' request_type = 'type2', first_name = 'Alice', last_name = 'Cooper' So we can replace it with a variable $array that has the same data. $array = array( array( 'request_type' => 'type1', 'first_name' => 'Bob', 'last_name' => 'Consumer' ), array( 'request_type' => 'type2', 'first_name' => 'Alice', 'last_name' => 'Cooper' ), ); Then this: while ($cs_row = iterator($array)) { $cs_type2 = $cs_row['request_type']; $cs_name = $cs_row['first_name']." ".$cs_row['last_name']; print_r ($cs_row); } Is functionally the same as: $cs_type2 = $array[0]['request_type']; $cs_name = $array[0]['first_name']." ".$array[0]['last_name']; print_r ($array[0]); $cs_type2 = $array[1]['request_type']; $cs_name = $array[1]['first_name']." ".$array[1]['last_name']; print_r ($array[1]); Now. Why would the print_r return the right information? Because, you're printing it for EACH row. Why would the variable $cs_type2 have only the information of the last row? Because, you're OVERwriting it on EACH row.

« previous php.general (#264726) next »