Jump to content

Fetching data with links


musnoure

Recommended Posts

Hi guys,

I am using a search form that's supposed to pull data out of database and display them. I managed to to do that using the following:

 

while($rows = mysql_fetch_assoc($result))

{

 

echo "-Title: " . $rows['title'] . "<br/>" . "-Author: " . $rows['author'] . "<br />";

 

Displays something like this:

-Title: Young One

-Author: Edwards Bonnie.

----------------------------------

 

I have links related to each book in a folder and I want to make the title clickable and each title should take me to the corresponding book page.

 

I know that I have to do something like <a href=\"...........\">$rows['title']</a>", but as far as I know all titles will take me to one page using that! please help and thank you.

Link to comment
https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/
Share on other sites

Hey  monsure,

 

You should create one dedicated page of book_details.php and whenever you display overview page with title and author give a link to that particular page, see below code

 

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

echo "-Title: <a href='book_details.php?id=".$rows['id']."'>" . $rows['title'] . "</a><br/>" . "-Author: " . $rows['author'] . "<br />";
}

On book_details.php page fetch a bookdetails from $_GET['id'] variable.

 

Query should be

 

$sql = "select * from book where title_id =".$_GET['id']

 

Hey  monsure,

 

You should create one dedicated page of book_details.php and whenever you display overview page with title and author give a link to that particular page, see below code

 

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

echo "-Title: <a href='book_details.php?id=".$rows['id']."'>" . $rows['title'] . "</a><br/>" . "-Author: " . $rows['author'] . "<br />";
}

On book_details.php page fetch a bookdetails from $_GET['id'] variable.

 

Query should be

 

$sql = "select * from book where title_id =".$_GET['id']

 

 

Just a security note; as users can easily modify the id value and therefore do SQL injection, I strongly suggest doing like below:

 

$id = mysql_real_escape_string($_GET['id']);
$sql = "select * from book where title_id = " . $id;

 

You probably already know this, but just making sure he does not make this mistake. :)

Thank you so much chintansshah for the response!

I tried it and it worked, but all links take me to book_details.php. Is there anyway to make different links for each title. Let's say I have pages named book1.php, book2.php, book3.php, etc. Assuming book1.php is the page that should be loaded when I click on a book title called Angles. I just don't want all titles to take me to the same page (book_details.php), each has to be linked to the corresponding page.

 

I appreciate you taking the time out to write back, thanks buddy!

Thank you so much chintansshah for the response!

I tried it and it worked, but all links take me to book_details.php. Is there anyway to make different links for each title. Let's say I have pages named book1.php, book2.php, book3.php, etc. Assuming book1.php is the page that should be loaded when I click on a book title called Angles. I just don't want all titles to take me to the same page (book_details.php), each has to be linked to the corresponding page.

 

I appreciate you taking the time out to write back, thanks buddy!

 

You would need a field in the database to reflect this, if you really wanted to do it that way.  (time consuming IMO)

 

Do as stated above by chintansshah, use the page.php?id=ID and then that page can grab all the information from the database where the id = the id you have selected.

 

You should notice with all your links that the ?id=X are all different.

 

-Pengu

I didn't read what you are doing too much, but I hope the below will be useful anyways (the principle).

 

You could put a column in your current table (I will assume that it is named "Booking") named something like "pageId" and then make a new table named "Page" with the following columns: pageId, pageName. You should of course add more if you find some useful - a description for instance. Then add a foreign key from Booking.pageId to Page.pageId. So, each row in your "Booking" table has a reference to a Page row. This means that you can have different pages for different bookings. So, you should be able to do something like this:

 

$query = "SELECT b.field1, b.field2, b.field3, b.pageName FROM Booking b, Page p WHERE b.someUniqueField = 'someValue' AND p.pageId = b.pageId";

// Some code omitted

while($rows = mysql_fetch_assoc($result))
{
      echo '-Title: <a href="' . $rows['pageName'] . '.php" alt="' . $rows['title'] . '">' . $rows['title'] . '</a><br/>-Author: ' . $rows['author'] . '<br />'; 
}

 

Excuse me if I made some simple syntax mistake above. But, now you should be able to associate a given booking row with a page and link to that page.

 

Good luck! :)

Archived

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

×
×
  • 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.