EllisLab text mark
Advanced Search
     
DB:  calling information from TABLE when row NAME =X
Posted: 19 November 2012 03:11 AM   [ Ignore ]
Joined: 2012-11-14
13 posts

So after a lot of thought, and input, and such I decided to build my blog based on a single table and categories.


What I mean is write all my articles to an article database and if the category is = blog then all that data shows on the blog page and if category data = news all that data shows on the news page. I was informed that this was the more difficult of the two routes but I’ve also been told its the better structure for ‘future proofing’.

My Controller

<?php

class Site extends CI_Controller{
 
 
function index()
 
{
  redirect
('site/news');
 

 
 
function news() {
   $data[
'main_content''news';
   
$this->load->view('template'$data);
 
}
 
 
function about() {
   $data[
'main_content''about';
   
$this->load->view('template'$data);
 
}
 
 
function resume() {
   $data[
'main_content''resume';
   
$this->load->view('template'$data);
 
}
 
 
function blog() {
   $data[
'main_content''blog';
   
$this->db->where('categories_id'2);
   
$data['$query'$this->db->get('articles');
   
$this->load->view('template'$data);
 
}

 
function projects() {
   $data[
'main_content''projects';
   
$this->load->view('template'$data);
 
}
 
 
function forums() {
   $data[
'main_content''forums';
   
$this->load->view('template'$data);
 
}

 

My Blog View

<title>Blog</title>

<
div id="container">
 <
h1>Welcome to CodeIgniter!</h1>

 <
div id="body">
  
<?php if ($query->num_rows() > 0): ?> 
   <?php 
foreach($query->result() as $row): ?>
   
<h3><?=$row->title?></h3>
   <
p><?=$row->body?></p>
   
   <
hr>
   
   
<?php endforeach; ?>
  <?php 
endif; ?>
 
</div>

 <
class="stats">Page rendered in <strong>{elapsed_time}</strongseconds</p>
</
div

 

if you look at my controller page you’ll see its setup for a template system if you look at “function blog” I think you can see what I am trying to accomplish. if the categories_id = 2 (2 is the blog category) load any articles with a category_id value of 2.


My blog view gives me an error at line 7

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/blog.php

Line Number: 7


codeigniter 2.1.3

 
Posted: 19 November 2012 04:22 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts
$data['$query'$this->db->get('articles'); 

You have $data[’$query’], which should be $data[‘query’] (no dollar sign on “query”)

 Signature 
 
Posted: 19 November 2012 09:05 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-14
13 posts

Well I updated my code as suggested to

<?php

class Site extends CI_Controller{
 
 
function index()
 
{
  redirect
('site/news');
 

 
 
function news() {
   $data[
'main_content''news';
   
$this->load->view('template'$data);
 
}
 
 
function about() {
   $data[
'main_content''about';
   
$this->load->view('template'$data);
 
}
 
 
function resume() {
   $data[
'main_content''resume';
   
$this->load->view('template'$data);
 
}
 
 
function blog() {
   $data[
'main_content''blog';
   
$this->db->where('categories_id'2);
   
$data['query'$this->db->get('articles');
   
$this->load->view('template'$data);
 
}

 
function projects() {
   $data[
'main_content''projects';
   
$this->load->view('template'$data);
 
}
 
 
function forums() {
   $data[
'main_content''forums';
   
$this->load->view('template'$data);
 
}


It’s gotten rid of the error but hasn’t displayed any of the content in the database. I commented out the

$this->db->where('categories_id'2); 

in hopes that maybe I was just calling the categories_id incorrectly. No dice however, still doesn’t show the contents of the database just a blank page with my <HR>‘s showing.


Thanks CroNiX for seeing the $query, as son at you called it I face palmed.

 
Posted: 19 November 2012 09:08 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

In your view, try doing a var_dump($query).

Are you loading the database?

 Signature 
 
Posted: 19 November 2012 09:28 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-11-14
13 posts

I am a php noob so I hope i I put this correctly i did

<?php var_dump($query)?> 


and this was the output I got.

object(CI_DB_mysql_result)#15 (8) { ["conn_id"]=> resource(29) of type (mysql link persistent) ["result_id"]=> resource(32) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(1) { [0]=> object(stdClass)#16 (5) { ["id"]=> string(1) "1" ["title"]=> string(12) "test entry 1" ["categories_id"]=> string(1) "2" ["date"]=> string(10) "2012-11-18" ["article"]=> string(11) "testing 123" } } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(1) ["row_data"]=> NULL } 
 
Posted: 19 November 2012 09:36 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

I see you are getting results there, so the query works.

Looking more closely at your view, I see you are mixing full php open tags (<?php) and short tags (<?).  Where you are using the short tags is where your output isn’t showing up.
So try changing these to full php open tags in your view:

<h3><?=$row->title?></h3>
<
p><?=$row->body?></p

Edit: one other thing, in your db results there isn’t a column called “body”.  Maybe you meant to have “article” there

 Signature 
 
Posted: 19 November 2012 09:51 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-11-14
13 posts

ahh yes I should mention I did change those to full tags when it would display blank, when I used short tags it would just show title?>

so too confirm

<?$row->title ?> 

just shows “title ?>”

changing the tags to full php

<php $row->title ?> 

shows blank

 
Posted: 19 November 2012 10:01 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts
<php $row->title ?> 

That is missing the ? before the php, and also you aren’t echoing anything.

<?php echo $row->title?> 

I think you are not going to have a very easy time with CI if you aren’t very fluent in basic php.  Not a putdown, just an observation.

 

 Signature 
 
Posted: 19 November 2012 10:14 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-11-14
13 posts

the ? was a mistake in my posting here I did have it in my actual code. The problem was in the ‘echo’. I’ve never done PHP before, I had read some books years ago and could never grasp it though with CI and some video tutorials I have already done a far cry more in the last week with PHP than I ever had before.


I was following the 20 minute blog tutorial on CI tutorials page. In the blog tutorial he uses shows hand and long hand and it seemed to work for him. I wonder is “<?=” the same thing as “<?php” or the same thing as “<?php echo” I ask because I didn’t see him ‘echo’ and his worked.

Thank you so much for your help, and I apologize for my noob-i-ness, I hope to learn PHP if I can find a better way to learn the basics, or if you have a better way I’ll gladly take it.

 
Posted: 20 November 2012 04:52 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-11-14
13 posts

I did want to make one quick question to add to all of this. My news and my blog posts are being displayed on their appropriate pages now but how would I make them acceding or descending based on date? I want the more recent post showing first.

 
Posted: 20 November 2012 05:20 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Active record’s order_by()