making my array public
Posted: Thu Mar 01, 2007 5:01 pm
hi. i have the following code:
everything is fine but i need to use $idarray in the final while loop. how can i set it so that it has global scope? i have tried putting the word global infront of it when declaring but get the following error:
on the line where it is declared. can anyone help?
thanks
Code: Select all
if(isset($_POST['ingredients_added']))
{
//adds list of ingredients to a variable
$allingredients = $_POST['ingredients_added'];
//explode adds a new element to the array every time \n is found
$ingredientsarray = explode("\n", $allingredients );
//function to strip \n off the end of each line
function trim_value(&$value)
{
$value = "'" . trim($value) . "'";
}
//apply trim_value function to every element in array
array_walk($ingredientsarray, 'trim_value');
//by this point the array has each line in an element in the correct format but has an unwanted element at the end
//gets rid of the unwanted empty element
$deletedelement = array_pop($ingredientsarray);
//NEEDS TO BE GLOBAL!
$idarray = array();
$index = 0;
$getids = @mysql_query("SELECT ingid FROM ingredients WHERE name IN (". implode(",",$ingredientsarray) . ")");
if (!$getids)
{
echo 'fail ' . mysql_error() . ' ';
}
while ($row = mysql_fetch_assoc($getids))
{
$value = $row['ingid'];
$idarray[$index] = $value;
$index++;
}
}
if(isset($_POST['newdishname']))
{
$newdishname = $_POST['newdishname'];
$newvegetarian = $_POST['newvegetarian'];
$newmethod = $_POST['newmethod'];
$newpreptime = $_POST['newpreptime'];
$newcooktime = $_POST['newcooktime'];
$newtype = $_POST['newtype'];
$insertcommand = @mysql_query("INSERT INTO dishes SET
name ='$newdishname',
vegetarian ='$newvegetarian',
method ='$newmethod',
preptime ='$newpreptime',
type = '$newtype',
cooktime ='$newcooktime'");
if(!$insertcommand)
{
echo'<p>fail ' . mysql_error() . '</p>';
}
else
{
echo"<p>$newdishname added to database!</p>";
}
//gets maxdishid
$getmaxdishid = @mysql_query("SELECT max(dishid) FROM dishes");
if(!$getmaxdishid)
{
echo ' MAX DISH ID FAIL ' . mysql_error() . ' ';
}
else
{
echo"max dish ok";
}
while ($row = mysql_fetch_assoc($getmaxdishid))
{
$maxidno = $row['max(dishid)'];
echo "max dish id is: $maxidno ";
//put this back outside
}
$iterationnumber = 0;
$noofingsindish = count($idarray);
echo "number of ingredients added: $noofingsindish";
$thisdish = $maxidno +1;
echo "this dishes id no: $thisdish";
//do for each ingredient
while ($iterationnumber < $noofingsindish)
{
echo "we got here ok!";
$thisid = $idarray['iterationnumber'];
echo "ingredient id : $thisid";
$insertingredients = @mysql_query("INSERT INTO recipes SET
dishid = '$thisdish',
ingid = '$thisid',
quantity =1");
$iterationnumber++;
}
if(!insertingredients)
{
echo ' ADD DISH TO DATABASE FAIL ' . mysql_error() . ' ';
}
}Code: Select all
Parse error: syntax error, unexpected '=', expecting ',' or ';'thanks