Selecting Data in Codeigniter

Discussion in 'PHP' started by mohammed ismail, Jul 9, 2015.

?

have these infogrmation enough ?

Poll closed Oct 17, 2015.
  1. yes

    0 vote(s)
    0.0%
  2. no

    0 vote(s)
    0.0%
  1. #1
    Selecting Data :
    1- $this->db->get();
    Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:

    $query = $this->db->get('mytable');
    // Produces: SELECT * FROM mytable

    The second and third parameters enable you to set a limit and offset clause:
    $query = $this->db->get('mytab', 17, 70);

    // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
    You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:

    $query = $this->db->get('mytab');

    foreach ($query->result() as $row)
    {
    echo $row->title;
    }
    Please visit the result functions page for a full discussion regarding result generation.

    $this->db->get_where();
    Identical to the above function except that it permits you to add a "where" clause in the second parameter, instead of using the db->where() function:

    $query = $this->db->get_where('mytab', array('id' => $id), $limit, $offset);
    2- $this->db->select();
    Permits you to write the SELECT portion of your query:

    $this->db->select('title, content, date');

    $query = $this->db->get('mytable');

    // Produces: SELECT title, content, date FROM mytable

    Note: If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *

    $this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

    $this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE);
    $query = $this->db->get('mytable');
     
    mohammed ismail, Jul 9, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    So, you wrote this to show us CodeIgniter's redundant functions (well, classes) for pulling data?
     
    PoPSiCLe, Jul 10, 2015 IP
  3. mohammed ismail

    mohammed ismail Active Member

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #3
    yea i wrote this subject about how can you select data from DB over php CI ... and i will complete my lessons about codeigniter

    thank you
     
    mohammed ismail, Jul 15, 2015 IP
  4. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #4
    Actually CodeIgniter is obsolete in our days. PHP 5.4 has many interesting features, use them for easy life. Let see your examples in Laravel 5.* (or 4.* no difference)

    $items = MyModel::get();
    foreach ($items as $item) {
      echo $item->title;
    }
    PHP:
    Looks the same, but $item is an instance of MyModel class. You can use Accessors to change a field logic or relationships to get a related model ($item->tags for example), or many-many more.

    Few more examples with Laravel Eloquent ORM:

    Where condition:
    $items = MyModel::where('karma', '>=', 100)->orderBy('title', 'DESC')->get();
    PHP:
    Get one item by id or throw an error exception:
    $model = MyModel::findOrFail($id);
    PHP:
    Get a single model with related models:
    $model = MyModel::with('tags')->orderBy('updated_at', 'DESC')->first();
    print_r($model->tags->toArray()); // print tags as an array
    PHP:
    Print model as JSON string:
    echo $model->toJSON();
    PHP:
    Create/update model:
    $model = MyModel::firstOrNew(['slug' => 'hello']);
    $model->title = 'A new title';
    $model->save();
    PHP:

    The main idea is you are working not with data, but object. You can code your business logic inside a model class and your controllers and templates will be smaller and easier to understand and support.
     
    Sano000, Jul 19, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Yeah, I'm trying to figure out if this thread was a question or a statement; the language used is so vague Christmas only knows what it's actually about...

    Though it does an EXCELLENT job of proving what pointless bloated rubbish Codeigniter is.
     
    deathshadow, Jul 20, 2015 IP