Basic template system

Ask about a PHP problem here.
Post Reply
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Basic template system

Post by icey2k »

Hey guys,

I'm just wondering if its possible to combine a "blog tutorial"-lookalike, and the basic template system from the tutorial?

I get things working, but as soon as I start getting data from the database in the home.page.inc.php file, nothing is listed.

This is what my home.page.inc.php file looks like.
[syntax=php]<?php

$posts = get_posts();

foreach ($posts as $post){

?>
<div class="post">
<p class="meta"><span class="date"><?php echo $post['date']; ?></span><a href="?page=users&id=<?php echo $post['user_id']; ?>">
<?php echo $post['user']; ?></a></p>
<h2 class="title"><a href="?page=read&id=<?php echo $post['post_id']; ?>"><?php echo $post['title']; ?></a></h2>
<div class="entry">
<p><?php echo $post['preview']; ?></p>
<a href="?page=read&id=<?php echo $post['post_id']; ?>" class="links">Les mer..</a>
</div>
</div>
<?php
}
?>
[/syntax]

And this is my posts.inc.php
[syntax=php]function get_posts(){
$sql = "SELECT
`postdata`.`post_id`,
`postdata`.`post_title`,
LEFT(`postdata`.`post_body`, 512),
DATE_FORMAT(`postdata`.`post_date`, '%d.%m.%Y - %h:%i:%s'),
`postdata`.`user_id`,
`userdata`.`user_id`,
`userdata`.`username`
FROM `postdata`
INNER JOIN `userdata`
ON `postdata`.`user_id` = `userdata`.`user_id`
ORDER BY `postdata`.`post_date` DESC
";

$posts = mysql_query($sql);
$rows = array();

While (($row = mysql_fetch_assoc($posts)) !== false){
$rows[] = array(
'post_id' => $row['post_id'],
'title' => $row['post_title'],
'preview' => $row['post_body'],
'user' => $row['username'],
'date' => $row['post_date'],
'user_id' => $row['user_id'],
);
}
return $rows;
}[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Basic template system

Post by jacek »

Try running the SQL in phpmyadmin. Most likely, you have messed up the JOIN somehow so that it returns 0 rows all the time.
Image
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Re: Basic template system

Post by icey2k »

jacek wrote:Try running the SQL in phpmyadmin. Most likely, you have messed up the JOIN somehow so that it returns 0 rows all the time.


Noo, I've checked that 10 times now, and it returns correctly.

"Showing rows 0 - 0 (1 total, Query took 0.4887 sec)"


When I removed the get_posts(); function the site is loaded correctly.
I've tried to add an include("../functions/posts.inc.php"); no luck.
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Re: Basic template system

Post by icey2k »

I figured it out.. Apparently my vps cant connect to database from external ip.. so I had to use localhost..

Anyways.. Thanks :lol: :lol: :lol:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Basic template system

Post by jacek »

icey2k wrote:"Showing rows 0 - 0 (1 total, Query took 0.4887 sec)"

Half a second for a simple SELECT !

You need better hosting !

Also good news :D Glad you got it working.
Image
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Re: Basic template system

Post by icey2k »

I guess I got unlucky on that query, its usually under 0.00004 =p
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Basic template system

Post by jacek »

Well the second time you run it the result will be still be in memory form the first time.

The 0.5 is more likely representative of the time take to query the data on the disk.

You probably did get a bit unlucky too ;)
Image
Post Reply