musnoure Posted October 10, 2010 Share Posted October 10, 2010 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 More sharing options...
chintansshah Posted October 10, 2010 Share Posted October 10, 2010 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 https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1120746 Share on other sites More sharing options...
Andy17 Posted October 10, 2010 Share Posted October 10, 2010 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 https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1120860 Share on other sites More sharing options...
musnoure Posted October 10, 2010 Author Share Posted October 10, 2010 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 https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1120902 Share on other sites More sharing options...
musnoure Posted October 10, 2010 Author Share Posted October 10, 2010 Thanks Andy17!!!! I appreciate it!! Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1120910 Share on other sites More sharing options...
pengu Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1120977 Share on other sites More sharing options...
musnoure Posted October 11, 2010 Author Share Posted October 11, 2010 if it's time consuming like you said, which way you suggest I should do it? any help is appreciated!! Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121079 Share on other sites More sharing options...
BlueSkyIS Posted October 11, 2010 Share Posted October 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121098 Share on other sites More sharing options...
musnoure Posted October 13, 2010 Author Share Posted October 13, 2010 didn't work Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121677 Share on other sites More sharing options...
Andy17 Posted October 13, 2010 Share Posted October 13, 2010 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 https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121721 Share on other sites More sharing options...
musnoure Posted October 13, 2010 Author Share Posted October 13, 2010 Thanks a million Andy17!!!! I tried it and it worked :) :) :) :) :) :) :) :) :) :) Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121871 Share on other sites More sharing options...
Andy17 Posted October 13, 2010 Share Posted October 13, 2010 You're welcome - glad I could help. Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121874 Share on other sites More sharing options...
musnoure Posted October 13, 2010 Author Share Posted October 13, 2010 I appreciate you taking the time out to help Link to comment https://forums.phpfreaks.com/topic/215532-fetching-data-with-links/#findComment-1121879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.