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?
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:
You may also have to setup your database in
application/config/database.php, and also load the database class inapplication/config/autoload.php: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:
Then you have to create a view like so in
applications/views/test_view.php: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.