PHP Bug? Problems with arrays
| From: | Ivan Quintero | Date: | Wed, 15 Nov 2000 07:26:54 +0000 |
| Subject: | PHP Bug? Problems with arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-25387@lists.php.net to get a copy of this message | ||
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.