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   

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.