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='';
Related
I am currently working on the title of a website where I have defined a function to give result from the database to show on the title section it works fine for condtion 1 & 2, however when I need it to display 'Home' i.e. when condition 1 or 2 are not set or empty it doesn't return 'Home'. I am hoping to have done something wrong with the 3rd condition but not sure where.
P.s: get_path() is a function that extracts url parameters to compare them with database values.
<?php
function title($dbc){
$path_info = get_path();
$title_prod= $path_info['call_parts'][0];
$title_cat = $path_info['call_parts'][1];
if(isset($title_cat))
{
$query = "SELECT * FROM prdct_categories WHERE slugs = '$title_cat'";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_assoc($result);
$titleslug = $row['subgroup'];
} elseif(isset($title_prod))
{
$query1 = "SELECT * FROM prdct_categories WHERE product = '$title_prod'";
$result1 = mysqli_query($dbc, $query1);
$row = mysqli_fetch_assoc($result1);
$titleslug = $row['product'];
} elseif(!isset($title_prod) OR !isset($title_cat) OR isset($title_prod)=='' OR isset($title_cat)=='')
{
$titleslug = 'Home';
}
return $titleslug;
mysqli_close($dbc);
}
?>
<title><?php echo title($dbc); ?></title>
Thanks in advance for looking into it.
I'm guessing isset($title_prod)=='' should be $title_prod == '' and
isset($title_cat)=='' should be $title_cat == ''.
You can't compare isset to a empty string as you did here:
OR isset($title_prod)=='' OR isset($title_cat)==''
isset returns a boolean.
You should do this instead on that if:
elseif(!isset($title_prod) OR !isset($title_cat) OR $title_prod=='' OR $title_cat=='')
There is a slight mistake in your third condition:
elseif(!isset($title_prod) OR !isset($title_cat) OR isset($title_prod)=='' OR isset($title_cat)=='')
isset() returns a boolean value which means that one of these conditions will never be true isset($title_prod)=='' OR isset($title_cat)==''
I think you meant the following: [...] OR $title_pord == "" OR $title_cat == ""
I fixed it with below change with title tag;
<title><?php if(title($dbc) != null){echo title($dbc)." | ".'Upperbit';}else echo "Home" ." | ".'Upperbit'; ?></title>
Along with above changes as suggested. Perhaps I don't require a 3rd condition
Assuming you have changed the script according to correct isset syntax in this way:
elseif(!isset($title_prod) OR !isset($title_cat) OR $title_prod=='' OR $title_cat=='')
your script however has unexpected result because if, i.e., $title_cat=='', the first if statement is evaluated as True:
if( isset($title_cat) )
You have to change your code in this way:
if( $title_cat )
{
(...)
}
elseif( $title_prod )
{
(...)
}
else
{
(...)
}
I'm relatively new to PHP and I need some help on a search query.
I have a few drop down 'select' and a 'checkbox group' which will filter the search from database using (...WHERE somethingA = 'somethingA' && somethingB = 'somethingB' etc)
That's all working great but the problem comes when I want to make it so that some search fields DONT have to be used, so if 'SomethingA' is either disabled or value='none' then it will only return WHERE somethingB = 'SomethingB'.
I have tried using OR instead of AND but that returns both values if they are true and not really filtering it properly.
my initial solution was to have if..else statements to define the query,
for example:
$query = "SELECT * FROM table";
$results = $con->query("$query $where $QueryA $QueryB $QueryC");
if($_GET['SomethingA'] == "none" && $_GET['SomethingB'] == "none" && $_GET['SomethingC'] == "none"){
$where = ""
$QueryA = ""
$QueryB = ""
$QueryC = "ORDER by ID" //if all search field is 'none' then get all results
}elseif($_GET['SomethingB'] == "none" && $_GET['SomethingC'] == "none"){
$where = "WHERE"
$QueryA = "SomethingA = '{SomethingA}'" //only use A filter one field
$QueryB = ""
$QueryC = ""
}elseif($_GET['SomethingA'] == "none" && $_GET['SomethingC'] == "none"){
$where = "WHERE"
$QueryA = ""
$QueryB = "SomethingB = '{SomethingB}'" //only use B filter one field
$QueryC = ""
.....
it works but you can already see the problem as if i wanted to cross matrix all conditions it becomes very lengthy and confusing.
So my question is whether there is a much better way of doing this, for instance, make value='none' return all results?
been looking around and attacking it from many angles but cant find a solution..
maybe javascript could help but im not the best with it.
thanks in advance
The question is not too clear but look into this. It should help.
$query="SELECT * FROM table WHERE";
$query_link = " AND ";
$isASet=false;
$isBSet=false;
$isCSet=false;
if(strcmp($_GET['SomethingA'],"none") != 0){
$query.=" column = {$_GET['SomethingA']}";
//set this to true for later if statements
$isASet=true;
}
if(strcmp($_GET['SomethingB'],"none") != 0){
//check if A has been set, if yes include an AND
if($isASet){
$query.=$query_link;
}
//include this one as usual
$query.=" column = {$_GET['SomethingB']}";
$isBSet=true;
}
if(strcmp($_GET['SomethingC'],"none") != 0){
//check if A or B has been set, if yes include an AND
if($isASet || $isBSet){
$query.=$query_link;
}
//include this as usual
$query.=" column = {$_GET['SomethingC']}";
}
//run query and collect result
$result = $connection->query($query);
I have this PHP statement:
if (($row['rest'] != "") or ($row['rest'] != "Select An Option")) {
$rest = "<b>Rest Stops:</b> {$row['rest']},";
}
else {
$rest = "";
}
which is not evaluating properly and I can't figure out why. What I want the statement to do is if the field 'rest' is blank or "Select An Option" then the variable $rest should evaluate to "Rest Stops:" followed by the data. My data is "Select An Option" and I get "Rest Stops: Select An Option" as the output. I did some testing of this statement and I figured out PHP is assigning the variable $row['rest'] as not equal to "" instead of evaluating the 'or' statement. What would be the correct syntax?
What I want the statement to do is if the field 'rest' is blank or Select An Option than the variable $rest should evaluate to Rest Stops: followed by the data.
Your logic is incorrect. You need to check if they are equal to, so use == instead of !=.
if ($row['rest'] == "" || $row['rest'] == "Select An Option") {
^--------------------^ ^--------------------------------^
if field 'rest' blank if field is 'Select An Option'
This can be be improved by using empty() to perform the "is empty" check:
if (empty($row['test']) || $row['rest'] == "Select An Option") {
"If the field 'rest' is blank ...." - if that's true, then your logic is backwards:
if ($row['rest'] == '') || ($row['rest'] == 'Select an option') {
$rest = 'rest stops';
} else {
$rest = '';
}
Note the use of == for equality, rather than != for inequality.
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).
$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}
$db->sql_freeresult($result);
I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:
if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')
Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.
if ($row['pf_kp_em_no_bonethr'] === NULL)
Something like this should work.
if (is_null($row['pf_kp_em_no_bonethr'])) {
echo "Is NULL";
}
MySQL will return NULL values to PHP as actual PHP NULL. In this situation, what you need is:
// Notice lack of quotes around NULL
// And use === to distinguish type properly between integer 0 and NULL
if ($row['pf_kp_em_no_bonethr'] === NULL)
However, it would be more appropriate to check it in the query if NULL values are what you need to work with in PHP.
$sql = 'SELECT * FROM `phpbb_profile_fields_data` WHERE pf_kp_em_no_bonethr IS NULL';
Or to find all three values:
$sql = 'SELECT * FROM `phpbb_profile_fields_data`
WHERE pf_kp_em_no_bonethr IS NULL
OR pf_kp_em_no_bonethr IN (1,2)
';
I'd recommend to be very carfull with this one: I have seen
<?php
$field=$row['fieldname'];
if ($field===null) {
//Do something
}
?>
fail intermittently, especially on windows. This is why I prefer
SELECT
IFNULL(fieldname,'some_safe_value') AS fieldname
...
FROM
...
and the resulting trivial null-check.
Use is_null or === NULL.
if(is_null($row['pf_kp_em_no_bonethr'])){
}
or
if($row['pf_kp_em_no_bonethr'] === NULL){
}