Set multiple variables via $_GET from URL - php

I have a table that can be sorted by any column using GET. I'm trying to make a second variable that determines sort order also using get. My understanding is that I could make the address as follows:
mypage.php?sortorder=1&sorttype=up
Using this as my code:
if (isset($_GET['sortorder'])) {
$sortorder = $_GET['sortorder'];
}
if (isset($_GET['sorttype'])) {
$sorttype = $_GET['sorttype'];
}
if ($sorttype = 'up') {
$sortby = SORT_ASC;
}
else {
$sortby = SORT_DESC;
}
My question is, what am I doing wrong? The GET for sort type is ignored, selecting the 'else' value every time.

if ($sorttype = 'up') should be if ($sorttype == 'up') (note the double equals sign).

Related

adding up sums of multiple variables in php

ok
so I have been stuck on this problem for a while and I'm sure there is a more elegant way of doing it.
I have 3 columns in a database -
Stand1 | Stand2 | Stand3
each column will either have a stand number (i.e D30) or 'Not Assigned'
What I need to do is count how many stands have been assigned to that customer.
For example :
D30 | not assigned | not assigned would = 1
D30 | B30 | E30 = 3
The code I have so far is
if ($row["stand1"] == 'Not Assigned') {
$stand1 = '0';
}
else {
$stand1 = '1';
}
if ($row["stand2"] == 'Not Assigned') {
$stand2 = '0';
}
else {
$stand2 = '1';
}
if ($row["stand3"] == 'Not Assigned') {
$stand3 = '0';
}
else {
$stand3 = '1';
}
I then thought I could try and count how many stand were assigned :
$standcount = $stand1 + $stand2 + $stand3
But when I print this out (everything else is in an array so loops through every customer) all I get is the total 3 for every customer.
I tried saving the data into an array like:
$standcount = array($stand1, $stand2, $stand3);
But I get the same result. I thought a foreach loop may work but not sure how to do it.
I would suggest you to:
Set the columns' default value to NULL, so that if no value is provided in an entry you'll have a null value instead of a string (that can contains different letters, as in your case). After that you can simply perform the isset() check against each value.
Use a function to avoid code repetition (the main source of errors), something like:
function check($value) {
return (isset($value)) ? 1 : 0;
}
$stand_1 = check($row["stand1"]);
$stand_2 = check($row["stand2"]);
$stand_3 = check($row["stand3"]);
If you instead want to use a string as "Not Assigned" or "NA", you can perform a case-insensitive check with strcasecmp() between the strings in case, for any reason, a value contains an extra space or a lower-case letter.
In this case the check function would be:
function check($str) {
return (strcasecmp($str, "NA") == 0) ? 0 : 1;
}
$stand_1 = check($row["stand1"]);
$stand_2 = check($row["stand2"]);
$stand_3 = check($row["stand3"]);

Correct Method for setting/using a NULL value in PHP 'Greater than or equal' IF ELSE

i have a simple if else statement and wondered if theres a better way of writing the code than I have listed below, the $price_data and $api_price are normally set but in some cases could be null / empty values, so if there is a empty value I need to revert back to $old_price
// now double the price compare api price and current price use highest value
if ($price_data >= $api_price) {
$price_double = $price_data;
// If we encounter a null or empty value for $price_data use old price data
if ($price_double == null){
$price_double = $old_price;
}
} else {
$price_double = $api_price;
// If we encounter a null or empty value for $price_data use old price data
if ($price_double == null){
$price_double = $old_price;
}
}
$useprice = ($price_double * 2);
I think this will do what you want:
if ((floatval($price_data) === 0) || (floatval($api_price) === 0)):
$price_double = $old_price;
else:
$price_double = ($price_data >= $api_price)?$price_data:$api_price;
endif;

Php dropdown change variable

I have an HTML with GET to a php file. The HTML has dropdowns one of which is called Combobox8. What I want is that when a user selects ALL in dropdown the relative variable in php is set to all, if not it is set to the selection in the dropdown:
$shopfloor = if ($_GET['Combobox8'] <> "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';
However I am getting an error.
What is wrong pls?
Thanks
If think you need some changes in your statement.
if($_GET['Combobox8'] != "all"){
$shopfloorvalue = $_GET['Combobox8'];
} else {
$shopfloorvalue='';
}
First of all you don't need to bind the if statement to a variable. If you want easy access to it bind it to a function instead!
function shoppingFloor(){
if($_GET['Combobox8'] != "all"){
$shopfloorvalue = $_GET['Combobox8']
} else {
$shopfloorvalue='';
}
return $shopfloorvalue;
}
then you can call it using shoppingFloor().
Second you should try != (is not) instead of <>.
If all you want is to simply bind the floor to $shopfloorvalue you can even try the short version of the if else.
$shopfloorvalue = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8'] : '';
^ ^ ^
Your IF Statement | if true what to do? | Else what? |
Give it a try!
$shopfloor = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8']:'';
change your code from
$shopfloor = if ($_GET['Combobox8'] <> "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';
To
$shopfloor = ($_GET['Combobox8'] <> "all")?$_GET['Combobox8']:'';
$shopfloor = if ($_GET['Combobox8'] != "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';

Return value in foreach loop in CodeIgniter

I am having some trouble in returning values from model to controller Using CodeIgniter. I am passing two arrays from controller to model. I am checking whether both the arrays are not empty and looping them using foreach loop to fetch some values from database and returning the query to controller. Here's my current code
if (!empty($search_for_area) && !empty($search_for_requirement))
{ foreach($search_for_requirement as $search_value_1)
{
foreach($search_for_area as $search_value_2)
{
if($search_value_2 != null && $search_value_1 != null)
{
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
print_r($nearbytution->result());
}
}
}
//Line 1
}
print_r($nearbytution->result()); works fine and i am able to view the results. Where should i put return $nearbytution; so that i can get all the fetched values? I tried it in Line 1 but i was getting only values of last array value.
function returnStuff($search_for_area,$search_for_requirement) {
$arr_area = array();
$arr_filter = array();
if ( ! empty($search_for_area) and ! empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$arr_area[] = $this->db->escape($search_value_2);
$arr_filter[] = $this->db->escape_like_str($search_value_1);
}
}
}
}
$str_area = 'NULL';
if ($arr_area)
$str_area = implode(', ', $arr_area);
$str_filter = "'^-$'";
if ($arr_filter)
$str_filter = "'(".implode('|', $arr_filter).")'";
$query = $this->db->query("
SELECT name, area, contactno
FROM tut_listing
WHERE area IN ({$str_area}) AND categoryfilter REGEXP {$str_filter} and partner > '' group by name
");
return $query->result();
}
Seriously, do consider this approach. You only need to bother the poor Mysql server with one call and you get all the data you want at the same time.
Apart from that, practice consistent style in your code. It will save both your time and your hair in the long run.
CodeIgniter escape()
MySQL REGEXP
Try this in the loop:
$nearbytution[] = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name')->result();
return $nearbytution;may then be placed at your "Line 1". It will contain an array of query results.
Unless I misunderstand your question why don't you just store all the results in a big array and then return that array?
function returnStuff($search_for_area,$search_for_requirement) {
$data = array();
if (!empty($search_for_area) && !empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
$data[] =$nearbytution->result());
}
}
}
}
return $data;
}
Now $data will be an array of results, each containing the query results. If your result set has to be cleaned up (to remove duplicates for instance) you can just loop through it and do that cleanup.
What you could also maybe do is build a large SQL query in your foreach loops and then just do that one big query and return the results.

elseif not working

This doesn't make sense.. I'm trying to sort posts according to the value of a URL parameter but my elseif statement isn't working.
This is a function that adds another WHERE clause to the query. There are no MYSQL errors I'm just having statement trouble.
function sort_where($where)
{
if (isset($_GET['sort'])) {
$sort = $_GET['sort'];
if ($sort = "up") {
$where .= " AND $sort > 1";
}
elseif ($sort = "down") {
$where .= " AND $sort > 1";
}
}
return $where;
}
The query eventually looks like this
$query = "SELECT * FROM posts WHERE something = $something AND $sort > 1";
The if statement works, the elseif is ignored. I get posts with up > 1 regardless or vice-versa if $sort = down in the if statement.
Actually, neither of the two inner ifs are working correctly.
You need to use == not =. One equals sign means assignment, and if you assign to a truthy value it always evaluates to true in an if condition. That's why your elseif appears to never happen.
You may also need to fix your WHERE clauses, they don't make sense to me (you're sorting but comparing to an up column and a down column?). Or maybe that's how you designed your table...
Based on your comments, try the following SQL WHERE clauses and see if you get the correct posts:
if ($sort == "up") {
$where .= " AND up > down";
}
elseif ($sort == "down") {
$where .= " AND down > up";
}
Single = means variable 1 = variable 2
Double == means compare
Also if you are going to use this code make sure you put mysql_real_escape_string() around your $_GET statements or anything that has user inputs or people will be able to use sql injection.
E.g. mysql_real_escape_string($_GET['sort']) and if you are using it multiple times makes sure you use a variable
Here is the corrected code ;)
function sort_where($where)
{
if (isset($_GET['sort'])) {
$sort = $_GET['sort'];
if ($sort == "up") {
$where .= " AND $sort > 1";
}
elseif ($sort == "down") {
$where .= " AND $sort > 1";
}
}
return $where;
}
Two problems:
if ($sort = "up") {
= is the assignment operator. You need to use == here if you want to test for equality.
Also, the body of both conditionals:
if ($sort = "up") {
$where .= " AND $sort > 1";
}
elseif ($sort = "down") {
$where .= " AND $sort > 1";
}
Are identical. I don't think you mean to append the same basic string to the query, do you? (Granted $sort will be different in either case, but why not just hardcode the strings in if that's what you mean anyway. This just reads really confusing, and it's hard to tell exactly what your intent is.)
if ($sort = "up")
elseif ($sort = "down")
should be
if ($sort == "up")
elseif ($sort == "down")

Categories