Help array, vairable based select box,displaying products

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!

Moderator: General Moderators

devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Help array, vairable based select box,displaying products

Post 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
}
}
Last edited by devain on Mon Oct 29, 2007 5:36 pm, edited 7 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's your trouble?
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

I am not able to get it do automatically select or highlight the values from the array in the select box
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What are the results of the query in the snippet?
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

wonder if you can post a example with the code that I provided above
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

will try to above and see if i can get it to work thanks for the fruity statement kinda funny
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

devain wrote:wonder if you can post a example with the code that I provided above
Try yourself first. :wink:
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post 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>");
Last edited by devain on Sun Oct 28, 2007 5:37 am, edited 2 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

when you want to echo an array, use print_r($array)
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post 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>");
Last edited by devain on Sun Oct 28, 2007 5:41 am, edited 2 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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']));
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post 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.
Post Reply