0

I am a newbie to Codeigniter framework, I have created a simple app that will fetch data from database and then display with condition in Views. I'm trying to display specific data from database to my View, So far, I have no luck using the code below.

Model (Datamodel.php)

function getData() {
    $this->db->select("name, value");
    $this->db->from('settings');
    $query = $this->db->get();
    return $query->result_array();
}

Controller (maincontroller.php)

function index()
{
    $data['metadata'] = $this->Datamodel->getData();
    $this->load->view('viewdata', $data);
}

View

  <h3>Site Information</h3>
  <?php foreach($metadata as $data) {?>
      <h4>Site Title: <?php echo $data['site_title']; ?></h4>
      <h4>Site Description: <?php echo $data['site_description']; ?></h4>
      <h4>Site keyword: <?php echo $data['site_keyword']; ?></h4>
      <h4>Site Year: <?php echo $data['site_year']; ?></h4>
  <?php }?>

I want to display something like these on browser:

Site Information

Site Title: My first codeigniter app
Site Description: This is sample ci application
Site keyword: simple ci app, first app
Site Year: 2015

{ Any help & suggestion is accepted. thanks! pls see this image [ http://prntscr.com/722hsm ] for my database structure !}

2 Answers 2

1

Appreciate that you are taking initiative.

Just some modifications:

Model

<?php
function getData() {
  $siteData = array();
  $this->db->select("name, value");
  $this->db->from('settings');
  $query = $this->db->get();
  if ($query->num_rows()) {
    foreach ($query->result_array() as $row) {
    // Your data is coming from multiple rows, so need to loop on it.
      $siteData[$row['name']] = $row['value'];
    }
  }
  return $siteData;
}

View:

<h3>Site Information</h3>
<?php foreach($metadata as $dataName => $dataValue) {?>
    <!-- You are getting the names and values in loop, so, 
use it in loop. Also, use ucwords() and str_replace() to beautify your
 output. for more informtion on this, read php.net documentation.
       -->
  <h4><?php echo ucwords(str_replace('-', ' ', $dataName))?>: <?php echo $dataValue; ?></h4>
<?php }?>

SECOND APPROACH (WITHOUT FOREACH):

<h3>Site Information</h3>
<h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
<h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
<h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $metadata['site_year']; ?></h4>
Sign up to request clarification or add additional context in comments.

4 Comments

yes, it does work, but I just want to display the value.
You are already getting name and value from database, why do want to put hard-coded names against values coming from database?
sorry Im just a newbie. I'd like to prefer that way.
@AlexNewbie, welcome. Continue researching and you will become a good programmer.
1

Just load your model in controller:

function index()
{
    $this->load->model('Datamodel');
    $data['metadata'] = $this->Datamodel->getData();
    $this->load->view('viewdata', $data);
}

try this on your views

<h3>Site Information</h3>

      <h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
      <h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
      <h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
      <h4>Site Year: <?php echo $metadata['site_year']; ?></h4>

6 Comments

It shows error: A PHP Error was encountered Severity: Notice Message: Undefined index: site_title, site_description, site_keyword, site_year
It will show error because you are not looping on result data and data is in multiple rows. Please refer my answer below.
just try print_r($metadata) on your view page, and see what did you get.
I tried your answer Pupil. thanks for that. but I want to get the value only.
@dinesh i tried your suggestion print_r($metadata), and it shows like this on browser. Site Title: Array ( [site_description] => this is my first codeigniter mini app [site_keyword] => first ci app, sample website [site_title] => simple ci app, first app [site_year] => 2015 )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.