Here's what I've got....
The goal of this code is to compare the result-sets of 2 queries after they're put into arrays, with the final result being that the value selected in the drop-down box will be the value that is present in both the ingredients table ( a look-up table ) AND the ingredient in the recipe.
This is for an update utility and I wish to have the current recipe ingredient pre-selected from a list of all available ingredients.
Presently the same ingredient is listed for each invocation of <?php preSelectIngredients() ?>
I'd REALLY appreciate some input here,
Thanks,
Pete
PHP:
<?php
function preSelectIngredients( $id ){
new MyConnect();
$theCode = $id;//"id3cc575193";//will be $_POST['recCode']
$sql = "SELECT rec_ingredient FROM recipe_ingredient_data WHERE rec_code ='" . $theCode . "'";
$rs = mysql_query($sql) or die(mysql_error());
$sql2 = "SELECT ingredient, gross_cost FROM ingredient";
$rss = mysql_query( $sql2 ) or die( mysql_error());
//declare global arrays
$GLOBALS["ALL"] = array();
$GLOBALS["REC"] = array();
//declare counter variable
$counterVal = 0;
//place this recipes' ingredients into an array with GLOBAL SCOPE
while($rec_ing = mysql_fetch_array( $rs ))
{
$REC[$counterVal] = $rec_ing;
$counterVal++;
}
//re-set counter variable
$counterVal = 0;
//place ALL ingredients into an array with GLOBAL SCOPE
while($all_ing = mysql_fetch_array( $rss)) {
$ALL[$counterVal] = $all_ing;
$counterVal++;
}
//determine the number of elements in each array
$numIngreds = count($ALL);
$numRecIngreds = count($REC);
$rCode = array_intersect($REC, $ALL);
//print out the values from the intersection
print_r( $rCode);
print_r($ALL);
//access a specififc element of the intersection
//echo("<BR><BR>Element at rCode[5][0] == " .$rCode[5][0]);
//spit out some test data
//echo("<BR><BR>Rec Array Sample: " . $REC[4][0] . "\n");
//echo("<BR>All ingredients array sample: " . $ALL[0][1] . "<BR><BR>\n");
?>
<!-- //HTML Comment
Uncomment this for diagnostics
Number of ingredients is: <?= $numIngreds ."<BR>\n"?>
Number of Recipe Ingredients is: <?= $numRecIngreds ."<BR>\n"?>
//-->
<?php
for($i=0; $i < $numIngreds; $i++)
{
if($rCode[$i]['rec_ingredient'][0] == $ALL[$i]['ingredient'][0]){
?>
<OPTION name="selUp[]" value="<?php echo($ALL[$i][0] . ":" . $ALL[$i][1]) ?>" SELECTED><?php echo($ALL[$i][0])?></OPTION>
<?
}else{
?><OPTION name="selUp" value="<?php echo($ALL[$i][1] . ":" . $ALL[$i][0]) ?>" ><?php echo($ALL[$i][0])?></OPTION>
<?
}
}
}
?>
//--------------end function definition--------------
//-----------------Updateable form------------------
<?php
/*
Created on:
Author:
Summary:
Classes;
Functions;
*/
$recCode = "id3cc575193";//$_POST['recCode'];
include("../../include/phpClasses/MyConnect.php");
include("../../include/functions/functions.inc");
include("../../include/functions/preSelectIngredients.inc");
//******************************************************
//DE-BUGGING CODE..uncomment for diagnostics
//...Get the posted variables
$INPUT_VARS = &${'HTTP' . $REQUEST_METHOD . 'VARS'};
//..output the variables for debug purposes
print_r($INPUT_VARS);
//get the count of the variables
$num = count($INPUT_VARS['tfHidden']);
echo($num);
//***********************************************************
$bgColor = "#F8CFA3";
$tc = new MyConnect();
$c = 0;
$sqlRecs = "SELECT rec_code, item_name FROM menu_item";
$recs = mysql_query($sqlRecs);
$sql1 = "SELECT rec_ing_id FROM recipe_ingredient_data WHERE rec_code = '" . $recCode ."'";
$rss = mysql_query( $sql1 );
$sql = "SELECT rec_ingredient, rec_unit, rec_portion, rec_portion_as from recipe_ingredient_data WHERE rec_code = '" . $recCode . "'";
$rs = mysql_query( $sql );
$ct = mysql_num_rows($rss);
?>
<!-- BEGIN MAIN TABLE-->
<TABLE align="center" bgcolor="#F8CFA3" name="tabMain" id="tabMain" border="2" height="50%">
<TH bgcolor="#8D85FA"colspan="2"><font color="White">Update Recipe Utility</font>
<TR width="100%">
<TD>
<TABLE name="tabList" id="tabList"><!-- BEGIN LEFT TABLE-->
<?php
while($recRow = mysql_fetch_row( $recs ))
{
print("<TR bgcolor=" . $bgColor . ">");
print("<TD>\n");
printf("<a href=http://" . $recRow[0] . ">" . $recRow[1] . "</a>\n");
print("</TD>");
print("</TR>");
}
?>
</TABLE><!-- CLOSE LEFT TABLE-->
</TD><!-- close left data cell-->
<TD><!-- open right data cell-->
<!--BEGIN RIGHT TABLE-->
<TABLE name="tabRight" id="tabRight" align="center" width="200px">
<TR>
<TD><!--begin right data cell to hold update form-->
<FORM METHOD="POST" ACTION="catch.php">
<?php
for($c = 0; $c < $ct; $c++ )
{
$p = mysql_fetch_row( $rss );
$x = mysql_fetch_row( $rs );
?>
<TR><TD>
<INPUT TYPE="hidden" name="tfHidden[]" value="<?php echo $p[0] ?>"><!--hidden field for row by row update-->
<SELECT NAME="selUpdate[]"><?php preSelectIngredients($recCode); ?></SELECT>
<INPUT TYPE="text" NAME="update[]" size="5" value="<?php echo $x[1] ?>">
<INPUT TYPE="text" NAME="update[]" size="5" value="<?php echo $x[2] ?>">
<INPUT TYPE="text" NAME="update[]" size="5" value="<?php echo $x[3] ?>"><BR>
</TD>
</TR>
<?
echo("\n");
}
?>
</TABLE>
<INPUT TYPE="submit" id="submit" value="Save Changes">
<INPUT TYPE="reset">
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>