Jump to content

[SOLVED] Help array,displaying products


devain

Recommended Posts

I am new to working with arrays and it took me alot of hours to come up with the following Was Wondering if I could get some input on some code to see if I am getting it right. Dont know if this a efficient way of doing it but it seem to work sorta with the exception of the list statement. I am hoping I am posting this in the right section.

 

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/test3.php

 

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

 

 

Thanks any help would be appericated

 

Link to comment
Share on other sites

try

$category='cat1';
$query = "SELECT product_name,id,categories from products WHERE FIND_IN_SET('$category', categories)";
$result = mysql_query($query) or die(mysql_error());
while($row = MySQL_fetch_assoc($result)){
   echo imlope(' - ', $row), '<br />';
}

 

 

Link to comment
Share on other sites

I recommend you read up on "normalization"

http://www.phpfreaks.com/forums/index.php/topic,126097.0.html

 

 

Normalized, the data becomes

[pre]

category        productcat        product

---------        ---------          --------

cat_id  --+      id          +---  prod_id

catname  +--    cat_id      |    prod_descrip

                prod_id  ---+

[/pre]

 

Then

    
<?php

$sql = "SELECT c.catname, p.prod_descrip
        FROM category c
        INNER JOIN productcat pc  c.cat_id = pc.cat_id
        INNER JOIN product p ON pc.prod_id = p.prod_id
        WHERE c.cat_id = '$cat'";     
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
while (list($cat, $prod) = mysql_fetch_row($res))
{
    echo "$cat : $prod <br />";
}        
?>

 

Job done.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.