Aptana Simple Inline Conditionals for PHP - php

I typically use Eclipse as main PHP and JS editor. While the JS Formatting piece does what I want, the PHP Formatting seems completely lacking. Thus, I'd prefer to use Aptana Studio. However, PHP Formatting does at least one annoyance that I'd like to change. The Formatter for Aptana has so many options already, it's surprising that this is not an option.
I prefer for simple inline conditionals to be all on one line, like this:
if ($test == FALSE) return FALSE;
However, Aptana PHP formats like this:
if ($test == FALSE)
return FALSE;
This gets really annoying when you test anything more than 1 at a time:
if ($test1 == FALSE) return FALSE;
if ($test2 == 14) return FALSE;
if ($test3 == "") return FALSE;
becomes:
if ($test1 == FALSE)
return FALSE;
if ($test2 == 14)
return FALSE;
if ($test3 == "")
return FALSE;
I wondered if there was a manual way to force that type of formatting. Any suggestions?

There is no way to force it at the moment. The if body is wrapped as a block and forced to the second line.
What you'll need to do is to ask for an improvement here: http://aptana.com/r/apbugs

Related

ifelse in echo statement

I have code as bellow :
number_format(($hasilz->harga>100000 ? $hasilz->harga+2000 :
($hasilz->harga>300000 ? $hasilz->harga+4000 :
($hasilz->harga>400000 ? $hasilz->harga+8000 :
$hasilz->harga+10000))), 0, ',', '.')
this code result and read +2000 and +10000 only
any idea ?
Let's see what you got there (simplified):
if (hagara > 100000) {
harga+2000
} else if (hagara > 300000) {
hagara+4000
} else if (hagara > 400000) {
hagara+8000
} else {
hagara+10000
}
If you write it like this, its easy to see. hagara is either >100000 which results in +2000 or it is less, then it results in +10000.
In other words, the two else if will never be true, because if they were, the first if would already be true.
I think this is also a good example, when you should NOT use the tenary operator. It just makes it really hard to read and understand... sometimes the good old it-approach ist just the better solution. ;)
EDIT: To answer the question, you have to use a different order of the if-statements, beginning with the biggest one (or write them completely different). However, as already mentioned, you shouldn't do that.

Real estate site, trying to only output properties based on type

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

Weird error when writting OR conditionals in PHP

This error has been bugging me for a long time and I can't find an answer anywhere on the Internet, even using PHP official documentation.
When I write if statements with multiple conditions like this
if ((empty($user) == true) || (isset($user->data) == false)) {
//...
}
PHP says "Call to undefined function ()".
Then I try this alternative:
if (empty($user) == true || isset($user->data) == false) {
//...
}
And PHP says Call to undefined function isset().
PHP version 5.5.15.
By chance I just found the answer to my own problem. I cannot believe it, after all this time.
You're right #Musa
if (empty($user) == true || isset($user->data) == false) {
if (empty($user) == true || isset($user->data) == false) {
I realized something was wrong reconstructing both conditionals and looking at Sublime's syntax highlighting.
I use alt gr to write the pipe symbol and sometimes I left it pressed more than I should and I end up writting alt gr + space. It results in an invisible character that I believed to be a space.
Thanks everyone.
How about:
if (empty($user) || !isset($user->data)) {
//...
}

php - issue with booleans in if statement

I have been running into a weird issue for some time with php now and wonder if anybody can help. It's probably a totally minor stupid thing I don't see.
"if" statements don't seem to work for me when I use && and || for logical expressions.
Latest example:
$isNotSet = !isset($moved); // var_dump prints false
$moveSuccess = $moved instanceof SuccessNotification; // var_dump prints true
if(($isNotSet == true) || ($moveSuccess == true)){
the script always breaks at exactly that line and doesn't go any further. It won't execute anything after this.
I have tried lots of things, e.g.
if(!isset($moved) || $moved instanceof SuccessNotification){
or
if(!isset($moved) === true || $moved instanceof SuccessNotification){
or
$isNotSet = !isset($moved);
$moveSuccess = $moved instanceof SuccessNotification;
if($isNotSet === true || $moveSuccess === true){
All don't work. I'm aware they all mean the same, I was just trying to eliminate the thought of being on the wrong track. Some of those examples are marked as syntax error in Netbeans, but not all of them, which got me thinking.
I'd like to know what's going on here because this limits coding a lot. But as I said, I probably overlook a totally simple thing.
edit: the script breaks without any errors or warnings shown, it just stops working at that line
edit2:
I took the snippet and executed it on its own.
$moved = new SuccessNotification($code, $title, $message);
echo 1;
$isNotSet = !isset($moved);
echo 2;
$moveSuccess = $moved instanceof SuccessNotification;
echo 3;
if(($isNotSet === true) || ($moveSuccess === true) || 1){
echo 4;
}else{
echo 5;
}
echo 6;
This prints "123", nothing else. And yes, I have included the SuccessNotification class (otherwise there would have been an error anyways) which is being used system-wide already and properly working, just removed that line for posting here.
I take it that nobody really has a clue and therefore tries to find a simple solution which is ok because I thought it's just a simple thing. But I realize I have tried everything you suggest or would do in this situation.
Shouldnt it be:
if(($isNotSet === true) || ($moveSuccess === true)){
Instead of:
if(($isNotSet == true) || ($moveSuccess == true)){

Stuck Troubleshooting Compound IF Statement

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;

Categories