joshgarrod Posted January 27, 2008 Share Posted January 27, 2008 hi all, i have a script that displays a list of articles, they are displayed in order of ID and are listed by the artile title. they are displayed as inks to link to each individual page, but this is not practical as i would need to manually create a page for each, is there a way that i can list the articles, then depending on which article the user selected pull the data from the table onto aother page displaying only that article. Any ideas? please help, thanks in advance. <?php $con = mysql_connect("localhost","user","pword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $query = "SELECT * FROM `Animals` ORDER BY `ID`"; $result = mysql_query($query); $num=mysql_num_rows($result); $i=0; while ($i < $num) { $cat=mysql_result($result,$i,"Article"); $url="index.php?page="; echo '<tr><td width=5></td><td align=center><a href="index.php?page='.$cat.'">'.$cat.'</a></td></tr>'; ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/ Share on other sites More sharing options...
phpSensei Posted January 27, 2008 Share Posted January 27, 2008 You need to create a new page lets say called "articles.php", and link $cat to it with the URL "?page=$cat", then use that ID to show a different data on the page depending on the page id. <?php $page = $_GET['page']; // This will be the ID of the page $page = mysql_real_escape_string(strip_tags($page)); $query = mysql_query("SELECT * FROM table WHERE id_of_page = '$page'"); if(mysql_num_rows($query)==0){ die("Sorry, but that page does not exists"); }else{ while($row = mysql_fetch_array($query)){ echo $row['column_1'] . "<br>"; echo $row['column_2'] . "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450527 Share on other sites More sharing options...
joshgarrod Posted January 27, 2008 Author Share Posted January 27, 2008 ok, so i change 'page' to the name of my id field and the other changes to be made and put it at the end of the script, does this then load within that page on what i have scripted already? Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450528 Share on other sites More sharing options...
phpSensei Posted January 27, 2008 Share Posted January 27, 2008 ok, so i change 'page' to the name of my id field and the other changes to be made and put it at the end of the script, does this then load within that page on what i have scripted already? Place this script in a new file called article.php, or you can place it in your same script. Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450532 Share on other sites More sharing options...
phpSensei Posted January 27, 2008 Share Posted January 27, 2008 Do you want to show a certan page for the Animal's ID? or do you have them stored in another table, and you want to show the data of the table depending on the page id? <?php $con = mysql_connect("localhost","user","pword"); if (!$con) { die('Could not connect: ' . mysql_error()); } if(isset($_GET['page'])){ $page = $_GET['page']; // This will be the ID of the page $page = mysql_real_escape_string(strip_tags($page)); $query = "SELECT * FROM Animals WHERE ID = '$page'"; if(mysql_num_rows(mysql_query($query))==0){ die("Sorry, but that page does not exists"); } } else { $query = "SELECT * FROM `Animals` ORDER BY `ID`"; } mysql_select_db("db", $con); $result = mysql_query($query); $num=mysql_num_rows($result); $i=0; while ($i < $num) { $cat=mysql_result($result,$i,"Article"); $url="index.php?page="; echo '<tr><td width=5></td><td align=center><a href="index.php?page='.$cat.'">'.$cat.'</a></td></tr>'; ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450533 Share on other sites More sharing options...
joshgarrod Posted January 27, 2008 Author Share Posted January 27, 2008 the artiles are stored in the 'animals' table. i just want to display the information of each article without having to create pages for each manually. Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450567 Share on other sites More sharing options...
phpSensei Posted January 27, 2008 Share Posted January 27, 2008 the artiles are stored in the 'animals' table. i just want to display the information of each article without having to create pages for each manually. Then do as i said ealier use a file call article.php and put this in it. Make the $cat links to this page instead of index.php?page= <?php $page = $_GET['page']; // This will be the ID of the page $page = mysql_real_escape_string(strip_tags($page)); $query = mysql_query("SELECT * FROM Animals WHERE `ID` = '$page'"); if(mysql_num_rows($query)==0){ die("Sorry, but that page does not exists"); }else{ while($row = mysql_fetch_array($query)){ echo $row['column_1'] . "<br>"; echo $row['column_2'] . "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450582 Share on other sites More sharing options...
joshgarrod Posted January 27, 2008 Author Share Posted January 27, 2008 ok thanks, but so i know, how does it know which link has been click on the previous page. also is this practical for search enges, what i mean is will they still crawl all of my pages if there is still a link to the page? Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450606 Share on other sites More sharing options...
phpSensei Posted January 27, 2008 Share Posted January 27, 2008 ok thanks, but so i know, how does it know which link has been click on the previous page. also is this practical for search enges, what i mean is will they still crawl all of my pages if there is still a link to the page? If search engines crawl your site, they will practically go through links and stuff too, I think. Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450613 Share on other sites More sharing options...
joshgarrod Posted January 27, 2008 Author Share Posted January 27, 2008 Hi, it still doesnt work, and its displaying the ID number as the link not the article name. at the moment its linking to 'idnumber.php'. i need to keep the php navigation within the linking system as it displays a page within a table. here are the two scripts again: Article list: <?php $con = mysql_connect("localhost","fgn","sfnfnfg"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dfsf", $con); $query = "SELECT * FROM `Animals` ORDER BY `ID`"; $result = mysql_query($query); $num=mysql_num_rows($result); $i=0; while ($i < $num) { $ArtID=mysql_result($result,$i,"ID"); $Article=mysql_result($result,$i,"Article"); $url="index.php?page="; echo '<tr><td width=5></td><td align=center><a href="index.php?page='.$ArtID.'">'.$ArtID.'</a></td></tr>'; ++$i; } ?> View article script: <?php $con = mysql_connect("localhost","sggm","sfdhnj"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("sdfg", $con); $page = $_GET['ID']; // This will be the ID of the page $page = mysql_real_escape_string(strip_tags($page)); $query = mysql_query("SELECT * FROM Animals WHERE `ID` = '$page'"); if(mysql_num_rows($query)==0){ die("Sorry, but that page does not exists"); }else{ while($row = mysql_fetch_array($query)){ echo $row['Article'] . "<br>"; echo $row['Text'] . "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450620 Share on other sites More sharing options...
phpSensei Posted January 28, 2008 Share Posted January 28, 2008 You placed the name twice in the same place, for the name and link... Your script would be better like this: Also, I made a mistake, its suposed to be $_GET['page'] not ID try: <?php $con = mysql_connect("localhost","fgn","sfnfnfg"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dfsf", $con); $query = "SELECT * FROM `Animals` ORDER BY `ID`"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $ArtID=$row['ID']; $Article=$row['Article']; echo '<tr><td width=5></td><td align=center><a href="idnumber.php?ID='.$ArtID.'">'.$Article.'</a></td></tr>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88058-solved-link-to-one-file-but-change-results-depending-on-what-link-is-clicked/#findComment-450856 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.