Forum Moderators: coopster
In order to use the same functions in the script to add, update and remove entries from all three sections, I'm using a multi-dimensional array to store the table name, column names and values. The script then selects the right array of values depending on which site section is being affected and uses those values to build the query string that either sends or removes data to or from the database.
The trouble I'm experiencing is with the un-used array values belonging to the two sections that are NOT being changed. Because the array contains variables (so that any given invocation of the function will plug the current variable values into the query string), I'm getting the following error listed for every variable in all three arrays that is not being used for the current query...
Notice: Undefined variable: whatever in c:\program files\apache group\apache\htdocs\mySite\includes\functions.php on line whatever
The queries are running without a hitch, so it's not interfering with the functionality of the script, but a list of half a dozen error messages every time you do something does not a nice CMS make.
I've tried any number of quoting combinations to no avail. The one that I'm using (below) works, but gives the error messages.
//store needed table names, field names and field values (as variables) to use in building query strings
$values[]=array("table"=>"widget_reviews","field_names"=>"id, meta_keywords, meta_description, name, size, color, text","field_values"=>"'$id', '$meta_keywords', '$meta_description', '$name', '$size', '$color', '$text'");
$values[]=array("table"=>"widget_articles","field_names"=>"id, meta_keywords, meta_description, title, text","field_values"=>"'$id', '$meta_keywords', '$meta_description', '$title','$text'");
$values[]=array("table"=>"widget_resources","field_names"=>"id, href, text, description","field_values"=>"'$id', '$href', '$text', '$description'");//use $type variable to select query values
foreach($values as $var){
if ($var['table']==$type){$query_values = $var;}
}//build and run the query
$conn = content_db_connect();
$sql = "INSERT INTO $query_values['table'] (" . $query_values['field_names'] . ") VALUES(" . $query_values['field_values'] . ")";
$result = mysql_query($sql) or die(mysql_error());
As an example: If $type = 'widget_review', then the script uses the first $values[] array and lists the above error for $title, $text, $href, and $description, (from the second and third arrays) because there are no values assigned to those variables when the script is called.
Can anyone suggest either a different way to code the arrays, or a different way to approach storing the query values and pulling them out for the appropriate query? I feel certain there is some simple approach that I'm just not thinking of.
Thanks in advance for any assitance.
cEM
I think I've come up with a better way for you to assign your arrays.
See the code below:
<?php
$id = 1;
$type = 'widget_reviews';
$meta_keywords = 'sdfsdf';
$meta_description = 'sfsd';
$name = 'sfsd';
$size='sdfs';
$color='sdfs';
$text='sdfs';
//store needed table names, field names and field values (as variables) to use in building query strings
switch ($type) {
case 'widget_reviews':
$query_values = array("table"=>"widget_reviews","field_names"=>"id,
meta_keywords, meta_description, name, size, color,
text","field_values"=>"'$id', '$meta_keywords',
'$meta_description', '$name', '$size', '$color', '$text'");
break;
case 'widget_articles':
$query_values = array("table"=>"widget_articles","field_names"=>"id,
meta_keywords, meta_description, title,
text","field_values"=>"'$id', '$meta_keywords',
'$meta_description', '$title','$text'");
break;
case 'widget_resources':
$query_values = array("table"=>"widget_resources","field_names"=>"id, href,
text, description","field_values"=>"'$id', '$href',
'$text', '$description'");
default:
echo 'Error: invalid type specified!';
break;
}
// use $type variable to select query values
// foreach($values as $var){
// if ($var['table']==$type){$query_values = $var;}
// }//build and run the query
$conn = content_db_connect();
$sql = 'INSERT INTO '.$query_values['table'].'
('.$query_values['field_names'].') VALUES
('.$query_values['field_values'].')';
echo $sql;
$result = mysql_query($sql) or die(mysql_error());
?>
It seems that using a switch to assign only the variables that are needed will work for you in this case.
Tell me if you have and questions or anything and I'll be glad to help. :)
Elijah
if you set $array['hello1']='world1'; and then later try to use $array['hello2'], then that part of the array has not been assigned.
There is 3 ways you can deal with it. First, change a setting in php.ini to simply not post these notices. Second, assign all array elements with a blank string "" before filling in the bits u actually want to use. Or three, use isset() to discover which elements of the array have been set and are available for use.
SN
The simplest solution - setting everything to an empty string before pulling in actual values - has won the day for me. Although it seems a little inelegant, it works, and I understand it, and it took all of three minutes to implement, so it's the answer for me. ;)
Thanks again for the assist.
cEM