phpmysqlcodeignitercodeigniter-datamapper

How to get table values using array and conditions


I'm studying and decided to make a CodeIgniter application with backend and frontend for learning purposes and I came across the following doubts. Would you like to get all data from a table in the database and put in an array, after that I would like to display the data in column 2 when the value of column 1 is equal to the value I determine.

To better illustrate what I'll give an example, look at the table below.

ID      Option              Value
1       site_name           Test
2       site_description    Example of description site.
3       site_url            http://www.example.com
4       site_author         John Doe
5       site_lang           en

Now let's say I want to display this data on my page within the tag for example I would use the "site_name", for a description of the site would be "site_description" and so on. What would be the best way to do this?

PS: I'm using an ORM Datamapper this application, is there any way to simplify operation using it?


Solution

  • Your question is very unclear - If I'm interpreting it correctly, and if you're using Datamapper ORM you'd do this:

    Your controller - controllers/sites.php:

    <?php
    class Sites extends CI_Controller{
        function __construct(){
            parent::__construct();
        }
    
        function get($id){
            $s = new Site($id);
            $data['site'] = $s->to_array();
            $this->load->view('sites', $data);
        }
    }
    

    Your model - site.php:

    <?php
    class Site extends Datamapper{
        function __construct($id){
            parent::__construct($id);
        } 
    }
    

    By default, Datamapper ORM does not support the to_array() method, you must enable it. Head into config/datamapper.php and modify the last 'extensions' array element to: $config['extensions'] = array('array');

    Finally, in your view - views/sites.php:

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Option</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
            <? foreach($sites as $sK => $sV):?>
                <tr>
                    <td><?= $sV['id'];?></td>
                    <td><?= $sV['option'];?></td>
                    <td><?= $sV['value']?></td>
                </tr>
            <? endforeach;?>
        </tbody>
    </table>
    

    Then put http://localhost/yourapp/sites/get/<id> into your browser, where <id> is actually some appropriate ID in your database.

    Edit after your comment:

    If these are site-wide globals, I advise against storing them in the database. No sense making database calls to grab this information. Just embed it in your PHP. The way I do this is by calling define() at the top of my front-facing index.php file:

    define('TITLE', "Some Title");
    define('DEVELOPER', "Your Name");
    define('DEVELOPER_EMAIL', 'youremail@example.com');
    

    These will then be available for you anywhere in your application. Simply calling <?= TITLE; ?>, for example, will echo "Some Title".

    You could also look into using config/constants.php.