Welcome Guest, Not a member yet? Register   Sign In
Problem in display (view)
#1

[eluser]johnmiller[/eluser]
Hey…

I have a table called ‘users’ with fields ‘name, address’.

There are 100 records in this table.

I need to display all the 100 records in the ‘view’.

But I am getting only one record(first record).

How to write the view which displays all the 100 records?

Can you please help me?

The below given are the model and controller

Model (blogmodel.php)

Code:
<?php

class Blogmodel extends  Model {

function select_entries()
{
    $this->db->select('name, address');
    $query = $this->db->get('users');
    return $query->row_array();

}
}
?>

Controller (blog.php)

Code:
<?php

class Blog Extends Controller {

function index()
{  
    $this->load->model('blogmodel');
    $data['query'] = $this->blogmodel->select_entries();
    $this->load->view('blogview', $data);
}
}

?>
#2

[eluser]beemr[/eluser]
Code:
$query->result_array();
instead of
Code:
$query->row_array();

Row_array() returns a single row
#3

[eluser]johnmiller[/eluser]
Thanks beemr, but if i use
Code:
result_array();
i get the output as the word 'array'.

The below is the view (blogview.php)

Code:
<html>
<body>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
&lt;?php foreach($query as $value):?&gt;
<tr>
<td>
can you please tell me how to write code here? if this is not correct, can you please tell me the correct code?
Code:
</td>

</tr>
&lt;?php endforeach; ?&gt;
</tbody>
</table>
&lt;/body&gt;
&lt;/html&gt;
#4

[eluser]beemr[/eluser]
So then, your results are an array. In the red text above, you would remove the surrounding <td> and insert
Code:
<td>&lt;?=$value['name']?&gt;</td>
<td>&lt;?=$value['address']?&gt;</td>

see the docs
#5

[eluser]elvix[/eluser]
just do a print_r($value) in there and it should print out all the values contained in the $value array. then just place the appropriate value ($value['something']) where you want it.

when it comes to arrays, print_r is your best friend. Smile
#6

[eluser]johnmiller[/eluser]
Thanks a lot everybody...

I tried the below code and it is working now...

Code:
&lt;?php $counter = count($query);?&gt;

<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
&lt;?php
for($i=0;$i<$counter;$i++)
{
?&gt;  
  <tr>
    <td>&lt;?php echo $query[$i]->name;?&gt;</td>
    <td>&lt;?php echo $query[$i]->address;?&gt;</td>
  </tr>
&lt;?php
}
?&gt;
</tbody>
</table>


Thanks a lot to everybody who helped me...

Someone can use this code in the future...
#7

[eluser]Randy Casburn[/eluser]
You can try this too...

Code:
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
&lt;?php foreach($query as $row):?&gt;
<tr>
<td>&lt;?php echo $row['name'];?&gt;</td>
<td>&lt;?php echo $row['address'];?&gt;</td>
</tr>
&lt;?php endforeach;?&gt;
</tbody>
</table>

Randy Smile
#8

[eluser]johnmiller[/eluser]
Thanks Randy




Theme © iAndrew 2016 - Forum software by © MyBB