Create page with PHP from MySQL?
How can i do this? My current main PHP page lists all my information from my database. db_one table_books: 1 - Title - Description - Image - Link 2 - Title - Description - Image - Link 3 - Title - Description - Image - Link 4 - Title - Description - Image - Link Like so. But the" Link" is the same for all. I want to be able to click the "Link" and have the web page be created from that rows information. Using a basic page template, the information would be filled in: ---------------------------------- "Title" display "Image" type "Description" --------------------------------- So i basically want 1 template web page to change based on which row i choose. So that way i don't have to go and make several hundred individual web pages for each item. Anyone know how? Oh yeah, I'm pretty new with PHP an MySQL. So the simpler, the better.
Public Comments
- You need to create a page that dynamically generates a template. The easiest way to do this is to pass the information in the URL. PHP lets you pass variables through a URL as follows: index.php?title=test&description= this%20is%20a%20description&image= test.jpg Then, the index.php page has access to all of these variables. Notice how spaces are replaced with "%20" There is a PHP function that does this for you called urlencode(). First generate the links in your main page echo "<a href= 'template.php?title=" . urlencode( $title ) . "&description=". urlencode( $description ) . "&image=" .urlencode( $image ) . "'>Link Text</a>"; Then, create the template.php page to process this. You get the variables from the URL using the $_GET variable. For example, to get the title from the URL, you would use: $title = $_GET['title']; Here's an example template.php page. Notice how I use the urldecode() function. <?php $title = urldecode( $_GET['title'] ); $description = urldecode( $_GET['description'] ); $image = urldecode( $_GET['image'] ); echo "<h1>" . $title . "</h1>"; echo "<img src='" . $image . "'>"; echo "<p>" . $description . "</p>"; ?> I Hope this helps. Good luck with PHP and MySql.
- I agree with jdorndog but I would make the following modification; I would only pass one unique id through the URL since the length of a URL is limited. Then you can use that unique id to access the row in MySQL from the template PHP file. Probably a PRIMARY AUTO_INCREMENT column would be the best bet for a unique id. I notice you have a column with 1, 2, 3, 4, etc. If that is a unique identifier for each row then I would pass that through the URL. e.g. your URL would look like this: http://localhost/template.php?id=4 then in template.php you would get the value of id, do some error prevention, and then load the row from your table, probably like this (pseudo code): if ( is_set( $_GET['id'] ) ) { $itemid = $_GET['id']; } if ( !is_set( $itemid ) ) { DEFAULT OUTPUT IF NO ID SUPPLIED } else { $itemid = mysql_real_escape_string($itemid); MYSQL_CONNECT; MYSQL_QUERY(' SELECT FROM SOMEDB WHERE ID = $itemid' ); FILL IN AND DISPLAY TEMPLATE } ?> Notice the mysql_real_escape_string, you should always pass any data that can be modified by the user (like URL variables) through that to prevent a SQL injection attack. [edit] You'll notice how Yahoo Answers uses this type of approach. It doesn't pass the whole question through the URL when you click on a link to a question, rather it uses qid=SOMENUMBER that identifies the question that should be displayed. [edit again] If you really want to take advantage of templates look into Smarty. That's what I usually use as a template engine, it lets you keep your PHP code and HTML data separate. http://www.smarty.net/
- One big mistake of the answers above is the use of GET! GET makes data visible in the address-bar and, hence, can be modified by the visitor. Avoid at all costs. Since your "main" shows a list of images (or data), and you have a button (some clickable object) for each, make it a FORM you send to "second.php", with the method "POST". Your second.php receives a POST. Read it, and dispaly whatever you need: $image = $_POST [ ' image ' ]; $title = $_POST [ ' title ' ]; etc...
Powered by Yahoo! Answers