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; });
ishegg
  • 9,685
  • 3
  • 16
  • 31
Chris Toxz
  • 32
  • 6
  • I would expect to see this logic in the `getRecords` method rather than the controller, but maybe it's just me. – Don't Panic Aug 21 '17 at 18:19

2 Answers2

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 : "");
Amir Fo
  • 5,163
  • 1
  • 43
  • 51
  • It returns a error: `Undefined variable: country`. Trying to debug now.. – Chris Toxz Aug 21 '17 at 18:08
  • It's because `$country` is not defined in your code scope @ChrisToxz – Amir Fo Aug 21 '17 at 18:09
  • It works with this: `$toxRecords->getRecords(toxInput::get('country') ? toxInput::get('country') : "");` Any solution to use a variable? Not necessary but just wondering. – Chris Toxz Aug 21 '17 at 18:10
  • But the code is trying to set variable in here `$country == toxInput::get('country')` . Why it is not setting this variable? – Chris Toxz Aug 21 '17 at 18:11
  • for condition you entered as `toxInput::get('country')` checks if it exists or not! – Amir Fo Aug 21 '17 at 18:11
  • if you mean you want to check if `$country` equalation with `toxInput::get('country')` has done or not, it's not necessary to check it, because it'll done automatically `$toxRecords->getRecords($country = toxInput::get('country'));` – Amir Fo Aug 21 '17 at 18:14
1

Try something like this:

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

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

onno204
  • 71
  • 8