making my array public

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

making my array public

Post by slash_gnr3k »

hi. i have the following code:

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() . ' ';
					}
					

				}
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:

Code: Select all

Parse error: syntax error, unexpected '=', expecting ',' or ';'
on the line where it is declared. can anyone help?
thanks
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

... It is global to the code you posted.

You have a typo in the very last if statement...
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

ok i see it. sorted now.
thanks!
Post Reply