verbindingsfout Posted February 6, 2014 Share Posted February 6, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285986-fetching-a-string-from-the-url-and-using-it-with-php/ Share on other sites More sharing options...
cyberRobot Posted February 6, 2014 Share Posted February 6, 2014 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']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285986-fetching-a-string-from-the-url-and-using-it-with-php/#findComment-1467931 Share on other sites More sharing options...
verbindingsfout Posted February 6, 2014 Author Share Posted February 6, 2014 Thanks cyberRobot, that did the trick So if i get it right $_REQUEST ['sort'] = 'price'; Where $_REQUEST requests the "sort" variable from the url, so the variable $_REQUEST['sort'] equals what you get after ?sort=price/price|d/name/name|d And if none it just goes by some default "name" Quote Link to comment https://forums.phpfreaks.com/topic/285986-fetching-a-string-from-the-url-and-using-it-with-php/#findComment-1467944 Share on other sites More sharing options...
cyberRobot Posted February 6, 2014 Share Posted February 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285986-fetching-a-string-from-the-url-and-using-it-with-php/#findComment-1467989 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.