Link to full article in database from a link

  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 27th, 2006, 8:02 pm

I have 3 articles in a database and a page with code that displays the article title and a short description via php. What I'm trying to do is have it so that a user can click on the articles title as a link (like an html href) and it will take the user to the full article. I want to use php and a mySQL database so that I don't have to manually code in each article's title as a link, especially when I add new articles to the database.

How would I go about doing this?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 27th, 2006, 8:02 pm

  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 28th, 2006, 8:56 am

Assuming that you have an id field

PHP Code: [ Download ] [ Select ]
 
echo "<a href='article.php?id={$rows['article_id']}'>{$rows['article_title']}</a>";
 
 
  1.  
  2. echo "<a href='article.php?id={$rows['article_id']}'>{$rows['article_title']}</a>";
  3.  
  4.  
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 28th, 2006, 3:45 pm

Ok thanks. Yeah I don't have an id field because I didn't know if it automatically kept a record like entry 1-whatever.

So how can I make it automatically assign an id number to each article when I enter it into the database with a form (if it doesn't already)? And how do I automatically assign that database id to each individual article title so that it can be called via the above method?

That way each article will automatically be numbered, and then I can fetch the article id and display it like you gave me above?

Thanks for that code by the way. I will keep working on it.
  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 28th, 2006, 6:32 pm

add an autonumber field to the table, it will take care of the numbering
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 28th, 2006, 7:52 pm

Ok, great! This is coming along pretty good. I figured out how to auto-increment my id field. Now all I need is code that recognizes what id was clicked on and displays only that id (aka that article).

For instance, when they click on the title of article 3, it takes them to a different page, but what code needs to be on that page in order to display only article 3 (or how does it recognize only article 3 has been clicked on).

Thanks for the help so far guys I appreciate it. I'm on the last leg now! lol.
  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 28th, 2006, 8:19 pm

something like
PHP Code: [ Download ] [ Select ]
 
<?php
 
//article.php
 
 
 
if(empty($_GET['article_id'])){
 
  echo "No article chosen. Please go back and click on the link";
 
  die();
 
}
 
 
 
//I would recommend against the below since it could be used against you.
 
//you should check the id value to ensure that its a number and do any
 
//other checks that you want to protect your site
 
 
 
$sql = "select * from articles where article_id = {$_GET['article_id']}";
 
 
 
//db stuff here
 
 
 
if ($result){
 
//show the article
 
 
 
}else{
 
echo "unable to locate article in archives. sorry";
 
die();
 
}
 
 
  1.  
  2. <?php
  3.  
  4. //article.php
  5.  
  6.  
  7.  
  8. if(empty($_GET['article_id'])){
  9.  
  10.   echo "No article chosen. Please go back and click on the link";
  11.  
  12.   die();
  13.  
  14. }
  15.  
  16.  
  17.  
  18. //I would recommend against the below since it could be used against you.
  19.  
  20. //you should check the id value to ensure that its a number and do any
  21.  
  22. //other checks that you want to protect your site
  23.  
  24.  
  25.  
  26. $sql = "select * from articles where article_id = {$_GET['article_id']}";
  27.  
  28.  
  29.  
  30. //db stuff here
  31.  
  32.  
  33.  
  34. if ($result){
  35.  
  36. //show the article
  37.  
  38.  
  39.  
  40. }else{
  41.  
  42. echo "unable to locate article in archives. sorry";
  43.  
  44. die();
  45.  
  46. }
  47.  
  48.  



[/php]
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 28th, 2006, 8:47 pm

Hmmm, it doesn't seem to be working.

I have this:
Code: [ Download ] [ Select ]
<?php
// Connecting to a database server
$dbh=mysql_connect ("localhost", "<username>", "<password>") or die ('I cannot connect to the database

because: ' . mysql_error());
mysql_select_db ("<my database>", $dbh);

// Connecting to a database 
$query=mysql_query("SELECT title, article, time, id FROM Articles ORDER BY `time` DESC LIMIT 0, 30");

if(empty($_GET['id'])){
  echo "No article chosen. Please go back and click on the link";
  die();
}


?>
  1. <?php
  2. // Connecting to a database server
  3. $dbh=mysql_connect ("localhost", "<username>", "<password>") or die ('I cannot connect to the database
  4. because: ' . mysql_error());
  5. mysql_select_db ("<my database>", $dbh);
  6. // Connecting to a database 
  7. $query=mysql_query("SELECT title, article, time, id FROM Articles ORDER BY `time` DESC LIMIT 0, 30");
  8. if(empty($_GET['id'])){
  9.   echo "No article chosen. Please go back and click on the link";
  10.   die();
  11. }
  12. ?>


And it is not displaying anything. Not even the "No article chosen...." part.

But it is getting late and I could be overlooking something simple. I'll just keep working on it and perhaps tomorrow I'll figure something out.
  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 29th, 2006, 6:07 am

is the above code you have for the page with the list or the full article?
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 29th, 2006, 12:44 pm

lostboy wrote:
is the above code you have for the page with the list or the full article?


the page with the full article.
  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 29th, 2006, 1:54 pm

PHP Code: [ Download ] [ Select ]
 
<?php
 
 
 
// Connecting to a database server
 
if(empty($_GET['id'])){
 
 
 
    echo  "No  article  chosen.  Please  go  back  and  click  on  the  link";
 
 
 
    die();
 
 
 
}
 
 
 
$dbh=mysql_connect ("localhost", "<username>", "<password>") or die ('I cannot connect to the database
 
 
 
 
 
because: ' . mysql_error());
 
 
 
mysql_select_db ("<my database>", $dbh);
 
 
 
 
 
// Connecting to a database  
 
 
 
$query=mysql_query("SELECT title, article, time, id FROM Articles where id={$_GET['id']} ORDER BY `time` DESC LIMIT 0, 30");
 
 
 
 
 
 
 
 
 
 
 
 
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.  
  5.  
  6. // Connecting to a database server
  7.  
  8. if(empty($_GET['id'])){
  9.  
  10.  
  11.  
  12.     echo  "No  article  chosen.  Please  go  back  and  click  on  the  link";
  13.  
  14.  
  15.  
  16.     die();
  17.  
  18.  
  19.  
  20. }
  21.  
  22.  
  23.  
  24. $dbh=mysql_connect ("localhost", "<username>", "<password>") or die ('I cannot connect to the database
  25.  
  26.  
  27.  
  28.  
  29.  
  30. because: ' . mysql_error());
  31.  
  32.  
  33.  
  34. mysql_select_db ("<my database>", $dbh);
  35.  
  36.  
  37.  
  38.  
  39.  
  40. // Connecting to a database  
  41.  
  42.  
  43.  
  44. $query=mysql_query("SELECT title, article, time, id FROM Articles where id={$_GET['id']} ORDER BY `time` DESC LIMIT 0, 30");
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. ?>
  59.  
  60.  
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 29th, 2006, 2:27 pm

Nope that didn't work either :(

What actually displays the article? Does the $_GET variable actually display the article, or do I need something else?
  • lostboy
  • Expert
  • Expert
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 511
  • Loc: Just north of Toronto
  • Status: Offline

Post October 29th, 2006, 3:28 pm

the get variable is the id of the article passed from the previous pages of listed articles...on that page you would have

PHP Code: [ Download ] [ Select ]
 
echo  "<a  href='article.php?id={$rows['id']}'>{$rows['title']}</ a>";
 
 
  1.  
  2. echo  "<a  href='article.php?id={$rows['id']}'>{$rows['title']}</ a>";
  3.  
  4.  


which should show the article title as the link
Lostboy

Cat, the other other white meat
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 29th, 2006, 4:36 pm

Yeah, I have that. But don't I need something that will display the article then on the other page? All the get variable does is grab the article id......so when it grabs the id wouldn't I need to use another command to show the article on the second page?

I don't know I'm just guessing?
  • Rapt0r
  • Student
  • Student
  • No Avatar
  • Joined: Jul 17, 2005
  • Posts: 95
  • Status: Offline

Post October 29th, 2006, 6:02 pm

YES! I got it finally. :)

I guess all I had to do was keep pretty much everything the same except for changing the line when connecting to a database to:

$query=mysql_query("SELECT title, article, time, id FROM Articles where id={$_GET['id']} ORDER BY `time` DESC LIMIT 0, 30");

like you said above. I was changing everything, but that's all I had to do. Of course, I kind of got lucky and don't really know why that worked but it does.

Thanks for the help, especially lostboy for giving me some code and helping me along. This is really what I was wanting to do so thank you.

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 180 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.