Does ternary operator can only do one kind of condition? - ternary

ternary make code concise and readable, I'm curious about how to change the following if condition to ternary operator:
var1 = if(true){'a'};
I try the following
var1 = true? 'a': ;
since it require nothing to do with false condition, I leave blank after :, but apparently it gives me a error.
Is there a way to do this?
--------update---------
The intention of using the above example is that I want to simplify the problem, however it made everyone more confuse, so I post my original code:
if($_SERVER['REQUEST_METHOD'] == 'GET'){ $sub_count = 0; }
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : ;
how to change the if condition to ternary ?

$sub_count = null;
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : null;
// To check:
if(!isset($sub_count))
{
// Do something because $_SERVER['REQUEST_METHOD'] != 'GET'
} else {
if($sub_count===0)
{
// REQUEST METHOD IS GET
}
}

Related

How to use Logical operator OR " || " in ASP .NET

I've followed this question How to use IF statement in asp.net using C#, but I really can make it out with my code.
if ((txtNome.Value == null) || (txtNome.Value == (""))
{
}
Here is the error
The "||" operator cannot be applied a bool and string operands
I've tried all the possible solutions in the question above, but still not working. Some ideas?
Thank you
Solution 1
if ((txtNome.Value == null) || (txtNome.Value == ""))
{
}
Solution 2
The same as above but without the extra round brackets.
These are unnecessary for single logical statements.
if (txtNome.Value == null || txtNome.Value == "")
{
}
Solution 3
Built in function for the above in C#
if (string.IsNullOrEmpty(txtNome.Value))
{
}

Fatal error: Can't use function return value in write context in

Fatal error: Can't use function return value in write context in /home4/tigas44/public_html/wp-content/themes/cardealer/includes/menus/menus.php on line 67
can be seen at:
http://firstnationsautomobileapproved.com/
which refers to the following line;
if(!isset($_COOKIE['cars']) || empty(json_decode($_COOKIE['cars']))) {
from the block:
$compareClass = "";
if(!isset($_COOKIE['cars']) || empty(json_decode($_COOKIE['cars']))) {
$compareClass = esc_attr(' style=display:none');
}
The empty() functions seems to be the cause of your issue. The documentation says the following:
Prior to PHP 5.5, empty() only supports variables; anything else will
result in a parse error. In other words, the following will not work:
empty(trim($name)). Instead, use trim($name) == false.
So try to change your code to this:
$compareClass = "";
if(!isset($_COOKIE['cars']) || json_decode($_COOKIE['cars']) == false) {
$compareClass = esc_attr(' style=display:none');
}

Symfony 3 Too many parameters

I'm new to Symfony, and I got an error while running a query :
public function getFilteredArticles($page, $nbPerPage, $data) {
$query = $this->createQueryBuilder('a')
->leftJoin('a.images', 'i')
->addSelect('i')
->leftJoin('a.type_stockage', 't')
->addSelect('t')
->leftJoin('a.famille', 'f')
->addSelect('f');
if ($data['famille'] != '') {
$query->where('f.id = :famille')
->setParameter('famille', $data['famille']);
}
if ($data['rds'] == false) {
$query->where('a.stock_actuel > 0');
}
if ($data['recherche'] != '' && $data['recherche'] != null) {
$query->where('a.ref_article LIKE :recherche')
->setParameter('recherche', '%' . $data['recherche'] . '%');
}
$query->leftJoin('a.sousfamille', 's')
->orderBy('a.ref_article', 'ASC')
->getQuery();
$query->setFirstResult(($page - 1) * $nbPerPage)
->setMaxResults($nbPerPage);
return new Paginator($query, true);
}
This query have conditionnals parameters as you can see, that returns the list of articles I need for a table. But when I run this query to fill my table, I got the error :
An exception has been thrown during the rendering of a template ("Too
many parameters: the query defines 0 parameters and you bound 1").
I don't know why he is expecting 0 parameters. I tried using setParameters instead, but the result is the same.
Does anyone has an idea?
You should use andWhere() methods instead of where().
where() method removes all previous where, but setParameter() does not. That's why he found more parameters than where clauses.
I personally never use where if the condition has no sense to be the first condition, to avoid this kinds of errors.
if ($data['famille'] != '') {
$query->andWhere('f.id = :famille')
->setParameter('famille', $data['famille']);
}
if ($data['rds'] == false) {
$query->andWhere('a.stock_actuel > 0');
}
if ($data['recherche'] != '' && $data['recherche'] != null) {
$query->andWhere('a.ref_article LIKE :recherche')
->setParameter('recherche', '%' . $data['recherche'] . '%');
}
where() php doc
Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.
andWhere() php doc
Adds one or more restrictions to the query results, forming a logical
conjunction with any previously specified restrictions.
My error, in Symfony 4, using Doctrine 2.6 was
Too many parameters: the query defines 0 parameters and you bound 2
The problem was that I wasn't defining the parameters in andWhere method as
$this->createQueryBuilder('q')
...
->andWhere('q.propertyDate IS NOT NULL') //this also couldn't find anywhere
->andWhere('q.parameterName = :parameterName')
->setParameters(['q.parameterName' => $parameterName, ...2nd parameter])
As I couldn't find any answer to my problem, but was similar to this one, I thought to maybe help someone who is struggling like I was.
also in symfony 5 and 6 you should use andWhere() methods instead of where().
where() method removes all previous where, but setParameter() does not. That's why he found more parameters than where clauses.

How to assign dataGrid to other dataGrid in Flex. a = b doesn't work

in Flex I have something like that:
var dg:DataGrid = new DataGrid();
if (something) dg = dg1 else if (something_2) dg = dg2;
dg.dataProvider.getItemAt(3).id;
and dg is ALWAYS pointing at DataGrid (even if dg1 has name DataGrid_test and dg2 = DataGrid_test2) and finally action is made on my first DataGrid (DataGrid_test).
Why?
How can I pass dg1 or dg2 to dg?
Here is pasted almost full code of this part of application. I edited it to make that more clear.
var dg:DataGrid = null;
if ( currentState == "state1" ) { //if this condition is true then app. go into if and
dg = dataGrid_first; // make dg = DataGrid (1)
test.text = "inco"; // shows "inco" in "test" label
} else if ( currentState == "state2" ) { // if this is true then app. go..
dg = dataGrid_second; //here and set dg as DataGrid (exactly!) (2)
test.text = "outgo"; // and change test label into blank text (earlier text disapears)
}
search(dg);
It is modified with advice of '#splash'
Still not working.
EDIT:
I made this sceond edit to answer for all You who are helping me with that :) I think that it will be the best way. In codeblock above I added comments. (please read now comments and after that come back here :) )
Now I will explain exactly what happens.
I debug it many times and here are results:
dg is pointing at DataGrid (as component in flex, not as my dataGrid_first), I needed to extend DataGrid so now it is ColorColumn component (I don't know if I called it properly), not DataGrid. And dg is pointing at ColorColumn not at dataGrid_first or dataGrid_second. I even tried today the same thing what suggest #splash:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
and search still points at ColorColumn :/ My problem is really easy- I just want to pass to search different dataGrid on each state. If You have other ideas how I can do that in right way then I will pleased to hear about it. :)
But still I don't understand why it doesn't work. My search function uses algorhitm Boyer-Moor for searching through dataGrid.dataProvider for some text. If it find something then it is pushed into new array and after passing whole dataProvider I colorize rows with searched word.
If dg is never pointing to dg1 and dg2 then your (something) expressions may be evaluate to false. Check the value of your if-conditions - this should be easy to debug.
This should work:
var dg:DataGrid = null;
if (something)
dg = dg1;
else if (something_2)
dg = dg2;
if (dg)
{
// do something with dg
}
[Update]
I still can't see why your code isn't working, but you could simplify it like this:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
I'd propose to write this - since I guess either dg1 or dg2 should be assigned:
if (something) {
dg = dg1;
} else {
dg = dg2;
}
There may be cases, where if () {} else () {} neither executes the first or the second conditional block.
Finally a small hint, which structurally eliminates unwanted assignments in if conditions: Always write the literal left of the comparison operation: if ( "state1" == currentState ). If you accidentally typed = instead of ==, the flex compiler emits an error. The other notation silently assigns a value.
Additionally: Did you single-stepped through your code and watched the variables dg1, dg2 and dg? If not, set a breakpoint a few line before the if-statement and run the code step by step from there on. What do you see?
Here's a another tip: Use assertions to check for inconistencies:
package my.company.utilities {
public function assert(expression:Boolean):void {
// probably conditionally compile this statement
if (!expression) {
throw new Error("Assertion failed!");
}
} // assert
}
Use it e.g. at the beginning of a method like this:
public function doTransaction( fromAccount:int, toAccount:int ) {
assert( 0 < fromAccount );
assert( 0 < toAccount );
}
A typically good use of assert is to check variables regarding their range. As of the above example, fromAccount and toAccount should always be positive. Due to a bug, bad values might get passed to doTransaction(). In this case, the assertion fires an error.

1050: Cannot assign to a non-reference value

Hi my code as fallows what is wrong with it?
thank you and sorry for my bad english.
protected function belgelerDG_itemClickHandler(event:ListEvent):void
{
var durum:Boolean = false;
if(belgeicerikWindow==null){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
else{
durum=false;
for ( var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i ) {
if(NativeApplication.nativeApplication.openedWindows[i].title.toString() == belgeicerikWindow.title=belgelerDG.selectedItem.belge){
belgeicerikWindow.orderToFront();
durum=true;
}
}
if(durum==false){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
}
}
I'm betting the problem lies with the if statement that starts with:
if(NativeApplication.nativeApplication.openedWindows[i].title.toString()
You are doing an assignment within the value you are trying to compare against:
== belgeicerikWindow.title=belgelerDG.selectedItem.belge)
If it isn't what is causing your problem, at least it is something you should fix to make things more legible. :)

Resources