Jump to content


Photo

Dynamic Links Using Auto_Increment ID


  • Please log in to reply
3 replies to this topic

#1 hansman

hansman
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts

Posted 21 May 2006 - 01:47 PM

Ok, say i have 15 mysql entries in my database, all with unique ID numbers, titles, sections, and article fields, i want to get 5 of the latest titles with a the title as a link. This link will then send you to the full blog post. How could i create the hyperlink that changes as i add stuff to mysql?

Thanks in advance for your help

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 04:43 PM

assuming:

tablename = your table name
id = the column name you are sorting by
title = your column name for the title
listlinkies.php is where you list the linkies.
showdetails.php is where you show the details of your entries or whatever

in listlinkies.php you would do this:
$sql = "select * from tablename order by id desc limit 5";
$rs = mysql_query($sql);

while ($info = mysql_fetch_array($rs)) {
    echo "<a href='showdetails.php?id=" . $info['id'] . "'>" . $info['title'] . "</a>";
}

Basically you are selecting the last 5 entries from the table based on the highest 5 id numbers (note: most people use a date column and sort by date...). Then you do a loop that fetches each row and puts the row information in an array and builds a linkie. showdetails.php is your target script, and ?id= passes the id to it. Then in your showdetails.php, you would do something like this:

showdetails.php
<?php
   if (!$_GET['id']) { 
      header("Location: listlinkies.php"); 
      exit(); 
   } else { 
      $id  = $_GET['id'];
   }

   $sql = "select * from tablename where id = '$id'";
   $rs = mysql_query($sql);
   $info = mysql_fetch_array($rs);

   //example output
   echo "Your title is: ". $info['title'];
?>

First we check to see if there is an id value in the url. If there is not (example, if the user types blah.php in the address bar instead of clicking on the linkie) then it sends them to listlinkies.php

Then we select the information from the database based on the id that was passed, fetch the results, and (as an example) echo the title.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 hansman

hansman
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts

Posted 21 May 2006 - 05:34 PM

thanks for the help, i have a little problem though

on listlinkies.php i have this

<?php
$sql = "SELECT * FROM 'blog' ORDER BY 'id' DESC LIMIT 5";
$rs = mysql_query($sql);

while ($info = @mysql_fetch_array($rs)) {
echo "<a href='showdetails.php?id=" . $info['id'] . "'>" . $info['title'] . "</a>";
}
?>

the page is blank,

showdetails.php's code is this

<?php
if (!$_GET['id']) {
header("Location: listlinkies.php");
exit();
} else {
$id = $_GET['id'];
}

$sql = "SELECT * FROM 'blog' where id = '$id'";
$rs = mysql_query($sql);
$info = mysql_fetch_array($rs);

//example output
echo "Your title is: ". $info['title'];
echo "Section: ". $info['section'];
echo "Article: ". $info['article'];
?


any ideas to what im doing wrong?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 06:02 PM

is that the entire script for both of the files? if so then you need to establish a database connection first. i assumed that you already had that taken care of. you will need to add the following to the top of each one:

$conn = mysql_connect('localhost','username','password');
$db = mysql_select_db('databasename');

replacing username, password, and databasename with the database username and password, and the database name, of course.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users