06-27-2008, 01:15 AM
|
#4 (permalink)
|
|
The Gregarious
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
|
Don't worry about putting the numbers in there, though you can, PHP will automagically assign them a number. I actually did this just recently on a page where I was setting configuration values, and the code looked like (after performing sanitization of the data elsewhere and error checking it as well):
PHP Code:
// Get the database connection $this->pSQLconn = $pSQL->returnSQL();
// Add the new data to the database foreach ($aData as $key => $value) { $q = sprintf("UPDATE `".TBL_PREFIX."config` SET cfg_value='%s' WHERE cfg_name='%s'", $this->pSQLconn->real_escape_string($value), strtoupper($key)); $bSuccess = ($this->pSQLconn->query($q)) ? true : false; if ($bSuccess === false) break; // If there were any problems along the way, drop out }
return (bool) $bSuccess;
However I tend to favour foreach when I can over for loops, but you could do it just like you were saying too. Note that this was from an associative array, from a numerical array I would just use a foreach ($row_data as $value) .. since you probably don't need the number unless you're using that in the database table.
-m
|
|
|
|