Jump to content

how do I display same page for end-user and admin separately!


manddox

Recommended Posts

hi all...

well silly but i really got stuck in my project and don knw where else to go for help!

 

I am trying to display my blog entries in a single page. Let me explain my confusion in detail so that you guys will be able to understand what I am trying to do. My blog will show the titles of the postings and on clicking on the title, the post details displays below the titles. the link to the screenshots are here:http://img39.imageshack.us/i/72074778.jpg/ and then on clicking one of the titles, it shows:http://img151.imageshack.us/i/39614734.jpg/

 

Now when clicking only one of the titles, it displays all of the postings which should not be the case. It should show only the posts details which is clicked. The smarty template directs back to the same myblog.php page. below is my myblog.php page

<html>
<head>
<body>
<div class="blog-content"> <?php
require_once('config.php');
require_once('../db_login.php');
require_once('DB.php');
//Dispaly the page header
$smarty->assign('blog_title', $blog_title);
$smarty->display('header.tpl');
//Check the valid login

$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if(DB::isError($connection))
{
die ("Could not connect to the database:<br />".DB::errorMessage($connection));
}

//Query the database for posts with categories and user information
$query = "SELECT * FROM users NATURAL JOIN posts NATURAL JOIN categories ORDER BY posted DESC";
//Execute the query
$result = $connection->query($query);
if(DB::isError($result))
{
die ("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
// Place the query results into an array
while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$test[] = $result_row;
}
//Send the data to the template
$smarty->assign('posts', $test);
//Display the template with the data plugged in
$smarty->display('posts.tpl');

?>
<br />
<?php
if($_GET["post_id"])
{
$post_id = $_GET["post_id"];
$post_id = mysql_real_escape_string(get_magic_quotes_gpc()?stripslashes($post_id):$post_id);
$query = "SELECT * FROM users NATURAL JOIN posts NATURAL JOIN categories WHERE post_id=$post_id";
$result = $connection->query($query);
if(DB::isError($result))
{
die ("Could not query the database: <br />".$query."".DB::errorMessage($result));
}
while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$test[] = $result_row;
}
$smarty->assign('owner_id', $_SESSION["user_id"]);
$query = "SELECT * FROM users NATURAL JOIN comments WHERE post_id = $post_id";
$result = $connection->query($query);
if(DB::isError($result))
{
die ("Could not query the database: <br />".$query."".DB::errorMessage($result));
}
$comment_count = $result->numRows();
while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$comments[] = $result_row;
}
$smarty->assign('posts', $test);
$smarty->assign('comments', $comments);
$smarty->assign('comment_count', $comment_count);
?><div class="comment"><?php
$smarty->display('view_post.tpl');?></div>
<?php $connection->disconnect();
}
?>
</div>
</body>
</html>

below is my posts.tpl page:[code]
{section name=mysec loop=$posts}
<img src="example_images/some_icon_dark.png" alt=""/>
<a href="myblog.php?post_id={$posts[mysec].post_id}">{$posts[mysec].title}</a>
by <em>{$posts[mysec].first_name} {$posts[mysec].last_name}</em>
from the <em>{$posts[mysec].category}</em>
category at {$posts[mysec].posted}.
<br />
{/section}
<br />
Click to <a href="modify_posts.php?action=add">add</a> a posting. <br />

 

But this works only for the end user even though i manage to successfully display only one post when clicking on the particular title. If I want to manage my blog I'll have to visit my view_post.php page which looks like this:

<?php
session_start();
require_once('../db_login.php');
require_once('config.php');
require_once('DB.php');
//Display the header
$smarty->assign('blog_title', $blog_title);
$smarty->display('header.tpl');
//Check for valid login
if(!isset($_SESSION["username"]))
{
echo 'Please <a href="login.php">login!</a>';
exit;
}
//connect to the database
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if(DB::isError($connection))
{
die ("Could not connect to the database: <br />".DB::errorMessage($connection));
}
$post_id = $_GET["post_id"];
$post_id = mysql_real_escape_string(get_magic_quotes_gpc()?stripslashes($post_id):$post_id);
$query = "SELECT * FROM users NATURAL JOIN posts NATURAL JOIN categories WHERE post_id=$post_id";
$result = $connection->query($query);
if(DB::isError($result))
{
die ("Could not query the database: <br />".$query."".DB::errorMessage($result));
}
while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$test[] = $result_row;
}
$smarty->assign('owner_id', $_SESSION["user_id"]);
$query = "SELECT * FROM users NATURAL JOIN comments WHERE post_id = $post_id";
$result = $connection->query($query);
if(DB::isError($result))
{
die ("Could not query the database: <br />".$query."".DB::errorMessage($result));
}
$comment_count = $result->numRows();
while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$comments[] = $result_row;
}
$smarty->assign('posts', $test);
$smarty->assign('comments', $comments);
$smarty->assign('comment_count', $comment_count);
$smarty->display('view_post.tpl');

$connection->disconnect();
//Display the footer
$smarty->display('footer.tpl');
?>

 

And the view_post.php page will be displayed by posts.tpl which actually is like this:

{section name=mysec loop=$posts}
<img src="example_images/some_icon_dark.png" alt=""/>
<a href="view_post.php?post_id={$posts[mysec].post_id}">{$posts[mysec].title}</a>
by <em>{$posts[mysec].first_name} {$posts[mysec].last_name}</em>
from the <em>{$posts[mysec].category}</em>
category at {$posts[mysec].posted}.
<br />
{/section}
<br />
Click to <a href="modify_posts.php?action=add">add</a> a posting. <br />

 

and the view_post.tpl page is:

{section name=mysec loop=$posts}
<h2>{$posts[mysec].title}</h2>
{$posts[mysec].body}
<br />
Posted by <b> {$posts[mysec].first_name} {$posts[mysec].last_name}</b>
from the <em> {$posts[mysec].category} </em> Category at
<em> {$posts[mysec].posted}</em>. <br />
<a href="modify_comments.php?post_id={$posts[mysec].post_id}&action=add">Add a comment</a>
{if $posts[mysec].user_id == $owner_id}
|| <a href="modify_posts.php?post_id={$posts[mysec].post_id}&action=edit">Edit</a> ||
<a href="modify_posts.php?post_id={$posts[mysec].post_id}&action=delete">Delete</a>
<br />
{/if}
{/section}
{if $comment_count != "0"}
<h3>Comments</h3>
{section name=mysec2 loop=$comments}
<hr/>
<b>{$comments[mysec2].title}</b>
<br />
{$comments[mysec2].body}
<br />
Posted by <b> {$comments[mysec2].first_name} {$comments[mysec2].last_name} </b>
at <em>{$comments[mysec2].posted}</em> <br />
{if $comments[mysec2].user_id == $owner_id}
<a href="modify_comments.php?comment_id={$comments[mysec2].comment_id}&action=edit">Edit</a> ||
<a href="modify_comments.php?comment_id={$comments[mysec2].comment_id}&action=delete">Delete</a> <br />
{/if}
{/section}
{/if}

 

If I use the posts.tpl page to redirect to myblog.php page for each click on the post title, I'll be unable to manage the posts which will only work if the posts.tpl page directs me to view_post.php page. I need both of this work .i.e I want to display the postings details on each click on the title in the myblog.php page itself and also I'll have manage my blog by visiting the view_post.php page. So, please guide me on how to do this with the same posts.tpl page.

 

I know what I have asked is a lot but I hv nowhere else to go for help and I have written quite a long thread for my need to understood properly. Please help this newbie to learn and improve.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.