0

I am relatively experienced PHP developer.

I have chosen the CodeIgniter framework. So far I have understood how it is used. Until now, I want to be able to do a while loop.

Within my controller I have:

$this->load->model('Test');
$data['result'] = $this->Test->getInfo();
$this->load->view('index1', $data);

In my Model I have:

$result = mysql_query("SELECT * FROM users WHERE last_name='Hart'")or die(mysql_error());  
$row = mysql_fetch_array( $result );    
return $row;

And in my View:

echo $result[0];

However, this only outputs the first field within the first row found.

Please could you help me with retrieving the information from a database - while loop.

Does the while loop happen in the Model? Am I just echoing out the result correctly?

2 Answers 2

9

There's actually a much easier way to do what you want to do using the ActiveRecord class that CodeIgniter includes which will allow you to just return an array of results. Here's the documentation.

Your model would become:

$this->db->where('last_name','Hart');
$result = $this->db->get('users');
return $result->result_array();

You may also have to setup your database in application/config/database.php, and also load the database class in application/config/autoload.php:

$autoload['libraries'] = array('database');

To display this information, using the MVC pattern properly your controller should pass the information it gets from the model to a view. To do this generally you do this:

$data['myinfo'] = $this->test->getInfo();
$this->load->view('test_view',$data);

Then you have to create a view like so in applications/views/test_view.php:

<h1>Some HTML goes here</h1>
<?php
foreach($myinfo as $row) {
    echo $row['field'];
}
?>
<p>Some more HTML goes here</p>

I suggest you read the CodeIgniter User Guide before diving into creating an application as CodeIgniter includes libraries that greatly simplify and speed up the whole process.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok great! Where do I declare the db connection details? And do i echo it out the same then?
I just threw that information in the post and I'm about to edit it again to add information on displaying that info properly.
@hart1994 - read the documentation for db connection details!
Thanks a lot, this has worked without any problems. Thanks for your time. Much appreciated!
0

Well mysql_fetch_array will only bring back one row at a time, so you would need to put the while loop around the mysql_fetch_array call

Something like

while($row = mysql_fetch_array($result)) { //save/process data }

I would however just use codeigniters libraries to load the db and then use it:

$this->load->database();
$result = $this->db->query("SELECT * FROM users WHERE last_name='Hart'");
$result = $result->result_array();
//check for data and handle errors
return $result[0]

Comments

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.