How to get a value from a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

How to get a value from a variable

Post by roma2509 »

Hi to all. I have a script that manage posts from my database. In this script I extract all posts from database. For each post It create two buttons, a button that delete post from database and other button that redirect to other page where the post can be edited. I do it in this lines from my script

Code: Select all

echo "<td><a href=\"edit_post.php?page=" . urlencode($page['id']) . "&post=" .
        urlencode($posts['id']) . "\"><img src=\"images/post_edit.png\" width=\"16\" height=\"16\" /></a></td>\n";
    echo "<td><a href=\"delete_post.php?post=" . urlencode($posts['id']) . "\"><img src=\"images/post_delete.png\" width=\"16\" height=\"16\" onClick=\"return confirm('Are you sure you wann delete this post. This action can not be undone!.');\"/></a></td>\n";


Now I need to obtain the value that it pass in the link, I mean I need the Id of post to use it on redirected page. How can I get it from my script in the redirected page? What I need to use?
The all code of script is:

Code: Select all

<?php
/**
 *
 * eliteCMS - The Lightweight CMS Copyright © 2008 elite-graphix.net.
 * eliteCMS is a free software to use for personal websites.
 * Anyone can use eliteCMS for free as long as he retain the
 * copyright note at the footer.
 * If anyone wants to remove the copyright note for commercial
 * use of the software can contact raj@elite-graphix.net.
 * eliteCMS comes with absolutely no warranty and is provided as-is.
 * raj - THE AUTHOR of the software or elite-graphix - THE COMPANY
 * will not be held responsible for any damage this software might cause.
 * You run this software at your own risk and agree not to hold author or company.
 * You are not allowed to make any profit from this software unless you
 * have a written authorization from the author to use the software for commercial use.
 *
 */
include ("includes/headerRefresh.php");
include ("includes/config.php");
include ("functions/functions.php");
require_once ("includes/session.php");
check_login();
get_settings();
include ("header.php") ?>
<div id="body">
<div class="box bigBox">
<h1>Manage CMS Posts</h1>
<?php
if (isset($_GET['deleted']) && $_GET['deleted'] == 1) {
    $successMSG = "The post has been deleted successfully !.";

}
if (isset($_GET['newpost']) && $_GET['newpost'] == 1) {
    $successMSG = "Post created successfully !.";

}
?>

<?php display_msg(); ?>
<a href="add_post.php" title="Add New Post" target="_self" class="addPageButton">Add New Post</a>
<?php
echo "<table cellspacing=\"0\" cellpadding=\"0\" id=\"pageList\">\n";
echo "<tr>\n";
echo "<th width=\"26%\" scope=\"col\">Page Name</th>\n";
echo "<th width=\"30%\" scope=\"col\"style=\"text-align:left; padding-left:40px;\">Post Title</th>\n";
echo "<th width=\"11%\" scope=\"col\">Post Order</th>\n";
echo "<th width=\"11%\" scope=\"col\">Published</th>\n";
echo "<th width=\"11%\" scope=\"col\">Edit Post</th>\n";
echo "<th width=\"11%\" scope=\"col\">Delete Post</th>\n";
echo "</tr>\n";
$i = 0;
$color1 = "#EEF7FD";
$color2 = "#FFFFFF";
$query = "SELECT * FROM posts ORDER BY page_id";
$result = mysql_query($query);
confirm_query($result);
while ($posts = mysql_fetch_array($result)) {
    $query = "SELECT * FROM pages WHERE id ={$posts['page_id']}";
    $result1 = mysql_query($query);
    confirm_query($result1);
    $page = mysql_fetch_array($result1);
    $i++;
    echo "<tr bgcolor=\"" . (($i % 2 == 0) ? $color1 : $color2) . "\">\n";
    echo "<td>{$page['menu_name']}</td>\n";
    echo "<td style=\"text-align:left; padding-left:5px;\">{$posts['title']}</td>\n";
    echo "<td>{$posts['position']}</td>\n";
    echo "<td>";
    if ($posts['active'] == 0) {
        echo "No";
    } else {
        echo "Yes";
    }
    echo "</td>\n";
    echo "<td><a href=\"edit_post.php?page=" . urlencode($page['id']) . "&post=" .
        urlencode($posts['id']) . "\"><img src=\"images/post_edit.png\" width=\"16\" height=\"16\" /></a></td>\n";
    echo "<td><a href=\"delete_post.php?post=" . urlencode($posts['id']) . "\"><img src=\"images/post_delete.png\" width=\"16\" height=\"16\" onClick=\"return confirm('Are you sure you wann delete this post. This action can not be undone!.');\"/></a></td>\n";
    echo "</tr>\n";

}
echo "</table>\n";
?>
</div>
</div>
<?php include ("footer.php") ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to get a value from a variable

Post by requinix »

roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

Re: How to get a value from a variable

Post by roma2509 »

Can you please explain me please how I do it? I read the source that you give me but I cant find anything. I need the ID of post from

Code: Select all

urlencode($posts['id'])
, not the post .
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to get a value from a variable

Post by requinix »

What I gave you will be the post ID, both in edit_post.php and delete_post.php (because they use the same "post=" in the URL). Though really you should be doing

Code: Select all

if (isset($_GET["post"])) {
    // id is (int)$_GET["post"]
} else {
    // no post ID given
}
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

Re: How to get a value from a variable

Post by roma2509 »

Super, Thank you for solution. It work all fine.
Post Reply