Welcome Guest, Not a member yet? Register   Sign In
Possibly bug or incorrect code?
#1

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');

foreach($query as $result);
{
$id = $result->id;
$msg = $result->msg;
$from = $result->from;
// Here goes table html (i don't post)

then the code

echo "<tr>"
echo "<td>$id</td>";
echo "<td>$msg</td>";
echo "<td>$from</td>";

}

// 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.
Reply
#2

(This post was last modified: 07-04-2017, 10:23 PM by usmanahmed.)

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>";
}
Reply
#3

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()
{
$query $this->db->query("SELECT FROM `news` WHERE `active`=1");
 
 if ($query->num_rows() > 0) {
 
    return $query->result();
 
 }
 
 else {
 
    return FALSE;
 
 }


In your controller function, you put this code:
PHP Code:
$this->load->model('news_model');
$data['records'] = $this->news_model->get_news();
$this->load->view('news_overview',$data); 

In your view 'news_overview.php', you put this code:
PHP Code:
<?php if ($records) : ?>

  //table starts here
  <?php foreach($records as $record) : ?>
  <tr>
    <td><?= $record->id;?></td>
    <td><?= $record->msg;?></td>
    <td><?= $record->from;?></td>
  </tr>
  <?php endforeach; ?>

<?php else : ?>

  <p>There are no news articles at this time.</p>

<?php endif; ?>
Reply
#4

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.
Reply
#5

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.
Reply
#6

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
class News extends CI_Controller
{
 public function 
__construct ()
 {
 
 parent::__construct();
 
 $this->load->model("news_model");
 }
 public function 
index ()
 {
 
 $active_news $this->news_model->get_active_news();
 
 $data["active_news"] = $active_news;
 
 $this->load->view("news/index");
 }


PHP Code:
// oath: proj/application/models/News_model.php
class News_model extends CI_Model
{
 public function 
__construct ()
 {
 
 parent::__construct();
 
 $this->load->database();
 }
 public function 
get_active_news ()
 {
 
 $args = array(
 
  "active" => // assuming the column datatype is tinyint
 
 );
 
 $result $this->db->get_where("news"$args);
 
 $active_news $result->result_array();
 
 $result->free_result();
 
 return $active_news;
 }

PHP Code:
//path: proj/application/views/news/index.php
<!doctype html>
<
html>
 <
head> ... </head>
 <
body>
 
 <main>
 
  ...
 
   <article>
 
    <?php foreach ($active_news as $news) : ?>
       <h1><?php echo $news["title"?></h1>
       ...
     <?php endforeach; ?>
    </article>
  </main>
 </body>
</html> 
Reply
#7

(07-05-2017, 05:44 AM)cyborg Wrote:
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.

You could also try using var_dump(); on the query result.
Reply
#8

$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
//or 
$records $query->result_array();  // returns an array of arrays, each sub-array 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) {
 
 // ...

Reply




Theme © iAndrew 2016 - Forum software by © MyBB