Home > database >  Repeat category name - PHP SQL
Repeat category name - PHP SQL

Time:09-07

I created a database with 5 categories and created a page to display the available categories that I pull from the database. The problem is that I have 4 divs that have the following class ( col-md-3 ) and one that is ( col-md-6 ).

Code for gathering the first 5 categories ( col-md-3 ):

<?php 
    $this->db->limit(4); 
    $categories = $this->db->get_where('category', array('parent' => 0))->result_array(); 
    foreach ($categories as $key => $category):
?>
    <div >
        <div >
            <div >
                <img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
                <div >                        
                    <span>2</span>Locations
                </div>
                <div >
                    <h3><a href="<?php echo site_url('home/filter_listings?category='.slugify($category['name']).'&&amenity=&&video=0&&status=all'); ?>"><?php echo $category['name']; ?></a></h3>
                    <p><?php echo $category['name']; ?></p>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>

Code for gathering another 1 category ( col-md-6 ):

<?php 
    $this->db->limit(1); 
    $categories = $this->db->get_where('category', array('parent' => 0))->result_array(); 
    foreach ($categories as $key => $category):
?>
    <div >
        <div >
            <div >
                <img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
                <div >                        
                    <span>2</span>Locations
                </div>
                <div >
                    <h3><a href="<?php echo site_url('home/filter_listings?category='.slugify($category['name']).'&&amenity=&&video=0&&status=all'); ?>"><?php echo $category['name']; ?></a></h3>
                    <p><?php echo $category['name']; ?></p>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>

With this code, I get a display of categories but instead of displaying 5 categories, it repeats one of the displayed 4. How can I fix the code so that the display automatically continues to display the categories in all divs? If I forgot to write something, correct it or ask, I will update the question.

CodePudding user response:

It looks like you are getting the first 4 items for the first foreach loop and then getting the first one again for the second loop. Would it make sense to get all 5 and process them in one single foreach loop only adding 'gallery-item-second' on the 5th item, something like ...

<?php 
    $this->db->limit(5); 
    $categories = $this->db->get_where('category', array('parent' => 0))->result_array(); 
    $index = 0;
    foreach ($categories as $key => $category):
        $index  ;
        if ($index == 5) {
            echo '<div >';
        } else {
           echo '<div >';
        }
?>
        <div >
        . . .
        </div>
    </div>
<?php endforeach; ?>

Or if you must have the second loop then get 5 items and only show the 5th. And to make sure you get the same 5 you could use a sort criteria when you get the items.

CodePudding user response:

The problem is resolved with math:

I have added the next code:

...
$num = 5;
$i = 0;

foreach ($categories as $key => $category):
    if ($i < ($num - 1)) {
        // show div col-md-3
    } else {
        // show div col-md-6
    }
endif;
  • Related