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]
Basic template system
Re: Basic template system
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
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
I figured it out.. Apparently my vps cant connect to database from external ip.. so I had to use localhost..
Anyways.. Thanks
![Laughing :lol:](./images/smilies/icon_lol.gif)
Anyways.. Thanks
![Laughing :lol:](./images/smilies/icon_lol.gif)
![Laughing :lol:](./images/smilies/icon_lol.gif)
![Laughing :lol:](./images/smilies/icon_lol.gif)
Re: Basic template system
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
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Re: Basic template system
I guess I got unlucky on that query, its usually under 0.00004 =p
Re: Basic template system
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![Wink ;)](./images/smilies/icon_e_wink.gif)
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
![Wink ;)](./images/smilies/icon_e_wink.gif)