0

I'm developing a small system to add records which be can opened later. On the page view.php it shows all records of all different country. I just extend this to only show one selected countries.

I call this function with the following argument: $toxRecords->getStatsCountry();

If I want to show only one country, I just simple add the county code as parameter. Example: $toxRecords->getStatsCountry('NL');

Because I use one page to show all countries but also one specific country it needs to check if there is an variable. I check my POST with the following argument: toxInput::get('country'); This just simple return: $_POST['country'].

Now I just want that it will only use the function parameter if value country exists. This can be very simple, see below:

if($country = toxInput::get('country')) {
    $toxList = $toxRecords->getRecords($country);
} else {
    $toxList = $toxRecords->getRecords();
}

But I was wondering if it possible to shorten this to one single lane? Example what I tried and will explain what I want:

$toxRecords->getRecords(if($country = toxInput::get('country')){ echo $country; });
1
  • I would expect to see this logic in the getRecords method rather than the controller, but maybe it's just me. Commented Aug 21, 2017 at 18:19

2 Answers 2

2

this statement returns only one value through the condition:

condition ? value if condition is true : value if condition is false

for example:

$toxRecords->getRecords($country == toxInput::get('country') ? $country : "");
Sign up to request clarification or add additional context in comments.

6 Comments

It returns a error: Undefined variable: country. Trying to debug now..
It's because $country is not defined in your code scope @ChrisToxz
It works with this: $toxRecords->getRecords(toxInput::get('country') ? toxInput::get('country') : ""); Any solution to use a variable? Not necessary but just wondering.
But the code is trying to set variable in here $country == toxInput::get('country') . Why it is not setting this variable?
for condition you entered as toxInput::get('country') checks if it exists or not!
|
1

Try something like this:

($val1 == $val2)? Echo "true" : Echo "False";

For more info check this post: https://stackoverflow.com/a/1506621/7116840

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.