Jump to content

[SOLVED] link to one file but change results depending on what link is clicked


joshgarrod

Recommended Posts

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;
}
?>

Link to comment
Share on other sites

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>";
             
          }
             
     }

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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>";
             
          }
             
     }

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
             
          }
             
     }

?>

Link to comment
Share on other sites

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>';

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.