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:

Post by devain »

Here is the current full script since I have modified the test page to make sure that other vairables and stuff is not shown Thanks for all the help from everyone that has posted to this. below is the script and also the link to the test page. I will be logging who goes to this page to track abuse Hopefully this does not happen.

http://72.29.78.150/~restore/test.php

Code: Select all

include($DOCUMENT_ROOT . '/connect.php');

$all_categories = array(); 
$selected_categories = array();

// This selects the all the available categories which is in the table product_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 = array_merge($selected_categories,explode(',',$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>");
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

There may be more to this post As am am attempting to display products based upon the array in the products_categories table which is a list of all the categories and displaying the products for each category based on the array from the products table. Even if I dont need help I will place the results of that query here. I appericate the help from feyd and Kieran Huggins and Jcart very smart people.



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

Help displaying products if value exists in array

Post by devain »

This is really my first time working with arrays and I cannot seem to get things right there are alot of factors that seem to be involved
I know I am missing something somewhere here is the link to the page as you can see I am not getting the results that I need

Products table contains a array categories(cat1,cat2,cat3)

What I am wanting to do is display the results by searching the array if the $category listed below is
in the array display that product

Products Table is as follows
id product_name categories
41708 TEST cat1,cat2,cat3,cat4


Here is the test page which is not displaying the results


http://72.29.78.150/~restore/test1.php

Code: Select all

$category="cat2";   


$query  = "select product_name,id from products where categories='$category'";
$result = mysql_query($query);



while(list($product_name,$id)= mysql_fetch_assoc($result))
{
	
    echo "<br>Id :$id <br>" .
         "Product Name : $product_name <br>" .
         "Message : $row <br><br>";
}
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

Ok I have come up with something that works if there is a better way of doing it please help with the query. Also I am trying to use the list function to create the variables instead of specifying the row here is the current code and link any help in making it better ( efficient ) would appericate the help


Test Link is as follows

http://72.29.78.150/~restore/test3.php


Dont know if this a efficient way of doing it but it seem to work with the exception of the list statement.

Code: Select all

$category='cat1';
// still not sure why you have to have this before the query I am assuming that this tells php that its going to be an array and is blank until query

$cat_categories = array(); 

//  still not sure why this has to be here before the query I am assuming that this tells php that its going to be an array and is blank until query


$all_categories = array(); 

///             ----------------------------------------------


// Query the database which is selecting all entries in the products tabls


$query = "SELECT product_name,id,categories from products limit 5"; 
	 


$result = mysql_query($query) or die(mysql_error());

while($row = MySQL_fetch_assoc($result)){

// read this still not really sure why I need the array 
//merge especially since I am only dealing with one table that contains the array cat1,cat2,cat3


	 
$categories = array_merge($cat_categories,explode(',',$row['categories']));  	

       			
   if (in_array("cat1", $categories,0)) {



	 echo("<br><br>TRUE<br>  If $ category='cat1' is in array this is where I want to create vairables for the fields product_name and id to display each product meaning more then one to user<br><br>");	

	 echo "<br>Product ID:<br>" . $row['id'] . "<br>";
     
	 echo("<br>");
     
	 echo "Product Name:<br>" . $row['product_name'] . "<br>";


// I have tried $result here but nothing also

list($product_name, $id) = $row;  


// Variables  I want to use using the list function above but not working

echo("<br><br> Trying to use the below Variables which are $ product_name $ ID using the list function but does not seem to be working");
	 																
	echo("<br><br>Product Name:<br>$product_name<br><br>Product_Id:$id<br>");
	 
///  Else statement if no results exist
	
}else{
	 echo("<br>-------------------------------------------------------------------------------");
	 echo("<br><br>false<br><br>  This is displaying as the else statement if it does not exits");
	
}     
}
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

should I delete the above request for help and make a new post? Was trying to keep it all together since it relates
Last edited by devain on Mon Oct 29, 2007 7:32 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

devain wrote:should I delete the above request for help and make a new post? Was trying to keep is all together since it relates
No.
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Post by devain »

Cool, Any comments on the code above on how to improve it or change it to make it more efficent? I have comments on the code and also the test page as the results display



Test page is http://72.29.78.150/~restore/test3.php


This page displays the current results of the current script
Post Reply