Not sure what should be the title for this case, please edit if you have a suitable title for it. See below for the exact question.
MySQL Database:
Now I want to check if the value option in "Right_Option" is having value or not. (As per above, it is not having as Right_Option is "C" & Option_C is not having any value.)
My Code:
$option = "$row['Option_".$right_option."'];";
if($option != ''){
//Other Codes
}
else{
//other codes
}
It gives me an error:
You are using it wrong. Use this instead:
if(isset($row["Option_".$right_option]) && $row["Option_".$right_option] !== ''){
//Other Codes
}
else{
//other codes
}
Related
So I inherited an old site from another developer, and I'm not really a programmer so I'm having some trouble. I've put the code into a Fiddle: https://jsfiddle.net/s6coraf5/
Basically there are different categories of real estate properties and when you click on different pages it's supposed to filter them and only display the ones specific to whatever page you're on. The problem is that no matter what page you're on, it's just displaying everything. I've narrowed down some specific code but can't figure out why it isn't applying it right.
In the php there's:
$select_title = "Unknown";
if ($select_type == "all") { $select_title = "All Listings"; }
if ($select_type == "office") { $select_title = "Office"; }
if ($select_type == "industrial") { $select_title = "Industrial"; }
if ($select_type == "retail") { $select_title = "Retail"; }
if ($select_type == "shoppingcenter") { $select_title = "Shopping Center"; }
if ($select_type == "land") { $select_title = "Land"; }
if ($select_type == "agricultural") { $select_title = "Ranch / Farm"; }
if ($select_type == "investment") { $select_title = "Investment"; }
if ($select_type == "lodging") { $select_title = "Lodging"; }
if ($select_type == "sportsentertainment") { $select_title = "Sports / Entertainment"; }
In the HTML there are various places where those $select_type's are supposed to be applied:
a href="properties.php?select_type=<?php echo $select_type;?>&select_city=<?php echo $select_city;?>&priceForm=<?= $lowPrice;?>,<?= $highPrice; ?>&sqft=<?= $lowSize;?>,<?= $highSize; ?>&sort_type=city, asking_price desc"><font size=4><b>Location,</b></a>
it's only applying the "all" one though on every page. Again, the fiddle has the full php and html which is probably more helpful. I realize it's ugly and bad but maybe someone can see something obvious that I can't.
Thanks in advance for any help anyone can provide.
Based on the PHP code in the fiddle (Which really shouldn't be there since the fiddle is for Javascript), it seems like the problem is that you never use the select_type given in the URL.
Take a look at this line. This is the first time $select_type is used.
if (!isset($select_type)) $select_type = "all";
Thus, $select_type will always be all.
Instead you should either change it to:
if (!isset($select_type)) $select_type = $_GET['select_type'];
Or just add this line before it:
$select_type = $_GET['select_type'];
Your ssql query in your jsfiddle seems like it might be the culprit. I'll put it here to make it easier:
select properties.property_id,selected_subtypes.property_type,properties.listing_type,properties.city,properties.asking_price,memberships.name,properties.membership_id,properties.building_size,memberships.website,properties.sold_price
from selected_subtypes,properties,memberships where (selected_subtypes.property_id = properties.property_id)
and (properties.membership_id = memberships.membership_id)
and (memberships.status = 'Active') and (properties.sold_information = ' ' or properties.sold_information = 'Undisclosed')
and ((selected_subtypes.property_category ='".$select_type."' or '".$select_type."'='all')
or (selected_subtypes.property_type ='".$select_type."'))
and (properties.city = '".$select_city."' or '".$select_city."'='all')
and (properties.asking_price BETWEEN ".$lowPrice." and ".$highPrice.")
and (properties.building_size BETWEEN ".$lowSize." and ".$highSize.")
".$date_sql."
order by ".$sort_type
The query appears to be, in each line, be selecting $select_type OR 'all
This boolean approach will always bring back either of those, so it would bring back "all" every time.
If you want to bring back only the selected type, you'd need to eliminate the "all" within the OR in these rows.
The easiest way to handle this would be to set the value $select_type to be equal to "all" if that is what is selected, or else, the specific type. One way he way I do "all" queries is to set the value to be "1=1" which will always be true.
In other words, modify the query like so (for each of the selected types) to show this (I changed the OR to AND in this case)
AND selected_subtypes.property_type ='".$select_type."'
and then in the php modify the code to be something like this:
if (!isset($select_type)) {
$select_type = "1=1"
}
else {
$select_type = $_GET['select_type'];
}
Another thing to be aware of
This particular code is somewhat vulnerable to SQL injection, so you might want to modify the way that you query the database. I strongly suggest you look into prepared statements, either using mysqli or PDO
While coding, i sometimes make the error of writing a single = instead of == in an if-statement. So lets say i have this:
<?php
$name = 'piet';
if($name == 'jan'){
print 'hello jan';
}
?>
And i make a mistake and write this instead:
<?php
$name = 'piet';
if($name = 'jan'){
print 'hello jan';
}
?>
This won't throw an error of course, since it is valid php code. However, i never use this short-hand notation, so if i enter it by mistake it will break the logic of my code without telling me why. Is there a solution for this? I am using aptana which is an editor based on eclipse. Is there any way i can add my own custom errors to an editor (or php) based on for example regular expressions? Or are there any other approaches to alert me whenever i make this mistake?
Just change the order to this:
if('jan' == $name) {
This is known under Yoda conditions:
So if you now make the mistake:
if('jan' = $name) {
This will give you an error!
I am trying to make a password validator and have run into a problem. It seems my code won't read special character. below is my code:
<?php
$Passwords = array(
'Pas$word1',
'Pas$word2',
'Pas$word3',
'Pas$word4',
'pas$word1',
'PAS$WORD1',
'Password1',
'Pas$word',
'Pas$word 1',
'Pas$word1234567890',
'P$wd1');
function validatePassword($Password)
{
if (preg_match("[^!-/0-9A-Za-z]", $Password)==1)
{
return TRUE;
}
else
{
return FALSE;
}
}
foreach ($Passwords as $Password)
{
if (validatePassword($Password) == false)
{
echo "<p>The password <em>$Password</em> does not appear to be valid.</p>";
}
}
echo "<p>Processing has completed.</p>";
?>
fixed my array so it has single quotes, everything is now displayed. Current problem is that everything is displayed. What is wrong in my validatePassword function?
Any help would be appreciated.
Use single quotes or escape your literal dollar signs with backslashes. Otherwise, $word1 will be interpreted as a variable named word1.
See http://php.net/manual/en/language.types.string.php.
Your second problem is that you do not have delimiters on your regular expression. Change:
if (preg_match("[^!-/0-9A-Za-z]", $Password)==1)
To:
if (preg_match("/[^!-/0-9A-Za-z]/", $Password)==1)
Your third problem is that you have error reporting turned down/off. Had it been on you would have gotten numerous warnings when running your code that would have indicated the source of your problems.
When you are developing new code put this at the top of your file:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
I am experienced programmer, but am not a PHP developer. I have been asked to troubleshoot the following code, but can't see what the problem is. This IF statement works:
<?php
if ($ice_cream_social == 'yes') {
$registration_price = "58.00";
}
?>
However, the page in question requires some compound IF statements to test if form elements have been checked and adjust the price accordingly. This code does NOT work, it doesn't give an error -- but the IF doesn't execute:
<?php
if ($ice_cream_social == 'yes' AND $registration_type == 'Banquet and Conference') {
$registration_price = "78.00";
?>
Using developer tools I have verified the form fields are being passed from the HTML, the code has the variable name and value spelled correctly, and I can test for any ONE variable's value successfully. It seems that when a second or third test is added using the syntax I showed above, the test fails and the IF doesn't run. Based on my limited understanding of PHP's syntax and some Googling, the code looks correct to me (and does not produce any errors), but I am thinking I am missing something about why this isn't working.
Can someone tell me if I'm missing something obvious in my code sample that would cause the IF not to run? I didn't include more code as this is one piece of a messy set of includes :)
Thanks!
It looks like to me on the elseif you don't have a logical check, so you either need to change it to else or add a check(if that is your intention) elseif($registration_price>0)
I used this code to test:
<?php
$registration_price = '';
$ice_cream_social = 'yes';
//$ice_cream_social = 'no';
$registration_type = 'Banquet and Conference';
//$registration_type = 'Something else';
if ($ice_cream_social == 'yes') {
$registration_price = "58.00";
} else {
$registration_price = "not defined";
}
echo $registration_price;
if ($ice_cream_social == 'yes' && $registration_type == 'Banquet and Conference') {
$registration_price = "78.00";
} elseif( 1 > 0) {
$registration_price = "1 million dolars!";
} else {
$registration_price = "not defined";
}
echo $registration_price;
There are two columns in the database table "system". I have the systemId and want to get the mobileSystemId. But the variable $mobileSystemIds which I already defined as global is always empty.
EDIT: Now array_map doesn´t work. I always get my Exception output "Arrayfehler ArrayMap"
I have the following code :
$mobileSystemIds=array();
function getMobileSystemId($systemId)
{
global $mysqli;
global $mobileSystemIds;
$query="SELECT mobileSystemId FROM system WHERE systemId ='" .$systemId ."'";
if(!$result=$mysqli->query($query))
{
echo "Datenbankfehler DB-QUery";
exit(0);
}
if (!$mobileSystemId=$result->fetch_assoc())
{
echo "Datenbankfehler DB-Fetch";
exit(0);
}
$mobileSystemId=$mobileSystemId["mobileSystemId"];
echo "mobile System ID: " .$mobileSystemId ."<br />";
return $mobileSystemId;
}
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
In this case, using a return in your function would be much cleaner.
Nothing to do with your problem, but is your $systemId var trusted ? (To prevent SQL injection).
Update:
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
ought to read (just checked; it works for me):
$mobileSystemIds = array_map('getMobileSystemId', $systemsList);
if (empty($mobileSystemIds))
{
if (empty($systemsList) || !(is_array($systemsList)))
echo "OK: no mobile IDs, but no systems either";
else
echo "THIS now is strange :-(";
}
else
{
echo "Alles OK";
var_dump($mobileSystemIds);
}
I tried this by returning a dummy value based on input; if it does not work for you, there must be something strange in the database.
(Update: the text below refers to your original code, which did not use array mapping)
Your code ought to be working as it is. You put several $mobileSystemId 's into a single $mobileSystemId.
It works: I tested with a simpler code, removing the DB calls but leaving your code, and spelling, untouched.
So, the error must be elsewhere. I would guess that this code is included into something else, and:
the $mobileSystemIds = array(); declaration gets executed more than once, thereby losing all its data;
the $mobileSystemIds = array(); declaration is itself included in a more local scope and you read it from outside, reading an empty value or a totally different value.
Try replacing the first part of your code with:
GLOBAL $mobileSystemsIds;
if (defined($mobileSystemsIds))
trigger_error("mobileSystemsId defined more than once", E_USER_ERROR);
else
$mobileSystemsIds = array();
and also, in the function body:
if (!defined($mobileSystemsId))
trigger_error("mobileSystemsId should have been defined", E_USER_ERROR);