Possibly bug or incorrect code? |
Query returns only one row in table (I tried with table helper, and without with my own table).
here is the example of view Code: $query = $this->db->query("SELECT FROM `news` WHERE `active`='1'); // It returns only 1 row in table, but i need all where the `active` is '1'. I tried with model too, no works. Hope it will be resolved or i will try another PHP MVC Framework.
Remove the semicolon from "foreach($query as $result);" and try. Also, you forgot to add ending double quote in the query.
$query = $this->db->query("SELECT FROM `news` WHERE `active`=1"); foreach($query as $result) { echo "<tr>"; echo "<td>$result->id</td>"; echo "<td>$result->msg</td>"; echo "<td>$result->from</td>"; echo "</tr>"; }
First of all, it's not good practice to put database actions inside a view.
CI is based on the MVC mechanism: model - view- controller. Controllers are for your program logic. Models are for database interaction. Views are for ouptut to the browser. The use of models is optional in CI, but it's good to know the principals. Create a News_model.php model in the application/models folder. Create this method inside it: PHP Code: public function get_news() In your controller function, you put this code: PHP Code: $this->load->model('news_model'); In your view 'news_overview.php', you put this code: PHP Code: <?php if ($records) : ?>
Apart from being rather bad code (see Wouter60 comments)
where is your $query->result(); statement?
On the package it said needs Windows 7 or better. So I installed Linux.
Quote:First of all, it's not good practice to put database actions inside a view. I accept this fact that putting database actions inside a view is bad, but i've tried in model and then i was investigating the issue, but the same as i was having experience in the model. I will try this and will see.
If it returns one row only then it must be because that the query only got one row, check the rows of your table and see if they are active, chances are there is only one row which has an active column set to true in your table.
PHP Code: //path: proj/application/controllers/News.php PHP Code: // oath: proj/application/models/News_model.php PHP Code: //path: proj/application/views/news/index.php
(07-05-2017, 05:44 AM)cyborg Wrote:Quote:First of all, it's not good practice to put database actions inside a view. You could also try using var_dump(); on the query result.
$this->db->query(...) returns an object, not an array with the records from the table.
To fetch the records from the query object, you need to do this: PHP Code: $records = $query->result(); //returns an array of objects, each object is a table record You can use foreach { } to cycle through the records and to get access to the fields inside each record: PHP Code: foreach ($records as $record) { |
Welcome Guest, Not a member yet? Register Sign In |