How to Format and Organise a result of a query which is a continuos array?
My Code :
public function get_all_posts(){
$this->load->database();
$sql="SELECT * FROM posts";
$query=$this->db->query($sql);
foreach($query->result() as $item)
{
foreach($item as $record)
{
echo $record;
}
Sample Output:
36sss37Does it work?38394041aaaaaaa42hey43hey44qqq45hey46malibu
Expected Output:
36 sss
37 Does it work?
38
39
...
42 hey
43 hey
So,that explains it all,i'm just trying to organise and possibly format the output of the query. Also,is it possi开发者_如何学Pythonble to work on individual records? like,is it possible to add a comments module to each post here?
I guess the method get_all_posts() is a part of your model. It's not a good practise to use formating of the result in your model. You have to format the result in your view.
Model
public function get_all_posts(){
$this->load->database();
$sql="SELECT * FROM posts";
$query=$this->db->query($sql);
return $query->result();
}
Controller
$this->load->model('Your_model', 'post');
$data['posts'] = $this->post->get_all_posts();
$this->load->view('your_view', $data);
View
<?php foreach($posts as $post): ?>
<h2><?php echo $post->title; ?></h2>
<p><?php echo $post->content; ?></p>
<?php endforeach; ?>
You can get more info from here: http://codeigniter.com/user_guide/general/models.html
try using the $item as $key=>$value
$iter = 0;
foreach($item as $record){
if($iter == 1){
echo "<br />";
$iter = 0;
}
echo $record;
$iter++;
}
精彩评论