Creating individual pages based on ids.

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
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Creating individual pages based on ids.

Post by nonso »

I am trying to better my understanding of php by building a very basic blog system. I made a form that inserts the data to the database and then displays it but when I want to do is be able to have one blog post on its individual page. Each new post is assigned its own id so I am guessing it is possible using their individual id numbers but I can't figure out how to do so without displaying all the blog posts in the database.

Any ideas?
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Creating individual pages based on ids.

Post by LanceEh »

select * from table where id='currentid'

That should help ;)
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Re: Creating individual pages based on ids.

Post by nonso »

But doesn't that mean I have to specify the ID and have a different script for each blog post submitted?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Creating individual pages based on ids.

Post by Benjamin »

viewblogpost.php?comment=12

Code: Select all

 
if (!preg_match('#^[\d]{1,12}$#', $_GET['comment'])) {
    header("Location: http://mysite.com/index.php");
    exit();
}
 
$query = "SELECT field, field2, field3 FROM table WHERE id = '{$_GET['comment']}' LIMIT 1";
 
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Re: Creating individual pages based on ids.

Post by nonso »

That works on its own but how can I display it so IF the post id is in the URL it will display its contents.

Here is what I want to integrate the code into....

Code: Select all

    $query = "SELECT * FROM blog_post ORDER BY `id` DESC LIMIT 8;";
    $result = @mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing shouts from the database.</p>');
    
    ?><ul><?
    while ($row = mysql_fetch_array($result)) {
    
        $ename = ($row['name']);
        $epost = ($row['post']);
        $eid   = ($row['id']);
                   
        echo('<h1><a href=/index.php?post='.$eid.'>'.$ename.'</a></h1><p>'.$epost.'</p>');
    
    }
    ?></ul><?
    
?>
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Re: Creating individual pages based on ids.

Post by nonso »

Any ideas?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Creating individual pages based on ids.

Post by Benjamin »

I already posted a code example that demonstrates how to accomplish retrieving a record based on a get variable.
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Re: Creating individual pages based on ids.

Post by nonso »

I know, but what I am saying is I want to query the blog posts if there isn't an id defined in the URL, but if there is query the other code that displays what is in that specific id. Unless I am missing something I cannot get it to work right, it either displays nothing when just looking that the page with no id or it displays everything but when I click on one of the blog titles it does nothing.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Creating individual pages based on ids.

Post by Inkyskin »

This for example, will assign a default value of 1 as the id if the comment variable isn't set. Is that what you mean?

Code: Select all

 
if(!isset($_GET['comment']) || $_GET['comment'] = ''){
    $_GET['comment'] = 1;
}
 
nonso
Forum Newbie
Posts: 12
Joined: Sat Mar 14, 2009 10:58 pm

Re: Creating individual pages based on ids.

Post by nonso »

This is close to what I want, two things though.

How can I have it not execute one query if another is being executed properly?

There seems to be a strange bug when I goto a page (say /index.php?blog=54) but there is a post with a lesser ID, that ID takes its place.

Here is the code:

Code: Select all

<?php
$self = $_SERVER['PHP_SELF'];
$ipaddress = ("$_SERVER[REMOTE_ADDR]");
include ('db.php');
$connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
 mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');
 
      if(isset($_POST['send'])) {
        if(empty($_POST['name']) || empty($_POST['post'])) {
            echo('<p class="error">You did not fill in a required field.</p>');
        } else {
 
            $name = ($_POST['name']); 
            $post = ($_POST['post']);
        
                $sql = "INSERT INTO blog_post SET name='$name', post='$post', ipaddress='$ipaddress';";
        
                    if (@mysql_query($sql)) {
                        header("Location: ../index.php");
                    } else {
                        echo('<p>There was an error updating the blog.</p>');
                    }
        }
    }
    
    if (!preg_match('#^[\d]{1,12}$#', $_GET['blog']))
    if(!isset($_GET['blog']) || $_GET['blog'] = ''){
    $_GET['blog'] = 1;
    }
    $query2 = "SELECT * FROM blog_post where `id` = '{$_GET['blog']}' LIMIT 1;";
    $query = "SELECT * FROM blog_post ORDER BY `id` DESC LIMIT 8;";
    $result = @mysql_query("$query") or die('<p class="error">There was an unexpected error getting the blog posts.</p>');
    $result2 = @mysql_query("$query2") or die('<p class="error">There was an unexpected error getting the blog posts.</p>');
 
    ?><ul><?
    while ($row = mysql_fetch_array($result)) {
    
        $ename = ($row['name']);
        $epost = ($row['post']);
        $eid   = ($row['id']);
                   
        echo('<h1><a href='.$self.'?blog='.$eid.'>'.$ename.'</a></h1><p>'.$epost.'</p>');
    
    } 
        while ($row = mysql_fetch_array($result2)) {                   
        echo('<h1>'.$ename.'</h1><p>'.$epost.'</p><br /><a href='.$self.'>back home</a>');
        }
    ?></ul><?
?>
 
You can see the page live here.

Thanks guys.
Post Reply