CodeIgniter Forums
how i can to show specific record from a table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: how i can to show specific record from a table (/showthread.php?tid=72061)



how i can to show specific record from a table - king_ahwaz - 10-31-2018

hi guys
how are you?
i have a question about, how can i show for example last 5 photo added in specific category

sample:
PHP Code:
<?php foreach ($projects as $item): ?>
                        <li>
                            <a href="<?php echo $item->image ?>">
                                <div class="gallery-item"><img src="<?php echo $item->image ?>" alt="<?php echo $item->title ?>"></div>
                            </a>
                        </li>
            <?php endforeach ?>

this code show all records/photos on the projects, i just want to show specific category.

thanks


RE: how i can to show specific record from a table - php_rocs - 10-31-2018

@king_ahwaz,

This question has many answers depending on how you want it to work. Here are a few answers...
- You could have the query to only pull the last 5 photos from a specific category.
- You could still pull all the photos and have a filter which would allow the user to select specific photos by category

Your thoughts...


RE: how i can to show specific record from a table - king_ahwaz - 10-31-2018

could you please show my an example, i'm not an expert.

thanks


RE: how i can to show specific record from a table - dave friend - 11-01-2018

(10-31-2018, 06:31 PM)king_ahwaz Wrote: could you please show my an example, i'm not an expert.

thanks

What needs to be done depends completely on how the table(s) are structured. So it's really hard to provide a complete and accurate example.

That being the case, I'll make some stuff up. The table "projects" has the following fields

"projID"  an auto incrementing integer and primary key for the table
"catID"  an integer representing various categories
"image" the actual image data
"title" a string

A model function could look something like this

PHP Code:
public function get_last_five($category)
{
 
   $this->db
            
->select('image, title')
 
           ->order_by('projID''DESC');  //reverse the natural sort order

 
   // get five rows starting at the first row
 
   $query $this->db->get_where('projects', array('catID' => $category), 50);
 
    // make sure that get_where worked - it will be false if it failed
 
   if($query !== FALSE)
 
   {
 
      return $query->result();  //returns an array of "row" objects
 
   }


The Query Builder documentation is HERE
and everything you need to know about generating query results is HERE.