Re: PHP Bug? Problems with arrays
| From: | Tim Converse | Date: | Wed, 15 Nov 2000 07:38:16 +0000 |
| Subject: | Re: PHP Bug? Problems with arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-25389@lists.php.net to get a copy of this message | ||
Ivan ---
A for loop that begins like this
for ($j=0; $j <= 7; $j++) { ...
will go through 8 iterations. On the first iteration $j is 0, and on the
final one $j is 7. If you want only 7 iterations, then you either need to
start with $j = 1, or else use a less-than test rather than
less-than-or-equal.
--tim
On Wed, 15 Nov 2000 07:26:55 GMT, Ivan Quintero wrote:
> I am a PHP newbie (coming for about a year of PERL development). I wrote
> this function to determine the rows and values that go into an UPDATE sql
> statement.
>
> Going straight to the point, the problem I am having is that the last
"for"
> loop iterates one more time that is supposed to. The value for
> $total_columns is 7, but as you can see the array that it is populating
has
> 8 elements.
>
> Here is the code snippet. I have to apoligize in advance if this is some
> sort of obvious logical mistake on my part. It is rather late and I quit
> drinking coffee long time ago :).
>
> ### Begin code ####
> <?
> #initialize array with list of columns
> $column_list = array ("FirstName", "LastName", "MiddleName",
> "Birthday",
> "Nationality", "Client", "CountryBirth",
> "MarineID");
>
> #values for variables
> $firstname="lolo";
> $lastname= "lala";
> $middlename="";
> $birthday="1974-02-02";
> $nationality="martian";
> $client="1";
> $marineid="23423";
> $countrybirth="mars";
>
>
> #initialize array with list of values
> $arguments = array ($firstname, $lastname, $middlename, $birthday,
> $nationality, $client, $countrybirth, $marineid);
>
> $total_items = count ($column_list); # get the total number of columns
>
> for ($i=0; $i <= $total_items; $i++)
> {
>
> if ($arguments[$i]) #check to see if there is a value for the column
> {
>
> #Determine the type of data in array $values to decided
> #whether to use quotes or not.
>
> $values[$i] = "'". $arguments[$i]. "'"; #add to the list of
> values
>
> $columns[$i] = $column_list[$i];#add to the list of columns
>
> }
>
> }
>
>
> $total_columns = count($columns);
>
> for ($j=0; $j <= $total_columns; $j++)
> {
> $set_statement[$j] = "$columns[$j]=$values[$j]";
> echo $set_statement[$j] . "<p>";
>
> }
> echo $total_columns;
> exit;
> ?>
>
> ### End of code ####
>
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at
> http://www.hotmail.com.
>
> Share information about yourself, create your own public profile at
> http://profiles.msn.com.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
_______________________________________________________
Tired of slow Internet? Get @Home Broadband Internet
http://www.home.com/xinbox/signup.html