Page 1 of 1

Basic template system

Posted: Mon Sep 19, 2011 5:44 pm
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.
<?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
}
?>
And this is my posts.inc.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;
}

Re: Basic template system

Posted: Mon Sep 19, 2011 6:16 pm
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.

Re: Basic template system

Posted: Mon Sep 19, 2011 6:20 pm
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.

Re: Basic template system

Posted: Mon Sep 19, 2011 7:11 pm
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:

Re: Basic template system

Posted: Mon Sep 19, 2011 10:25 pm
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.

Re: Basic template system

Posted: Tue Sep 20, 2011 6:39 am
by icey2k
I guess I got unlucky on that query, its usually under 0.00004 =p

Re: Basic template system

Posted: Tue Sep 20, 2011 10:23 pm
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 ;)