0

I'm making a query in PHP using CodeIgniter to get the data that I wanted from a database. To be able to get all values for a specific column, I stored it into an array and passed it into a view. But I can do it only for one column.

Here's my code:

$query = $this->db->query('SELECT name, description FROM module'); $result = $query->result_array(); foreach ($result as $key => $rowdata) { $resultdata['values'][$key] = $rowdata['name']; } $this->load->view('myview', $resultdata); 

With this scenario I can get all name from the module table. But my problem is, I also wanted to get all the description in the module table, but I don't know how can I implement it and be able to pass it into the view. Hope someone could help me with this. Thanks!

2
  • Can you display the code from result_array() ? Is this a custom function of yours ? Commented Jan 30, 2013 at 11:20
  • nope! result_array() is a codeigniter function for getting database value into an array. if there's result() function, then there's also result_array(). Commented Jan 30, 2013 at 11:21

4 Answers 4

2

Your not using the MVC pattern!

First you should write your query in a model!

Then load it in your controller like this

$this->load->model('ModelName'); 

Then call the funcion to retreive the data

$data = $this->modelname->functionToRetreiveData(); 

Then loop through data with foreach

$dataToView = array(); foreach($data as $key=>$row) { $dataToView['something'][$key]['value'] = $row->name_of_column; } 

And pass the array to the view $this->load->view('someview',$dataToView);

Then in the view

foreach($value as $val): <p><?php echo $val['name']?><p> endforeach 
Sign up to request clarification or add additional context in comments.

Comments

1

hi in you way you will do loop twice in controller and then in view to print it check this out

//in controller

 $query = $this->db->query('SELECT name,description FROM module'); $resultdata['results'] = $query->result_array(); $this->load->view('myview',$resultdata); 

myview.php

foreach($results as $result) { echo $result['name'],' ',$result['description']; } 

4 Comments

This answer is not a very good one. You're solving the problem but you're not using the MVC pattern whatsoever as you're doing data manipulation in the controller.
Joao Dias we are here to give solution not to give extra problem if guy is new to CI or PHP, if he asked me about MVC i can give answer according to MVC
@JoãoDias: Thank you for reminding me of MVC. Yes I'm new to codeigniter and because of just thinking of something to work rather than following a good practice, I forgot to create my model. And maybe that's also because I've hard time figuring this one out...sorry for that. Once again, thank you for your attempt, I appreciate it a lot.
@umefarooq CI is intended for the MVC pattern. I understand that the author is somewhat newbie in the concept, but giving him an inacurate answer won't help him to learn. And this comunity is intended for that! Share and Learn. That's why I've voted your answer down
1
$query = $this->db->query('SELECT name,description FROM module'); $result = $query->result_array(); foreach($result as $rowdata){ $resultdata['values'][] = $rowdata; } $this->load->view('myview',$resultdata); 

Try in view:

print_r($values); 

You will probably have:

$result[0]['name'], $result[0]['description'] ... 

Comments

0
$this->load->database(); $this->db->select('employee'); $query = $this->db->get(); return $query; foreach ($query as $row) { print $row->'Name'; . . . print $row->'Address'; } 

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.