Welcome Guest, Not a member yet? Register   Sign In
how to get limited number of records and order by rand ?


I am 100% new to CI 4 and fairly new to MVC.

I did the Build Your First Application and from that I am trying to make my project.

I created a homepage in which I have to load banners and using the example code from news tutorial I got all the banners but I want to get them randomly and also I want to limit the number of banners I get. Right now I am getting all findAll(); so according to this my question is:

1. How to get 5 banners randomly
2. How / Where (in which folder) to add custom js / css and other files ?


1)you might be able to do something like lever use of

and use codeigniter built in models utility :
public function getOne($id)
      return $this->find($id);

you would have to do count but id might not start with min nor end with max.

2)this is how i set out my assetts:

├── blogImages //dir
├── captcha   //dir
├── css       //dir
├── errors
├── favicon.ico
├── fonts  //dir
├── images   //dir
├── index.php
├── js    //dir
├── minCss   //dir
├── portfolio //dir
├── robots.txt
└── sounds    //dir

eg in js I have

│   ├── bk
│   ├── bootstrap.bundle.js
│   ├── jquery-3.5.1.min.js
│   ├── jquery-migrate-3.3.0.js
│   ├── jquery-migrate.js
│   └── popper.js

One more question would be how to use composer to install other 3rd party resources which I have to use???

(This post was last modified: 11-07-2020, 05:57 AM by captain-sensible.)

ok there is a handy site for software that you can get via composer : https://packagist.org/

lets say you want to allow people to download the text version of a blog you have. So i enter html2pdf in search box of packagist : Top of the list i see :


i left click on it and then see:

composer require spipu/html2pdf

thats tells me that if i have composer set up correctly , i open a terminal and cd to root directory of web dev

and run:

composer require spipu/html2pdf
//or maybe php composer.phar require spipu/html2pdf

i then see in vendor directory: spipu

and also notice a chnage in composer.json

"spipu/html2pdf": "^5.2",
//above has been added

some software obtained via composer have deps , and to make sure you have them, just run after running
composer require spipu/html2pdf

composer install

A few things to note here. If your using appstarter, vendor directory is where stuff goes and thus is critical to keep. System directory if its there is more for ref as far as i can see.

Once you have software , its now easy when you need to update.

For instance ig a new version of codeigniter appears you can run

composer update codeigniter4/framework

to avoid getting dev you can use --no-dev

oh one more thing, composer has autoload so it knows where the software is :

to use for example spipu in a controller i found all i had to do was put at top of controller:

use Spipu\Html2Pdf\Html2Pdf;
and then in method:

$html2pdf = new Html2Pdf();
$html2pdf->output($mytitle.'.pdf', 'D');

if you manually set something up not using composer then you have to use the autoloader of CI4 which is : app/Config/Autoload.php

if it has a namespace use $psr4 = [


if not there is a class map section below that .That should be enough to get you going ?

Theme © iAndrew 2016 - Forum software by © MyBB