devain Posted October 30, 2007 Share Posted October 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75384-solved-help-arraydisplaying-products/ Share on other sites More sharing options...
sasa Posted October 30, 2007 Share Posted October 30, 2007 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/75384-solved-help-arraydisplaying-products/#findComment-381373 Share on other sites More sharing options...
Barand Posted October 30, 2007 Share Posted October 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75384-solved-help-arraydisplaying-products/#findComment-381375 Share on other sites More sharing options...
devain Posted October 30, 2007 Author Share Posted October 30, 2007 thanks I will read the link and try the code I appericate the post and reply will let you know how it goes Quote Link to comment https://forums.phpfreaks.com/topic/75384-solved-help-arraydisplaying-products/#findComment-381409 Share on other sites More sharing options...
devain Posted October 30, 2007 Author Share Posted October 30, 2007 Problem solved and the article on "normalization" is a really good read. I appericate it. Quote Link to comment https://forums.phpfreaks.com/topic/75384-solved-help-arraydisplaying-products/#findComment-381434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.