0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • when you're retrieving data from your CodeIgniter model depending on which method you use to return your data the data can be accessed using the column name and then you can put it in a table or whatever you want. I'm not sure if thats what you're looking for though, you should read up on how to return query results here: http://codeigniter.com/user_guide/database/results.html – Will Sampson Oct 03 '12 at 20:08

1 Answers1

1

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.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • Sorry, my English is not very good and should not have been able to express myself. I know you talked about using arrays, but that because I thought it would be the easiest way, might be mistaken. Anyway what I want to do is display the Option Value when the value is requested, using the following logic: If Option is equal site_name then show the Value, which in this case is Test. While searching for a solution I found a few places talking about Multidimensional Arrays, but did not quite understand how to capture the data, process and then display them in view. – Bruno Marcel Oct 04 '12 at 13:21
  • Sorry, still don't understand yet. How are you proposing to "request the value"? By passing the option in the url string? As in: `http://localhost/yourapp/sites/get_some_option/site_name`? If you gave samples of your database & your desired view, it would be very helpful. – Jordan Arsenault Oct 04 '12 at 14:20
  • If Option equals site_name then show the value corresponding to the line, this value will be shown on several pages and would not need to call the value for ID even intend to leave this model as autoload. These values ​​would be used for the configuration of the application, for example site_name would be the main title of the site and would be within the tag . – Bruno Marcel Oct 05 '12 at 17:53
  • After much searching I found something, just do not know yet to do this on CodeIgniter pulling data from the database. "Test", "site_description" => "Example of description site.", "site_author" => "John Doe"); ?> Home | <?php echo $settings['site_name']; ?> – Bruno Marcel Oct 22 '12 at 18:06