Amir
Amir

Reputation: 181

Trouble adding certain number of rows with Codeigniter

I have a script running through Codeigniter that pulls up a list of images pertaining to a certain product from the database. The max number of images per product is 10 but that is not always what is in the database. Thus, I am trying to pull from the database and complete the list of images even if it is less than 10. For instance,

I have Image 1 Image 2 Image 3 Image 4

from the database. On the view, I am trying to still display the final six images (with blank data of course) so on the page itself it still shows the containers.

This is the code I have compiled so far from other advice and suggestions.

EDIT: My question essentially is how can I complete all 10 spots for images even if what I pull from the database is less than that as referenced in the example above?

From product model...

public function getImages($id) {
    //run query
    $this->db->from('images_main');
    $this->db->where('images_main.product_id',$id);
    $this->db->limit(10);
    $this->db->order_by('img_order', 'asc');
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {
        return $query->result_array();

    }else{
        $this->session->set_flashdata( 'message', 'The query was not successful.' );
    }           
}   

From view...

<ul id="sortable">
<?php 
$count = count($img);
$max = 10;
$total = $max - $count;

for($t=1; $t<=$total; $t++):
    $img[] = $t;
endfor;
print_r($img);

for ($i=1; $i<=10; $i++):
?> 
    <li class="img_list">       
    <div class="photo_content">
    <div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/><a href="#" class="delete" id="<?php echo $img[$i]['img_id']; ?>"></a><a href="#" class="zoom" id="<?php echo $img[$i]['img_id']; ?>"></a></div>
    <div class="uploads"><input  type="text" name="photo_order"  size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
    <div class="browse"></div>
    </div>
    </li>       
<?php endfor; ?>
</ul>

Upvotes: 0

Views: 53

Answers (1)

LBridge
LBridge

Reputation: 2135

As I understand your code, the variable $count contains the number of entries from the database.

So instead of having a for loop to 10, set it to $count (or use a foreach on $img).

Your $total variable is not the total but how many blank images there are to fill in. Have another loop for that.

Put both loops inside the <li class="img_list">...<li> tag.

<?php

$count = count($img);
$total = 10;
$difference = $total - $count;
?>    

<ul id="sortable">
<?php
// Images from the database
for ($i = 0; $i <= $count - 1; $i++):
?> 
    <li class="img_list">      
    <div class="photo_content">
    <div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/><a href="#" class="delete" id="<?php echo $img[$i]['img_id']; ?>"></a><a href="#" class="zoom" id="<?php echo $img[$i]['img_id']; ?>"></a></div>
    <div class="uploads"><input  type="text" name="photo_order"  size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
    <div class="browse"></div>
    </div>
    </li>       
<?php endfor; ?>

<?php
// Blank images
for ($i = 0; $i <= $difference - 1; $i++):
?>  
    <li class="img_list">   
    <div class="photo_content">
    <div class="thumbnail">
        <img src=".../path/to/blank_photo.png" alt="Title">
    </div>
    </li> 
    <!-- rest of HTML code -->   
<?php endfor; ?>
</ul>

Hope this helps.

Notes:

  • IDs always start by 0, you should therefore start your loop at 0 and not 1
  • <img> tags must have an alt property

Upvotes: 1

Related Questions