Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associative Array Question

Status
Not open for further replies.

PaulinKC

Programmer
Apr 2, 2002
50
0
0
US
I have a PHP Script that parses input from the user, then it selects a table from a database and loads the results into a memory for faster searching...

Below is my code:

Code:
$ingredientID = array();

		if (!(empty($newQty)) || !(empty($newName))) {

			include_once("dbconn.inc");
			global $$link;
			$db_selected = mysql_select_db('recipes', $$link);
			$query = "SELECT * FROM tblMasterIngredient";
			$getmasterlist = mysql_query($query, $$link) or die ('Error in tblMasterIngredients SELECT statement: ' . mysql_error());

			while($rowid = mysql_fetch_assoc($getmasterlist)) {

				$masterID = $rowid["ID"];
				$masterIngredientName[] = $rowid["MasterIngredientName"];
				$ingredientID["$name"] = $masterID;
				
			}


			if (!empty($masterIngredientName)) {

				for ($z = 0; $z < sizeof($newQty); $z++) {

					$ingredientName = $newName[$z];
					//$currentID = getArrayID($masterIngredientName[$z], "tblMasterIngredient", "MasterIngredientName");
					
					

					if (in_array_recursive($ingredientName, $masterIngredientName)) {
						$currentID = $ingredientID[$ingredientName];
						print_r($currentID);
						echo "<br>look above<br>";
						echo "The Current ID is: " . $currentID . "<br>";					
						echo "This ingredient: " . $ingredientName . " was found in the array.<br>";
						$db_selected = mysql_select_db('recipes', $$link);
						$add_to_lookup = "INSERT INTO tblIngredients (RecipeID, MasterID, IngredientAmount) VALUES ('$last_id','$currentID', '$newQty[$z]')";
						echo "The add_to_lookup query is: " . $add_to_lookup . "<br>";
						$querydb = mysql_query($add_to_lookup, $$link) or die ('Error in tblIngredients Insert Query: ' . mysql_error());

					} else {
						echo "This ingredient: " . $ingredientName . " was NOT found in the array.<br>";
						$add_to_master = "INSERT INTO tblMasterIngredient VALUES ('', '$newName[$z]')";
						echo "The add_to_master query is: " . $add_to_master . "<br>";						
						$querydb2 = mysql_query($add_to_master, $$link) or die ('Error in tblMasterIngredient Insert Query: ' . mysql_error());
						$master_last_id = mysql_insert_id();

						$add_to_lookup2 = "INSERT INTO tblIngredients (RecipeID, MasterID, IngredientAmount) VALUES ('$last_id','$master_last_id', '$newQty[$z]')";
						echo "The add_to_lookup2 query is: " . $add_to_lookup2 . "<br>";
						$querydb3 = mysql_query($add_to_lookup2, $$link) or die ('Error in tblCategories Insert Query: ' . mysql_error());

					}

				}

			} else {

				/*************************************************/
				/* This section is designed only to run IF and   */
				/* ONLY IF the $masterIngredientName is EMPTY.   */
				/*************************************************/

				for ($a = 0; $a < sizeof($newQty); $a++) {
					echo "The Array - MasterIngredient is Empty<br>";
					
					$add_to_master = "INSERT INTO tblMasterIngredient VALUES ('', '$newName[$a]')";
					echo "The add_to_master query is: " . $add_to_master . "<br>";
					$querydb4 = mysql_query($add_to_master, $$link) or die ('Error in tblMasterIngredient Insert Query: ' . mysql_error());

					$master_last_id = mysql_insert_id();

					$add_to_lookup3 = "INSERT INTO tblIngredients (RecipeID, MasterID, IngredientAmount) VALUES ('$last_id','$master_last_id', '$newQty[$a]')";
					echo "The add_to_lookup3 query is: " . $add_to_lookup3 . "<br>";
					$querydb5 = mysql_query($add_to_lookup3, $$link) or die ('Error in tblIngredients Insert Query: ' . mysql_error());



				}

			}

		}

The problem that I am having is with this snippet of code:

Code:
$ingredientID["$name"] = $masterID;

I don't think this is the correct way to create an associative array in PHP. What I want to be able to do is for each ingredient that the user enters, I want that ingredient name to be checked against the array to see if the ingredient has an index and if so to return the ID (which is an auto incremented FIELD in the table) so that the entered data can be inserted into the table.

Any helpful insight would be greatly appreciated.

Thanks,

Paul
 

$ingredientID["$name"] = $masterID; would work, but you'd want to have already initialized $name.

The code you've shown would throw a PHP Notice Error
(Notice: Undefined variable: name...) and the array row
$ingredientID["name"] would translate into: $ingredientID[""] == the value of $masterID.



This is probably what you want:
$ingredientID["name"] = $masterID;

If $name is an actual defined variable
i.e.
$name = 'John';
then:
$ingredientID["$name"] = $masterID;
would have the array row:
$ingredientID["John"] would be equal to the value of $masterID;
 
In the "while ($rowid = mysql_fetch_array..." loop, you'd also be constantaly overwriting $ingredientID["$name"], since $name isn't being changed within the loop anyway.

Since you're asking about associative arrays, and your example has 2 single-dimensional arrays, I wonder if this is what you're thinking of:

while($rowid = mysql_fetch_assoc($getmasterlist)) {
$masterIngredientName[$rowid['ID']] = $rowid["MasterIngredientName"];
}

Then your second loop could be a bit simpler:
for ($z = 0; $z < sizeof($newQty); $z++) {
$ingredientName = $newName[$z];
if (isset($masterIngredientName[$ingredientName])) {
// Found it
} else {
// ID $newName[$z] not in the $masterIngredientName array
}
}

If I'm understanding your question correctly, that is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top