April 5, 200917 yr Hi all, I am helping an animal charity to redesign their website and to move to a database-driven set-up. I have a database with details on 170 dogs the charity is trying to get rehoused. Thanks to help from snowyswebsolutions and dddougal in this forum I have got to a position where I can display the name and photo of each dog as a clickable link on a page. The idea is for the visitor to click on a dog they like and be taken to a second page where they get all the facts and figures on that dog. Obviously, I don't want to have 170 html pages each with data on a different dog. What I need is a way for the name of a dog (or the name of its photo) stored in an array on page 1 to become one of the search criteria in a database search on page 2. Page 2 is just a template with a table set up up to echo that dog's full information. The relevant bit of code on Page 1 is: // sending query $results = mysql_query("SELECT * FROM $table WHERE type= 'dog' AND category = 'small' ORDER BY name"); if (!$results) { die("Query to show fields from table failed"); } while($results_array = mysql_fetch_array($results)) { $id = $results_array["id"]; $image = $results_array["photo1"]; $name = $results_array["name"]; echo "<div class=\"dog_box\">\r"; echo "\n"; echo "<p class=\"name\">".$name."</p>\t\r"; echo "\n"; echo "<a href=\"http://localhost/xampp/sosoldsite/dogfactstemplate.php\"> <p class=\"dog_img\"> ". $image ." </p></a>\t\r"; echo "\n\t"; echo "</div>"; } echo "<div class=\"clear\"></div>"; Thus, clicking on any of the photos takes the user to a page called dogfactstemplate.php. At the moment, on that page I have the following php - really just to make sure a search and table all work. The query is not correct, because it echoes ALL the dogs in the relevant category, not just the one the viewer wants to see: // sending query $result = mysql_query("SELECT category, name, weight, height, age, gender, colour, breed, history, disposition, photo1, photo2 FROM sosanimals WHERE type= 'dog' AND category='small' ") or die(mysql_error()); $fields_num = mysql_num_fields($result); echo "<table width='500px' background-color:transparent; cellpadding=5 > <tr>"; echo "<td width='200px' align=left <font color=#000000 size=3> Basics</font></td>"; echo "<td width='100px' align=left <font color=#000000 size=3> History</font></td>"; echo "<td width='200px' align=center <font color=#000000 size=3> </font></td>"; echo "</tr>\n"; // printing table rows $id = "id"; $category = "category"; $name = "name"; $weight = "weight"; $height = "height"; $age = "age"; $gender = "gender"; $colour = "colour"; $breed = "breed"; $history = "history"; $disposition = "disposition"; $photo1 = "photo1"; $photo2 = "photo2"; while($row = mysql_fetch_array($result)) { echo "<td align=left valign=top> Size: $row[$category]<br><br> Weight: $row[$weight]<br><br> Height: $row[$height]<br><br> Age: $row[$age]<br><br> Gender: $row[$gender]<br><br> Colour: $row[$colour]<br><br> Breed: $row[$breed]<br></td>"; echo "<td align=left valign=top> $row[$history] <br><br> $row[$disposition] <br> "; echo "<td align=left valign=top> $row[$photo1] </td>"; echo "</tr>"; } mysql_free_result($result); ?> </table> Can anyone help me to amend the query on the second page so that, for example, if the viewer clicks on an image called "pluto.jpg" on the first page, that becomes one of the search criteria to bring up Pluto's facts on page 2? TIA for any help. RJ
April 5, 200917 yr Hi you may need to GET the information from the link eg <a href="next_page.php?size=small">small</a> then your php would be <?php $size=$_GET['size']; ?> then select from your table in the databace where size='$id' this is one method hope this helps you Edited February 2, 201016 yr by Sam G added [code] tags
April 5, 200917 yr Author Hey - thanks Elite for the quick response. I am a bit of a noob at php (OK with html and css) so it may take me a bit to understand exactly what has to go where. But give it a go I will
April 5, 200917 yr Author Hi you may need to GET the information from the link eg <a href="next_page.php?size=small">small</a> then your php would be <?php $size=$_GET['size']; ?> then select from your table in the databace where size='$id' this is one method hope this helps you Hi Elite. Well, the problem with only half understanding your advice is I get zero output. The problem is with trying to change your code to fit my variables. Perhaps if I give a bit more detail you could help contextualise your code for my situation? On my "Page1" I have the line: echo "<a href=\"http://localhost/xampp/sosoldsite/dogfactstemplate.php\"> <p class=\"name\">".$name."</p>\t\r"; Clicking the name will take the user to a page where I want to display more information about a particular dog. The variable I want to carry over to the next page is the one created by the line: $name = $results_array["name"]; name is the field name in my table for the dog's name and is the variable I want to carry over to Page 2 and search with the line: $result = mysql_query("SELECT category, name, weight, height, age, gender, colour, breed, history, disposition, photo1, photo2 FROM sosanimals WHERE type= 'dog' ") or die(mysql_error()); I'll carry on trying to change these lines to produce the right output, but if you have a moment to help convert your code to my variables I would be very grateful Cheers Edited February 2, 201016 yr by Sam G added [code] tags
April 6, 200917 yr Author OK - I've cracked this problem The line for the link on my Page 1 (using my variables) should look like: echo "<a href=\"http://localhost/xampp/sosoldsite/dogfactstemplate.php?name=$name\"> <p class=\"name\">".$name."</p>\t\r"; And the GET statement on my Page 2 looks like: $name=$_GET['name']; Then the query includes this variable, so: $result = mysql_query("SELECT category, name, weight, height, age, gender, colour, breed, history, disposition, photo1, photo2 FROM sosanimals WHERE type= 'dog' AND name='$name' ") or die(mysql_error()); This seems to work fine. Many thanks to Elite for the generic code - just took a while to figure out how to translate it
April 6, 200917 yr Why are you using get? To allow the users to bookmark their form submissions? Seems a lil ilogical.
April 6, 200917 yr Author Why are you using get? To allow the users to bookmark their form submissions? Seems a lil ilogical. I'm open to suggestions as to a simpler or more elegant or whatever method. In a nutshell, this is what I want to do. I have a page of dogs photos displayed together with their names on, say, the "dogs for rehousing" page. These are produced via a database query when a user clicks on a link to that page. These images/names are displayed as clickable links. A person wanting to know more about a particular dog will click on that dog's image/name. This triggers a fuller search of the database for more info on the dog and it gets displayed on another page. There are no forms involved anywhere... Is that enough info? RJ
April 6, 200917 yr Why are you using get? To allow the users to bookmark their form submissions? Seems a lil ilogical. GET would be the standard way of accessing a database record through the URL. If you were to use POST instead, the user would not be able to bookmark the dogs seperate page or send the link anywhere else. Also upon refreshing the page for whatever reason, the user would always be prompted to "resend form data" which is bad user experience. Stick with how it's always been done and what GET was designed for. POST should only be used for private data for example a user login or input forms such as registration forms.
April 7, 200917 yr Author GET would be the standard way of accessing a database record through the URL. If you were to use POST instead, the user would not be able to bookmark the dogs seperate page or send the link anywhere else. Also upon refreshing the page for whatever reason, the user would always be prompted to "resend form data" which is bad user experience. Stick with how it's always been done and what GET was designed for. POST should only be used for private data for example a user login or input forms such as registration forms. Thanks for the advice. I am quite happy with the GET procedure - it is just that I am only learning about php at the moment, so always open to advice and suggestions about the best way to do things. RJ
April 7, 200917 yr Strictly speaking i would usually send the id number of the record within the url and not the text of what it is, if that makes any sense. Its much faster to retrieve numbers than it is to retrieve text from mysql, especially if you make an index on the id and its easier to check for legit requests on the mysql retrieving page. Just my way of doing things, others might disagree. Ste
Create an account or sign in to comment