Page 1 of 2

Help array, vairable based select box,displaying products

Posted: Sat Oct 27, 2007 5:45 pm
by devain
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

Code: Select all

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
}
}

Posted: Sat Oct 27, 2007 5:52 pm
by feyd
What's your trouble?

Posted: Sat Oct 27, 2007 5:56 pm
by devain
I am not able to get it do automatically select or highlight the values from the array in the select box

Posted: Sat Oct 27, 2007 6:04 pm
by feyd
What are the results of the query in the snippet?

Posted: Sat Oct 27, 2007 6:12 pm
by devain
The results I get are as follows


Select Box Comes up with the $name vairable which is a complete list of the categories from the top_Products_categories

Category1
Category2
Category3
Category4
Category5
Category6
Category7
Category8

Which show up fine in the select box

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

what I want it do display is this

Category1
Category2 Highlighted category
Category3 Gighlighted category
Category4
Category5
Category6
Category7
Category8

Posted: Sat Oct 27, 2007 6:34 pm
by Kieran Huggins
Your loops are fruity.

Try:

Code: Select all

$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>';

Posted: Sat Oct 27, 2007 6:34 pm
by feyd
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.

Posted: Sat Oct 27, 2007 6:38 pm
by devain
wonder if you can post a example with the code that I provided above

Posted: Sat Oct 27, 2007 6:40 pm
by devain
will try to above and see if i can get it to work thanks for the fruity statement kinda funny

Posted: Sat Oct 27, 2007 6:40 pm
by John Cartwright
devain wrote:wonder if you can post a example with the code that I provided above
Try yourself first. :wink:

Posted: Sun Oct 28, 2007 2:18 am
by devain
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


Link to test page : http://72.29.78.150/~restore/test.php


Here is my current code

Code: Select all

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>");

Posted: Sun Oct 28, 2007 3:48 am
by Kieran Huggins
when you want to echo an array, use print_r($array)

Posted: Sun Oct 28, 2007 3:57 am
by devain
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



Here is the new code

Code: Select all

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>");

Posted: Sun Oct 28, 2007 4:29 am
by Kieran Huggins
at the top of your script, add:

Code: Select all

$all_categories = array();
$selected_categories = array();
to create the arrays.

Also, change:

Code: Select all

$selected_categories[] = $row['top_category'];
to:

Code: Select all

$selected_categories = array_merge($selected_categories,explode(',',$row['top_category']));

Posted: Sun Oct 28, 2007 4:38 am
by devain
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.