Jump to content

Using href


Finker92

Recommended Posts

Hi all,

I have written a piece of code querying the database to display the last ten posts from different users on a forum/blog. This is displaying the titles of the posts but I am unsure how to create a link (complete novice) to read the content of those posts. I get the syntax etc but can't really get my head around the target, if you catch my drift. I have created a new php file called recentposts but am unsure of how to take it from there. The code for both is below. The first piece of code works fine but when I click read more I am getting an undefined variable message on line 11 (I'm guessing there should be a link between the two pieces of code) and I am getting mysqli_fetch_array() expects parameter 1 to be mysqli_result on line 12. Any thoughts/hints/suggestions welcome. Thanks in advance.

 

<?php
$connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') //Make a connection to the database
or die(mysqli_connect_error("Could not connect to the server")); //If it doesn't connect give the error message
$latestposts= mysqli_query($connection,"SELECT author_id, title FROM blogposts ORDER BY timeofpost DESC LIMIT 9");//Query the database
while($displayposts=@mysqli_fetch_array($latestposts)){ //go and get the information
echo "{$displayposts['title']}</br>";
echo "<a href='recentposts.php'>Read more</a></br>";
} 
?>

 

And the recentposts.php is

 

<?php
$connection = @mysqli_connect('localhost','root','','BLOG_PROJECT')
or die(mysqli_connect_error("Could not connect to the server"));
$detail= mysqli_query($connection,"SELECT * FROM blogposts WHERE 'title', 'content', 'timeofpost' = $content");
while($singlepage = mysqli_fetch_array($connection,$detail)){
extract($singlepage);
echo "<h2>{$singlepage['title']}</h2></br>";
echo "{$singlepage['content']}</br>";
echo "{$singlepage['timeofpost']}";
}
?>

Link to comment
Share on other sites

Depending on how the url's work for the blog posts it should be fairly simple to link to them dynamically.

 

The PHP will just echo a static link with one or two dynamic components similar to:

 

<?php
echo '<a href="/blog/post/' . $blogpostid . '/">Read Blog Post</a>';
?>

Link to comment
Share on other sites

OK, so now I am getting undefined index errors. I feel that I probably need to use $_GET somewhere here but I don't know where I am going wrong. From reading around it seems to me I should probably use isset somewhere? If this is very obvious please be patient... everybody starts somewhere. Any help or suggestions appreciated.

<?php
$connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') //Make a connection to the database
or die(mysqli_connect_error("Could not connect to the server")); //If it doesn't connect give the error message
$latestposts= mysqli_query($connection,"SELECT author_id, title FROM blogposts ORDER BY timeofpost DESC LIMIT 9");//Query the database
while($displayposts=@mysqli_fetch_array($latestposts)){ //go and get the information
echo "{$displayposts['title']}</br>";
echo "<a href='recentposts.php?id=content'>Read more</a></br>";
} 
?>

 

and then the other bit. It is running as far as the echo but I think I am missing something earlier?

 

<?php
$connection = @mysqli_connect('localhost','root','','BLOG_PROJECT')
or die(mysqli_connect_error("Could not connect to the server"));
$content= mysqli_query($connection,"SELECT ('author_id''title''content') FROM blogposts ORDER BY timeofpost DESC LIMIT 9");;
while($singlepage = mysqli_fetch_array($content)){
extract($singlepage);
echo "<h3>{$singlepage['title']}</h3></br>";
echo "{$singlepage['content']}</br>";
echo "{$singlepage['timeofpost']}";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.