hansman Posted May 21, 2006 Share Posted May 21, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10121-dynamic-links-using-auto_increment-id/ Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 assuming:tablename = your table nameid = the column name you are sorting by title = your column name for the titlelistlinkies.php is where you list the linkies.showdetails.php is where you show the details of your entries or whateverin 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. Quote Link to comment https://forums.phpfreaks.com/topic/10121-dynamic-links-using-auto_increment-id/#findComment-37712 Share on other sites More sharing options...
hansman Posted May 21, 2006 Author Share Posted May 21, 2006 thanks for the help, i have a little problem thoughon 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? Quote Link to comment https://forums.phpfreaks.com/topic/10121-dynamic-links-using-auto_increment-id/#findComment-37728 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10121-dynamic-links-using-auto_increment-id/#findComment-37741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.