jack28 Posted April 20, 2015 Share Posted April 20, 2015 Hi there, I'm learning PHP and working on building an ecommerce site using a MVC. I can display all six categories from the database on the index page however I only want to show four of these, as the links at the top will be the six categories. By using the next() method I can display all six categories: <?php while ($categories->next()) { ?> <section class="product"> <a href="products/<?= $categories->getURL() ?>"><h3 class="upper"><?= $categories->getName() ?></h3></a> <br/> <img src="<?= IMAGE_URL . $categories->getImage() ?>" style="width:80%; height:auto;" alt="Menu Tab"/> <h3 class="under"><a href="products/<?= $categories->getURL() ?>">SHOP NOW >></a></h3> </section> <?php } } } ?> I am wondering how to only extract certain products (each has a catID in the database). I thought the switch statement might be the answer and here was my attempt that did not work. I guess I am still not saying which catID to get but unsure how to fix this. Or is there another way not using switch? while ($categories->next()) { switch ($categories->getID()) { case 1: case 2: case 3: case 4: ?> <section class="product"> <a href="products/<?= $categories->getURL()?>"><h3 class="upper"><?=$categories->getName() ?></h3></a> <br/> <img src="<?=IMAGE_URL. $categories->getImage()?>" style="width:80%; height:auto;" alt="Menu Tab"/> <h3 class="under"><a href="products/<?= $categories->getURL()?>">SHOP NOW >></a></h3> </section> <?php break; default: return ""; } } } } ?> Any help would be greatly appreciated! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/295694-extracting-certain-categories-from-database/ Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2015 Share Posted April 20, 2015 Switch is pretty much just another way of doing if / elseif / else, assuming you know the ID's that you don't want to display, a simple single if should do. so you could try something like: while($categories->next()) if(($categories->getID() != <first value you don't want to display>) || ($categories->getID() != <other value you don't want to display>)){ <display categories code> } } Quote Link to comment https://forums.phpfreaks.com/topic/295694-extracting-certain-categories-from-database/#findComment-1509422 Share on other sites More sharing options...
Barand Posted April 20, 2015 Share Posted April 20, 2015 Select the four categories that you want to list in your query. For example SELECT ... WHERE catID IN (1,3,5,6) Quote Link to comment https://forums.phpfreaks.com/topic/295694-extracting-certain-categories-from-database/#findComment-1509430 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.