[eluser]codejack[/eluser]
Thanks!
That seems to be returning an empty array.
This:
Code: <pre><?php print_r($posts); ?></pre>
<?php print $this->db->last_query(); ?>
Returns this:
Code: Array
(
)
SELECT blog_post.id, blog_post.user_id, blog_post.title, blog_post.category, blog_post.date_published, blog_post.content, user.username, COUNT(blog_comment.id) AS total_comments FROM (`blog_post`) JOIN `user` ON `user`.`id` = `blog_post`.`user_id` LEFT JOIN `blog_comment` ON `blog_comment`.`post_id` = `blog_post`.`id` WHERE `blog_post`.`date_published` = 1 GROUP BY `blog_comment`.`post_id` ORDER BY `blog_post`.`date_published` desc
[eluser]vbsaltydog[/eluser]
How are you assigning $posts and where are you doing your print_r ?
print_r() in the model so we can rule out controller/view errors.
[eluser]codejack[/eluser]
Here's my complete code at the moment:
CONTROLLER:
Code: class Blog extends CI_Controller
{
function index()
{
// If user has logged in.
if ($this->session->userdata('username'))
{
$this->load->model('blog_model');
$data['current_user'] = $this->user_model->current_user();
$data['posts'] = $this->blog_model->get_all_posts();
$this->load->view('blog_view', $data);
}
else
{
// Redirect to the login page.
redirect('login');
}
}
}
MODEL:
Code: class Blog_model extends CI_Model
{
public function get_all_posts()
{
$query = $this->db->select('blog_post.id, blog_post.user_id, blog_post.title, blog_post.category, blog_post.date_published, blog_post.content, user.username, COUNT(blog_comment.id) AS total_comments', false)
->from('blog_post')
->join('user', 'user.id = blog_post.user_id')
->join('blog_comment', 'blog_comment.post_id = blog_post.id', 'left')
->where('blog_post.date_published', 1)
->group_by('blog_comment.post_id')
->order_by('blog_post.date_published', 'desc')
->get();
return $query->result_array();
}
}
VIEW:
Code: <?php $this->load->view('includes/header_view'); ?>
<div class="row">
<div class="eight columns content">
<?php foreach ($posts AS $post) : ?>
<div class="box-ribbon">
<div class="ribbon">
<img src="<?php echo site_url('images/icons-blog-category') . '/' . $post->category . '.png'; ?>" alt="" />
<p><?php echo clean_date($post->date_published); ?></p>
<div class="clr"></div>
</div>
<div class="box-ribbon-content">
<h2><a href="#"><?php echo $post->title; ?></a></h2>
<p><em>by <a href="<?php echo site_url('people') . '/' . $post->username; ?>"><?php echo $post->username; ?></a> / <?php echo relative_time($post->date_published) . "ago"; ?> / 13 comments</em></p>
<?php echo $post->content; ?>
</div> <!-- end box-ribbon-content -->
</div> <!-- end box-ribbon -->
<div class="box-bottom"></div>
<?php endforeach; ?>
</div> <!-- end content -->
<div class="four columns sidebar">
<div class="box pad0">
<center><img src="<?php echo site_url('images/sponsor-astro.png'); ?>" alt="Astro Gaming" /></center>
</div> <!-- end box -->
<div class="box-bottom"></div>
<div class="box pad0">
<center><img src="<?php echo site_url('images/sponsor-gametraders.png'); ?>" alt="Gametraders" /></center>
</div> <!-- end box -->
<div class="box-bottom"></div>
<div class="box">
<h4>January '12</h4>
<ul>
<li>No exclusive fighters in Xbox Street Fighter X Tekken</li>
<li>Gaikai full-game streaming to start 3 months after launching on Facebook</li>
<li>Max Payne gear to dress up, slow down your Avatar</li>
<li>Why Soul Calibur Still (Quietly) Burns After More than a Decade</li>
</ul>
<h4>December '11</h4>
<ul>
<li>Boingo offers free Wi-Fi to 3DS users at select UK airports</li>
<li>Hudson name will live on after Konami takeover</li>
<li>Slightly Mad's Project Cars coming to Wii U, has nothing to do with Pixar</li>
<li>Beta test Skyrim 1.4 on Steam (if you dare)</li>
</ul>
<h4>November '11</h4>
<ul>
<li>SF X Tekken reality show puts the spotlight on fighter fans</li>
<li>Sega's new UK studio is 'Hardlight'</li>
<li>LittleBigPlanet 2's Muppet DLC is just about the happiest thing ever</li>
<li>Rayman Origins comes to 3DS in North America March 20</li>
<li>Microsoft hiring for 'LEAP' and 'Platform Next' game studios</li>
</ul>
<h4>October '11</h4>
<ul>
<li>Capcom Digital Collection arrives on March 27 in US, March 30 in Europe</li>
<li>Ninja Gaiden 3 dev diary reasserts seriousness of ninja game development</li>
<li>Daxter, more PSP games re-rated for PlayStation Certified devices</li>
</ul>
<h4>September '11</h4>
<ul>
<li>Sony, CCP 'negotiating PS3 virtual item policy' for DUST 514</li>
<li>Grand Theft Auto 3 and GTA: Vice City rated by ESRB for PS3</li>
<li>FIFA 12 defends its UK sales championship, Rayman ascends</li>
<li>Japanese hardware sales, January 9 - 15: Looking back edition</li>
</ul>
<h4>August '11</h4>
<ul>
<li>Sony, CCP 'negotiating PS3 virtual item policy' for DUST 514</li>
<li>Sniper Elite V2 trailer, dev diary and gallery explode in your face</li>
<li>Resident Evil 4 on Android, exclusive to Korea's LG LTE market (for now)</li>
<li>Official Mario and Zelda songbooks now available in English</li>
<li>The Joystiq Indie Pitch: Defender's Quest</li>
<li>Zynga's paid $300 per new user in the past nine months, says analyst</li>
</ul>
</div>
<div class="box-bottom"></div>
</div> <!-- end sidebar -->
</div> <!-- end row -->
<?php $this->load->view('includes/footer_view'); ?>
I dropped the print_r and the last_query before opening the foreach statement in the view.
[eluser]vbsaltydog[/eluser]
@Aken
You're up.
[eluser]Aken[/eluser]
The empty array means there are no errors in the query - it just isn't returning any results. Usually means something like a WHERE clause is not matching any results.
Which is probably the problem - looks like I put the wrong column name in the where() method.
[eluser]vbsaltydog[/eluser]
Code: ->where('blog_post.date_published', 1)
How did that get through unnoticed?
[eluser]codejack[/eluser]
Ok, sweet. Not sure how I missed that either.
It's working now, but it's only returning 2 results for some reason. There are 4 published articles though.
:S
Code: public function get_all_posts()
{
$this->db->select('blog_post.id, blog_post.user_id, blog_post.title, blog_post.category, blog_post.date_published, blog_post.content, user.username, COUNT(blog_comment.id) AS total_comments', false);
$this->db->from('blog_post');
$this->db->join('user', 'user.id = blog_post.user_id');
$this->db->join('blog_comment', 'blog_comment.post_id = blog_post.id', 'left');
$this->db->where('blog_post.status_published', 1);
$this->db->group_by('blog_comment.post_id');
$this->db->order_by('blog_post.date_published', 'desc');
$query = $this->db->get();
return $query->result_array();
}
Weird.
[eluser]vbsaltydog[/eluser]
[quote author="codejack" date="1327998525"]
It's working now, but it's only returning 2 results for some reason. There are 4 published articles though.
[/quote]
Possibly because of the group_by clause.
[eluser]InsiteFX[/eluser]
Thats because it should be:
Code: return $query->result();
// not!
return $query->result_array();
He is using objects in his view!
[eluser]codejack[/eluser]
Thanks for all the help, guys. I got it working.
Code: class Blog_model extends CI_Model
{
public function get_all_posts()
{
$this->db->select('blog_post.id, blog_post.user_id, blog_post.title, blog_post.category, blog_post.date_published, blog_post.content, user.username, COUNT(blog_comment.id) AS total_comments', false);
$this->db->from('blog_post');
$this->db->join('user', 'user.id = blog_post.user_id');
$this->db->join('blog_comment', 'blog_comment.post_id = blog_post.id', 'left');
$this->db->where('blog_post.status_published', 1);
$this->db->group_by('blog_post.id');
$this->db->order_by('blog_post.date_published', 'desc');
$query = $this->db->get();
return $query->result();
}
}
Couldn't have done it without you!
|