0

I'm not sure how well i'll be able to explain this, but here goes. I have a website for attractions. Let's say that one of my categories is Historical villages. When the user opens the Historical villages page he gets a list of villages displayed from the database. The way I display them is: Name plus a picture of the attraction. What I want to do is unable the user to click on of the villages (by making the name and picture a clickable link) and the user to be redirected to a page that will run a php script that will display more information from the database about the selected village. That way I will only have one page for all attractions that will display different information every time a user selects something different, instead of hardcoding all the pages.

This is my code displaying the lits of villages:

$sql = "SELECT `Name`, `Location`, `Description`, `Airport`, `imglink`, `pagelink` "
        . "FROM `attractions` "
        . "WHERE `Category`='HistV'";
$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_assoc($result)) {

    echo $row['Name'];
    echo "<img src='" . $row['imglink'] . "'>";
}

Do any of you have any suggestions on how to make this output a link and the make it run the PHP to show the users selection?

7
  • google doesn't bite if you search on it Commented Apr 22, 2014 at 12:46
  • You've said you want "a user to be redirected to a page" and then you say that you only have "one page". Do you mean have 1 page displaying all the villages, and then when they click on a village it sends them to another page which has all the attractions in that village? Commented Apr 22, 2014 at 12:49
  • 1
    @JohnConde — No need for Ajax. This only needs a link (possibly with a query string in the URL) Commented Apr 22, 2014 at 12:51
  • robobobobo Yes, exactly what I want! Commented Apr 22, 2014 at 12:51
  • ok well then as @JohnConde said there is no need for Ajax here, it's much simpler, you just need a page which will accept a parameter passed in from a query string. So your url would be something like /attractions.php?village=SmallVille this html.net/tutorials/php/lesson10.php should help Commented Apr 22, 2014 at 12:55

2 Answers 2

1

Your while condition changed to like this,

while ($row = mysql_fetch_assoc($result)) {
    /* For example , 
       $row['pagelink'] must contains the pagelink as belowed here
            /viewVillage.php?village_id=1
            /viewVillage.php?village_id=2 and so on.  */
     echo "<a href='" . $row['pagelink'] . "'>"
             .  $row['Name']  .
             . "<img src='" . $row['imglink'] . "'>
           </a>";
}

This will generate your list of villages like this,

<a href="/viewVillage.php?village_id=1">
   Village name 1
   Village Image 1
</a>

<a href="/viewVillage.php?village_id=2">
   Village name 2
   Village Image 2
</a>

<a href="/viewVillage.php?village_id=3">
   Village name 3
   Village Image 3
</a>

 .....

When you click on any of the link, it will redirected to viewVillage.php page. Now you can get the particular village using $_GET['village_id']

viewVillage.php

if(isset($_GET['village_id']]) && $_SERVER['REQUEST_METHOD'] == 'GET' ) {

    $villageId = $_GET['village_id'];
    // Then do your stuff over here
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but how do I make it so that when the user clicks on that the next page will run a php scrip that will know what the user clicked on and will display info abou the user selection?
For that, you need to send village_id using GET method on view page. Now I edited my answer with comment
0

On your current page

while ($row = mysql_fetch_assoc($result)) {
/* For example , 
   $row['pagelink'] should be a village id */
 echo "<a href='/attractions.php?village=" . $row['pagelink'] . "'>"
         .  $row['Name']  .
         . "<img src='" . $row['imglink'] . "'>
       </a>";

}

Now it would print something like

<a href="/attractions.php?vilage=2"> Vilage Name <img src="urltoimage"></a>

When you click on this link you will be sent to a file called "attractions.php"

Create this file in the same directory and it should have the following php in it

<?php 
$villageId = $_GET['village']; //this gets the id of the village from the url and stores
//it in a variable
//now that you have the id of the village, perform your sql lookup here
//of course you will have to fill this is, as I don't know your actual table fields and names
$sql = "SELECT * FROM Attractions WHERE villageID = `$villageID`";

 //now perform the query, loop through and print out your results
?>

Does this make sense?

1 Comment

robobobobo and Ranjith thank you guys so much for the help, it works perfectly!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.