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!
Please refer to page 2 for trying to display products that have a table row that contains a array based on one single value in the array This is still in the works
Was wondering if I could get some help on some code that I am having trouble with.
What I have below is a multiple select box that pulls categories from a product_categories database and is suppose to automatically select the categories that are in a array based on a array that is in a products database
echo("<select name=topcategory[] MULTIPLE SIZE=5>
");
$res = mysql_query("select name,id from products_categories where status='enabled' order by name asc");
for ($i = 0; $i < mysql_num_rows($res); $i++) {
$name= mysql_result($res, $i, "name"); /// A list of all the categories from products_categories
$top_category = explode(',', $top_category); //// This is the array that is in the products table which is pulls categories to match up to
//// the above query Category1, Category2
foreach ($top_category as $value) { //// Dont know how to construct from here since array is exploded
if($value==$name) {$top_category_selected="selected";} else{$top_category_selected="";} /// This is a if statement selects if exists
echo("<option value='$name' $top_category_selected>$name</option>"); // This is the multiple option select box
}
}
Last edited by devain on Mon Oct 29, 2007 5:36 pm, edited 7 times in total.
When I try to compare and highlight categories with the array that is contained in the $top_category array which is pulled from the products database which is at the end of the code nothing is highlighted the array contains Category2 and Category3
when the script is complete it duplicates the first category so I get
Category1
Category1
Category2
Category3
Category4
Category5
Category6
Category7
Category8
$all_categories = array('cat 1','cat 2','cat 3'); // or wherever you get it from
$selected_categories = array();
// get the DB results
$result = mysql_query("select name from ......");
// build an array of those results
while($row = MySQL_fetch_assoc($result)){
$selected_categories[] = $row['name'];
}
echo '<select name="topcategory[]" multiple="true" size="5">';
// build your options
foreach($all_categories as $category){
echo '<option value="'.$category.'"';
if(in_array($category,$selected_categories)){
echo ' selected="selected"';
}
echo '>'.$category.'</option>';
}
echo '</select>';
The easiest way to do something similar to this, but instead of passing in a string for $chosen1, you pass in the array of values which were selected. You'll also need to use in_array() I suspect.
Still having problems here is the current code that I have I may be making a mistake somewhere also I am posting the link to a test page I appericate all help on this what is currently happening is that it displays all the top_categories from the top_sunglasses_categories tables in the select box and seems to insert this into the select box correctly but when it comes to do the compare and suppose to highlight the values that are being pulled from the sunglasses table no results
include($DOCUMENT_ROOT . '/connect.php');
// This selects the all the available categories which is in the table products_categories
echo("<form>");
// This is pulling a list of all categories and creating the array this is displaying correctly in the select box as the test page shows
$result = mysql_query("select * from products_categories order by name asc"); // This is pulling a list of all categories and creating the array
// build an array of those results
while($row = MySQL_fetch_assoc($result)){
$all_categories[] = $row['name'];
echo "All Categories<br>".$all_categories."<br>";
}
//$all_categories = array('cat 1','cat 2','cat 3','cat 4'); // or wherever you get it from
//$selected_categories = array();
// get the DB results from the sunglasses which is the products table
This is suppose to pull the two categories that should be highlighted the two items in the array is cat1,cat2
$result = mysql_query("select * from products where id='41708'");
// build an array of those results
while($row = MySQL_fetch_assoc($result)){
$selected_categories[] = $row['categories'];
echo "Selected Categories<br>".$selected_categories."<br>";
}
echo '<select name="category[]" multiple="true" size="5">';
// build your options
foreach($all_categories as $category){
echo '<option value="'.$category.'"';
/// According to the if statement this is suppose to compare the two arrays check to see if cat1,cat2 exists and highlight accordinly
if(in_array($category,$selected_categories)){
echo ' selected="selected"';
}
echo '>'.$category.'</option>';
}
echo("</select></form>");
Last edited by devain on Sun Oct 28, 2007 5:37 am, edited 2 times in total.
I am not trying to print the array it is just to show results that the array is created Please read the post and goto the test page that I have created for this sepcific query I have notes on the code
I have modified the code as follows to print the array to help resolve the code the problem I have printed the arrays as suggested Thanks for the suggestion I can now see all arrays for the categories and array for the products
include($DOCUMENT_ROOT . '/connect.php');
// This selects the all the available categories which is in the table products_categories
echo("<form>");
$result = mysql_query("select * from product_categories order by name asc");
// build an array of those results
while($row = MySQL_fetch_assoc($result)){
$all_categories[] = $row['name'];
print_r($all_categories);
echo("<br>Array For All Categories<br>");
}
//$all_categories = array('cat 1','cat 2','cat 3','cat 4'); // or wherever you get it from
//$selected_categories = array();
// get the DB results from the products which is the products table
$result = mysql_query("select * from products where id='41708'");
// build an array of those results
while($row = MySQL_fetch_assoc($result)){
$selected_categories[] = $row['categories'];
echo("<br><br>Array for Selected Categories These are suppose to be highlighted in the select box");
print_r($selected_categories);
}
echo '<select name="category[]" multiple="true" size="5">';
// build your options
foreach($all_categories as $category){
echo("<br><br>");
echo '<option value="'.$category.'"';
if(in_array($category,$selected_categories)){
echo ' selected="selected"';
}
echo '>'.$category.'</option>';
}
echo("</select></form>");
Last edited by devain on Sun Oct 28, 2007 5:41 am, edited 2 times in total.
Fantastic How on earth did you get this?? I need to read some more or something. difficult query It has seem to work I will leave this page up for a few days due to the fact that it works and I am sure that there are some people that will need this code to help them. I do appericate all the replies and help. If I feel and see with the server logs that people are abusing it then I will take the test page down but until then it will stay.