Jump to content

creating pages with the get function PHP


laff1975

Recommended Posts

Hi all,

 

I hope someone can help me as I’m so close to completing a 2 month project that has involved learning PHP from scratch.

 

I have my mysql database set up and can currently add articles. Each mysql record has its own unique id and using PHP I can currently echo the article title, date and article synopsis. I then have a read more link and I need it to display the whole article that is associated to that id. All I need to do is now create one link that links to one PHP page to displays the whole article for that id.

 

I know you have to query the database and use the ? id=$id in the link to then display the article which is associated to that specific read more link. But I’m not sure how this completely works! I'm very new to PHP so please excuse if I have mentioned anything that seems stupid or obvious!

 

Hope someone is able to help

 

Thanks you

Steve

 

You can create a list of articles using code that approximates to...

 

$result = mysql_query("SELECT * FROM articles");
while($article = mysql_fetch_assoc($result)) {
   echo '<a href="/article.php?id=' . $article['id'] . '" />' . $article['title'] . '</a><br/>';
}

Then in article.php you can use something like...

 

if(isset($_GET['id'] && is_numeric($_GET['id'])) {
   $result = mysql_query("SELECT * FROM articles WHERE id={$_GET['id']} LIMIT 1");
   $article = mysql_fetch_assoc($result);
   echo "<h2>" . $article['title'] . "</h2>";
   echo "<p>" . $article['body_text'] . "</p>";
}

If you wanted you could put the first block of code in and else block for the second block, so that if the article isn't found or id= isn't in the URL then a list of articles is displayed.

 

Disclaimer: This was just a simple example the code isn't perfect nor does it entirely use best practices.

 

 

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.