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:
The problem that I am having is with this snippet of code:
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
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