Jump to content

Extracting certain categories from database


jack28

Recommended Posts

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   

 

Link to comment
Share on other sites

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>
  }
}
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.