Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/294073-how-do-i-store-variables-in-url/
Share on other sites

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=audio
as 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 by Trainor91

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';


?>

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

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

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)."'";
}

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 by Trainor91

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 by CroNiX

 

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?

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";

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

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);
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.