Trainor91 Posted January 19, 2015 Share Posted January 19, 2015 Hi, I am building a website that will contain listings, there will be a categories page which will bring the user to the subcategories for that category then the user can see all listings linked to that subcategory. At the minute I am just linking each page using basic href tags to go to the page name.php and all my pages end up being homepage.com/clickedlink.php. How do I change my URL's so that if i click on a certain category the url changes to page name.php?category=chosen category? Thanks Quote Link to comment Share on other sites More sharing options...
Strider64 Posted January 19, 2015 Share Posted January 19, 2015 (edited) <a href="index.php?category=pizza">Mmm Pizza</a> Then at the top or another page: <?php if (isset($_GET['category']) { $my_favorite_food = $_GET['category']; } ?> Edited January 19, 2015 by Strider64 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 If you need to use or retain a multiple get query value use http_build_query Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 (edited) Hi, I am currently displaying the results of a query in table format using the code below: echo '<td class="categorydata"><a href="computing.php?subcategory='.strtolower($row['subcategory']).'"><img class="catimg" src="'.$row['subcategory_img_path'].'" border="0" /></br>'.$row['subcategory'].'</a></td>'; When i click on a cell the URL is changing but im not being redirected to another page. Does anyone know why this is and what I need to do? At the minute, if i click on audio for instance the URL changes to: http://c2share.com/computing.php?subcategory=audioas expected but the page does not change :S I have researched this a bit but still not 100% on how it works. Any help would be appreciated. Thanks Edited January 20, 2015 by Trainor91 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 Show the entire code for computing.php Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 Show the entire code for computing.php <?php include 'core/init.php'; include 'includes/overall/header.php'; ?> <h1> Computing </h1> <?php $sql = "SELECT * FROM categories WHERE Category ='Computing' ORDER BY subcategory ASC"; $result = mysql_query($sql) or die(mysql_error()."<br>".$sql); $catNo = 1; echo "<table class='categorytable'> <tr class='categoryrow'>"; while($row = mysql_fetch_array($result)){ echo '<td class="categorydata"><a href="computing.php?subcategory='.strtolower($row['subcategory']).'"><img class="catimg" src="'.$row['subcategory_img_path'].'" border="0" /></br>'.$row['subcategory'].'</a></td>'; if ($catNo % 3 == 0) { echo "</tr><tr>"; } $catNo++; } echo "</tr> </table>";?> </br> <?php include 'includes/overall/footer.php'; ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 As strider mentioned the other post you have to pull the GET value if it exists. if(isset($_GET['subcategory']) && trim($_GET['subcategory']) !=''){ $subcategory = trim($_GET['subcategory']) !=''); } if(isset($subcategory)){ $sql2 = "SELECT * FROM categories WHERE subcategory='".mysql_real_escape_string($subcategory)."'"; $result2 = mysql_query($sql2) or die(mysql_error()."<br>".$sql2); while($row2 = mysql_fetch_assoc($result2)){ //whatever your values are echo $row2['id']."<br />"; echo $row2['name']."<br />"; } } If you want to retain your href loop to navigate, need to do an additional query and display the results mysql_* functions are deprecated and should be using mysqli or pdo should escape anything being inserted to a database mysql_real_escape_string Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 As strider mentioned the other post you have to pull the GET value if it exists. if(isset($_GET['subcategory']) && trim($_GET['subcategory']) !=''){ $subcategory = trim($_GET['subcategory']) !=''); } if(isset($subcategory)){ $sql2 = "SELECT * FROM categories WHERE subcategory='".mysql_real_escape_string($subcategory)."'"; $result2 = mysql_query($sql2) or die(mysql_error()."<br>".$sql2); while($row2 = mysql_fetch_assoc($result2)){ //whatever your values are echo $row2['id']."<br />"; echo $row2['name']."<br />"; } } If you want to retain your href loop to navigate, need to do an additional query and display the results mysql_* functions are deprecated and should be using mysqli or pdo should escape anything being inserted to a database mysql_real_escape_string Ok thanks, would this go inside the computing.php file or a new file? Had already started with mysql functions before i realised it was deprecated, im going to change it all to mysqli at the end though but thanks Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 It's up to you how you want to set this all up. You could make an included menu just hrefs Optionally can just do a form and a select dropdown in a loop, when selected would set the GET value instead of all those href links could do if/else or build dynamic sql queries based upon what is set an example would be if no subcategories is set to just display all, if a subcategory is set only display that $sql2 = "SELECT * FROM categories"; if(isset($subcategory)){ $sql2 .= " WHERE subcategory='".mysql_real_escape_string($subcategory)."'"; } Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 (edited) I am displaying query results in a table on computing.php page. The code for this page is below: <?php include 'core/init.php'; include 'includes/overall/header.php'; ?> <h1> Computing </h1> <?php $sql = "SELECT * FROM categories WHERE Category = 'Computing' ORDER BY subcategory ASC"; $result = mysql_query($sql) or die(mysql_error()."<br>".$sql); $catNo = 1; echo "<table class='categorytable'> <tr class='categoryrow'>"; while($row = mysql_fetch_array($result)){ echo '<td class="categorydata"><a href="computing.php?subcategory='.strtolower($row['subcategory']).'"><img class="catimg" src="'.$row['subcategory_img_path'].'" border="0" /></br>'.$row['subcategory'].'</a></td>'; if ($catNo % 3 == 0) { echo "</tr><tr>"; } $catNo++; } echo "</tr> </table>";?> </br> <?php include 'includes/overall/footer.php'; ?> The URL for the computing.php page is http://c2share.com/computing.php then when i click on a subcategory the URL changes to http://c2share.com/computing.php?subcategory=projectors but the page is the exact same. How would I be able to be directed to a blank page where the page is relevant to the ?subcategory=projectors part of the URL? Really struggling with this, have researched POST and GET variables but still not sure how to get it to do what i want :S I'm aware that mysql_ functions are deprecated but I'm going to worry about that when I get all the functionality working the way i want it to. Edited January 20, 2015 by Trainor91 Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 20, 2015 Share Posted January 20, 2015 (edited) Try this just before your query: //set a default value for the subcategory. This will be used if no subcategory exists in the URL $subcategory = 'Computing'; //Check if the subcategory exists in $_GET, and it contains a value. If it does, use that value for the subcategory. if (isset($_GET['subcategory'] && trim($_GET['subcategory']) != '') { $subcategory = trim($_GET['subcategory']); } //Perform the query using the subcategory. Either the default, or the one passed via the URL. $sql = "SELECT * FROM categories WHERE Category = '$subcategory' ORDER BY subcategory ASC"; Edited January 20, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 Try this just before your query: //set a default value for the subcategory. This will be used if no subcategory exists in the URL $subcategory = 'Computing'; //Check if the subcategory exists in $_GET, and it contains a value. If it does, use that value for the subcategory. if (isset($_GET['subcategory'] && trim($_GET['subcategory']) != '') { $subcategory = trim($_GET['subcategory']); } //Perform the query using the subcategory. Either the default, or the one passed via the URL. $sql = "SELECT * FROM categories WHERE Category = '$subcategory' ORDER BY subcategory ASC"; I think I kind of got it but when echoing the variable it is always returning '1' instead of what the value in the subcategory is? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 Don't you have a category and also subcategory column in database? Seems the below code is trying to return a subcategory name within the category column $sql = "SELECT * FROM categories WHERE Category = '$subcategory' ORDER BY subcategory ASC"; Quote Link to comment Share on other sites More sharing options...
Trainor91 Posted January 20, 2015 Author Share Posted January 20, 2015 Don't you have a category and also subcategory column in database? Seems the below code is trying to return a subcategory name within the category column $sql = "SELECT * FROM categories WHERE Category = '$subcategory' ORDER BY subcategory ASC"; Yes, within the categories table there is categoryid, category, subcategory, I know what you mean but if i change it to subcategory = '$subcategory' now it doesn't work quite as well. In saying that - It still isn't working how I want it Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 You should make a dynamic query depending what is set. Btw, I saw all you past queries you had Category as uppercased if (isset($_GET['category'] && trim($_GET['category']) != ''){ $category = trim($_GET['category']); } if (isset($_GET['subcategory'] && trim($_GET['subcategory']) != ''){ $subcategory = trim($_GET['subcategory']); } $sql_array = array(); if ($category) { $sql_array[] = " category = '$category' "; } if ($subcategory) { $sql_array[] = " subcategory = '$subcategory' "; } $sql = "SELECT * FROM categories"; if (!empty($sql_array)) { $sql .= ' WHERE ' . implode(' AND ', $sql_array); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.