Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to pass 3 parameters compulsory in hyper link like

<a href="<?= base_url('report/print/') . $loc . '/' . $dept . '/' . $e_name ?>"


if $loc or $dept or &e_name is none i need to be set that value is zero,otherwise i will get error,
for example here i have passing 1 parameter value like $loc=1 and $dept= null &e_name=null and got error like this..

http://localhost/final_approval/report/print/1//

will get this error
Message: Too few arguments to function Report::print(), 1 passed in C:\xampp\htdocs\final_approval\system\core\CodeIgniter.php on line 533 and exactly 3 expected.

instead of this i would like to pass like this

http://localhost/final_approval/report/print/1/0/0

Can anyone help me how to do this?

What I have tried:

<?php if($loc==''){$loc=="0";}else ($loc=$loc;} if($dept==''){$dept=="0";}else { $dep=$dept;} if($e_name==''){$e_name=="0";}else { $ename=$e_name; }?>
Posted
Updated 28-Feb-23 2:49am
v2

As far as I'm aware, PHP supports the null-coalescing operator[^] which can be used to supply a default value if the target variable is null. In your example you might try:
PHP
base_url('report/print/').($loc ?? '0').'/'.($dept ?? '0').'/'.($e_name ?? '0')

Essentially the operator ?? checks the left-side of the expression (the variable) and if the value is null then it will use the right-side of the expression. Just bear in mind that if the value is something other than null (for example, false) then I believe it won't work the same, it would use the variable value.
 
Share this answer
 
Comments
Andre Oosthuizen 28-Feb-23 9:22am    
Sorry Chris, did not see your reply posted first when I did mine, we are on the same subject.
Member 11661832 28-Feb-23 13:27pm    
still got error

Type: ArgumentCountError

Message: Too few arguments to function Report::print(), 1 passed in C:\xampp\htdocs\final_approval\system\core\CodeIgniter.php on line 533 and exactly 3 expected
You have some errors in your operators like ")" missing or too many.

You can also use the null coalescing assignment operator ??= to simplify the code - PHP Null Coalescing Operator[^]

I have changed your code a little as base_url as undefined might also return some unknown errors and it is way shorter and more reliable -
function base_url() {
    $loc ??= 0;
    $dept ??= 0;
    $e_name ??= 0;
//If the values are empty, they will return 0 else they will return to whatever you have set them to be...

   return 'report/print/' . $loc . '/' . $dept . '/' . $e_name;
}


You can now make a call to your base_url function -
<a href="<?= $url = base_url(); ?>"><?=print_r($url);?></a>


The print_r will show you the returned values of each variable.
 
Share this answer
 
$loc ? $loc: '0'
finally gor it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900