Creating individual pages based on ids.
Moderator: General Moderators
Creating individual pages based on ids.
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?
Any ideas?
Re: Creating individual pages based on ids.
select * from table where id='currentid'
That should help
That should help
Re: Creating individual pages based on ids.
But doesn't that mean I have to specify the ID and have a different script for each blog post submitted?
Re: Creating individual pages based on ids.
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";
Re: Creating individual pages based on ids.
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....
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><?
?>Re: Creating individual pages based on ids.
Any ideas?
Re: Creating individual pages based on ids.
I already posted a code example that demonstrates how to accomplish retrieving a record based on a get variable.
Re: Creating individual pages based on ids.
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.
Re: Creating individual pages based on ids.
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;
}
Re: Creating individual pages based on ids.
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:
You can see the page live here.
Thanks guys.
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><?
?>
Thanks guys.