Jump to content

Fetching a string from the url and using it with php


verbindingsfout

Recommended Posts

Hello there,

 

This is my first time posting on this forum, i also have little to no knowledge about php script/coding so i don't really know anything about php yet.

 

This is what i'm working on, or at least this is what i want to change in order to modify the website

      if (isset($_REQUEST['sort']) && !empty($_REQUEST['sort'])) { 
        if (strpos($_REQUEST['sort'], '|d') !== false) { 
          $osC_Products->setSortBy(substr($_REQUEST['sort'], 0, -2), '-'); 
        } else { 
          $osC_Products->setSortBy($_REQUEST['sort']); 
        } 
      }

This piece of code detects the wether you have set a option to ascend or descent the product name or price (it is a tomatocart webshop)

 

When you give no input at all, like when you just go to the catagory you see all products sorted by the alphabet, you can also force or set it by choosing it on the website like this http://url.to.some.site.tld/index.php?sort=name

 

All available options on the site are these:

?sort=name (which would be sort by name ascending)

?sort=name|d (which would be sort by name descending)

?sort=price (which would be sort by price ascending)

?sort=price|d (which would be sort by price descending)

 

I cant quite make out how to modify the script so it will choose the "?sort=price" automatically

 

How would i do that by just modifying that part of the script?

Link to comment
Share on other sites

So are you just looking to set a default sort order? In other words, it would default to "price" if $_REQUEST['sort'] isn't set to anything? If so, you could try the following:

<?php
//IF VARIABLE ISN'T SET, USE DEFAULT
if (!isset($_REQUEST['sort']) || empty($_REQUEST['sort'])) {
    $_REQUEST['sort'] = 'price';
}
 
//SET SORT ORDER
if (strpos($_REQUEST['sort'], '|d') !== false) {
    $osC_Products->setSortBy(substr($_REQUEST['sort'], 0, -2), '-'); 
} else {
    $osC_Products->setSortBy($_REQUEST['sort']);
}
?>
Link to comment
Share on other sites

Basically, the code uses the default value ("price") if the sort variable isn't set or is empty. Otherwise, it uses the sort variable as passed.

 

Note that the code could be modified to prevent visitors from using something other than a designated sort value. If these are the only valid options:

price, price|d, name, name|d

 

...the code could be modified as follows (untested):

<?php
//IF SORT VALUE ISN'T VALID, USE DEFAULT
$validSortOptions = array('price', 'price|d', 'name', 'name|d');
if (!isset($_REQUEST['sort']) || !in_array($_REQUEST['sort'], $validSortOptions)) {
    $_REQUEST['sort'] = 'price';
}
 
//SET SORT ORDER
if (strpos($_REQUEST['sort'], '|d') !== false) {
    $osC_Products->setSortBy(substr($_REQUEST['sort'], 0, -2), '-'); 
} else {
    $osC_Products->setSortBy($_REQUEST['sort']);
}
?>

Of course, your $osC_Products->setSortBy() method may already be performing this type of sanitation.

Link to comment
Share on other sites

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.