Name: BigShow Date: April 29, 2008 at 08:00:52 Pacific Subject: using links to pull from mysql OS: xp CPU/Ram: pentium Model/Manufacturer: dell
Comment:
This is definately my toughest program yet. I have been fooling around with this thing for almost 2 weeks and have gotten no where. If it can't be done, I am hoping someone can tell me that so I can move on.
Here it is...I have a page called book that when it loads it is populated with the bookcovers from a database. Each cover obvously represents a book. There are several series in this collection. I need to make it so when someone clicks on a book cover it takes them to a page that shows that cover, some info on it then an excerpt from that book. I created the template for that page nad just figured the best way is to populate the template with whichever book and info the user clicks.
Problem...someone will be clicking an image link, not a submit button so I really cant use a form to extract the info and populate the template. How do I manage to do this? I have been trying to use Javascript onclick() to create a link with an id from the book and open up a page accordingly but I am getting no where. I was going to use Ajax but then that eliminates the back button and that is important to have for this particular section.
You are making it much harder than it needs to be (hmm, this appears to be a pattern for you - lol). I am assuming you are using PHP on this project as well.
Anyway, on the page that displays the book covers you just need to create standard HTML links of the images pointing to the template page and add a parameter to the URL. You can then access the parameter through the $_GET array, just as you would do with $_POST data (FYI: You know you can set a form's METHOD to either POST or GET, right? Not that we are using a form here, but just for future info)
I will assume that the data for each book is in a database. So, let's say the page that show's an individual books data is "showbook.php". You would create a link that looks something like this: showbook.php?id=5
On that page you would simply get the value for $_GET['id'] (in this case 5) and then do a database query for the book record that has an id of 5.
EDIT: Now that I think of it you *could* use form as well. Just create many mini-forms for each book cover with the image as the submit button. However, that would be ineficient in my opinion.
Your right Michael, I need to start giving more thought to it before I post. I have actually started doing what you mentioned just before you answered. Honestly I dont even think that is what I need help with, the part that is over welming me is the script used to make this happen. Let me explain.
Basically there are 7 series of books, each series has X amount of books. I need to first create a page that show all of the book covers, which I will call books.php. I have create two databases, one called book_titles and the other book_info. The book titles has the titles and an ID, the books_info has the titles, series, excerpts and some other tid bits about the book.
Basically on the books page when it opens I want it to pull all the books out of the database and display them across the page in rows of seven, after one row finishes the line automatically breaks and it goes to the next line and continues. This also has to be sectioned off in series so once the series ends it goes to the next line, prints the new series name then goes to the next line and continues showing those book covers. This is why I have 2 tables in the DB for this. I figure I can take all the series names from the forst db, basically write it so it takes all the bookcovers from the first series shows them then goes back and gets the next series name from the table, compares that to the book_info table and prints all those out so on and so forth.
I dont think this is complex but I am overwhelmed trying to make it work, this will involve a few different loops.
Does this seem like something that can be done or should I approach it another way.
Yes, it can be done. I don't understand your logic for the two tables. If all the data has a 1-to-1 relationship put all the data into one table. If there is a many-to-one relationship then you need two tables.
Here is some mock code:
<?php
$query = "SELECT id, title, series, cover FROM books ORDER BY series, title";
$result = mysql_query($query) or die (mysql_error());
$columns = 7; $current_series = '';
while ($record = mysql_fetch_assoc($result)) {
//For new series complete last row if not first column if ($current_series != $record['series']) { if ($current_column > 1) { for ($col=$current_column; $col<=$columns; $col++) { echo "<td> </td>"; } echo "</tr>"; }
Hey Michael, I figured out a different way. I created a wrap in css and put in individual cells the size of the covers and made them all float left, i through in the clearfix class adn as the row gets too big it automatically shoots down to the next row. Works great.
Also, Instead of one table I created a table for each series that way each series has ts own div in the code so there doesnt have to be a dozen ifs in my while loop.
Next question, I am running into a syntax error. I know this is right but it wont work. I have a table called 'dark_hunter', in that table i have a cloumn called 'book_title'. When someone clicks the bookcover it brings them to the 'bookdisplay' page where the book title and series is appended to the URL, I GET them and use that in my DB query.
This is my query
$result = mysql_query("SELECT * FROM $bookseriesfordisplay WHERE book_title = `$booktitlefordisplay`")
$bookseriesfordisplay is the series and $booktitlefordisplay is the title. I know these work because I did a little checking and echoed them out so I know the correct info is passing. Here is the error..
I would guess that the values for $bookseriesfordisplay and $booktitlefordisplay are swapped. One bit of advice - never "set" your queries direcrectly in the mysql_query() command. Instead save the query to a variable so you can echo it to the page if somethign gioes wrong for debugging purposes (and also use appropriate debugging code to exit gracefully from an error). Like this:
$query = "SELECT * FROM $bookseriesfordisplay WHERE book_title = `$booktitlefordisplay`"
$result = mysql_query($query);
if ($result == false) { echo "The query:[br /]$query[br /]"; echo "Produced the error:[br /]".mysql_error());
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE