Log in

View Full Version : PHP Index html link to populate other page



faketales
10-29-2011, 06:08 PM
Hey i currently have an index page , within this index page i have a navigation bar.

What i would like to do is when a button is pressed , for example whe the button called "Fiction" is pressed i would like my page to redirect to my other page book_category_list.php.

On this page i would like all the books with the category list called "Fiction" to populate.

But if i pressed the button called "Horror" i would like the page book_category_list.php to populate the page with the categorys "Horror".

my current solution is to have a number of different pages and have this line of code on there.

this code is on the book_category_list.php page


$sql = mysql_query("SELECT * FROM books WHERE category='Fiction' ORDER BY date_added DESC");

so i would like where it says 'Fiction" to grab the value of the html link from the index.php page.

is this possible?

bluewalrus
10-29-2011, 06:47 PM
You could do this a few ways, not sure how many categories you have...

index.php

<a href="book_category_list.php?cat=fiction">Fiction</a>
<a href="book_category_list.php?cat=Horror">Fiction</a>

book_category_list.php

<?php
if (!empty($_GET['cat'])) {
switch ($_GET['cat']) {
case 'fiction':
$sql = mysql_query("SELECT * FROM books WHERE category='Fiction' ORDER BY date_added DESC");
break;
case 'Horror':
$sql = mysql_query("SELECT * FROM books WHERE category='Horror' ORDER BY date_added DESC");
break;
default:
echo "Please provide a valid category";
break;
}
}
?>

This is untested let me know any issues you have.


You could also do..



<?php
if (!empty($_GET['cat'])) {
$cat = mysql_real_escape_string($_GET['cat']);
$sql = mysql_query("SELECT * FROM books WHERE category='$cat' ORDER BY date_added DESC");
}
?>

djr33
10-29-2011, 07:08 PM
Note that if you do use a variable category, you'd want to verify that it actually exists, either by checking the category itself or confirming that there is at least one match.

faketales
10-29-2011, 07:54 PM
it works thank you very much