Jump to content

Archived

This topic is now archived and is closed to further replies.

hansman

Dynamic Links Using Auto_Increment ID

Recommended Posts

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

Share this post


Link to post
Share on other sites
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:
[code]
$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>";
}
[/code]

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
[code]
<?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'];
?>
[/code]

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:

[code]
$conn = mysql_connect('localhost','username','password');
$db = mysql_select_db('databasename');
[/code]

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

Share this post


Link to post
Share on other sites

×

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.