Welcome Guest, Not a member yet? Register   Sign In
DB: calling information from TABLE when row NAME =X
#1

[eluser]john.weland[/eluser]
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
Code:
<?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
Code:
<title>Blog</title>

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

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

<p class="stats">Page rendered in <strong>{elapsed_time}</strong> seconds</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

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/blog.php

Line Number: 7


codeigniter 2.1.3
#2

[eluser]CroNiX[/eluser]
Code:
$data['$query'] = $this->db->get('articles');

You have $data['$query'], which should be $data['query'] (no dollar sign on "query")
#3

[eluser]john.weland[/eluser]
Well I updated my code as suggested to


Code:
&lt;?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
Code:
$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.
#4

[eluser]CroNiX[/eluser]
In your view, try doing a var_dump($query).

Are you loading the database?
#5

[eluser]john.weland[/eluser]
I am a php noob so I hope i I put this correctly i did
Code:
&lt;?php var_dump($query)?&gt;


and this was the output I got.
Code:
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 }
#6

[eluser]CroNiX[/eluser]
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 (&lt;?php) and short tags (&lt;?). 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:
Code:
<h3>&lt;?=$row->title?&gt;</h3>
<p>&lt;?=$row->body?&gt;</p>

Edit: one other thing, in your db results there isn't a column called "body". Maybe you meant to have "article" there
#7

[eluser]john.weland[/eluser]
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?&gt;

so too confirm

Code:
&lt;?= $row->title ?&gt;
just shows "title ?&gt;"

changing the tags to full php
Code:
<php $row->title ?&gt;
shows blank
#8

[eluser]CroNiX[/eluser]
Code:
<php $row->title ?&gt;
That is missing the ? before the php, and also you aren't echoing anything.

Code:
&lt;?php echo $row->title; ?&gt;

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.

#9

[eluser]john.weland[/eluser]
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 "&lt;?=" the same thing as "&lt;?php" or the same thing as "&lt;?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.
#10

[eluser]john.weland[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB