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

 

Link to comment
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']

 

 

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. :)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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! :)

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.