I need to put a condition when I create one element of my array
foreach ($score as $item):
if ($item['subject_id'] == "3"){
$file_data_array[] = array(
"y" => $item['result'],
////Need condition here///////////////////////////////
"color" => '#FFF'
);
}
endforeach;
So I need a condition like
if ($item['confirmed'] == 1) {
"color" => '#FFF'
} else {
"color" => '#000'
}
So, since we cannot put an if inside an array, how can I do my condition?
Try and use the ternary if:
foreach ($score as $item):
if($item['subject_id'] == "3"){
$file_data_array[] = array(
"y" => $item['result'],
"color" => ($item['confirmed'] == 1 ? '#FFF': '#000')
);
endforeach;
"color" => ($item['confirmed']==1 ? "#FFF" : "#000")
foreach ($score as $item):
if($item['subject_id'] == "3"){
if($item['confirmed'] == 1) {
$color = '#FFF';
} else {
$color = '#000';
}
$file_data_array[] = array(
"y" => $item['result'],
"color" => $color);
endforeach;
<?php
$login_detail=array(0=>array("sumit","8101"),1=>array("ashwani","9526"),2=>array("parmod","2592"),3=>array("jitendra","5792"),4=>array("umesh","5555"));
foreach($login_detail as $value)
{
foreach($value as $value2 )
{
echo $value2." ";
}
echo "<br>";
}
$username=$_POST['txtusername'];
$password=$_POST['txtpassword'];
if($username==$login_detail && $password==$value)
{
header("location: dashbord.php");
}
else
{
header("location: loginexample.php?msg=user name or password is wrong");
}
Related
Is there any smarter / better / faster / easier way to define values in PHP functions?
I am using:
function get_user($id){
if($id == "1"){
return "Ram";
}
elseif($id == "2"){
return "Shyam";
}
elseif($id == "5"){
return "Ramu";
}
elseif($id == "7"){
return "Raman";
}
elseif($id == "8"){
return "Laxman";
}
elseif($id == "9"){
return "Bharat";
}
}
Use an associative array in the function.
function get_user($id){
$arr =["1" => "Ram",
"2" => "Shyam",
"5" => "Ramu",
"7" => "Raman",
"8" => "Laxman",
"9" => "Bharat"];
return $arr[$id];
// Or:
// if(isset($arr[$id])) return $arr[$id];
}
when I do this in CgridView:
'value' => '$data->status == 1 ? "Payed" : "None" ',
it works, but when I do this:
'value' => 'if ($data->status == 1) { echo "Payed"; } else if($data->status == 2) { echo "Two"; } else { echo "None"; } '.
What I need to do to make work the second statement, or how I need to rewrite it?
Convert your statement to use ternary if:
'value' => '$data->status == 1 ? "Payed": ($data->status == 2 ? "Two" : "None")',
You could also use a function instead to give a bit more flexibility and make it more readable:
'value' => function($row, $data ) {
if ($data->status == 1) { return "Payed"; }
else if($data->status == 2) { return "Two"; }
else { return "None"; }
}
Just in case :
I've tried topher's solution and I found out that I had to switch param like that :
'value' => function($data, $row ) {
if ($data->status == 1) { return "Payed"; }
else if($data->status == 2) { return "Two"; }
else { return "None"; }
}
With topher's solution $data->attribute_name did not work and was, in fact, the row instead of the model..
Perhaps, if you don't need $row, don't pass it.
my solution:
function checkStatus($status)
{
if ($status == 1) {
return "opl";
} else if ($status == 2) {
return "nal";
} else {
return "neopl";
}
}
'value' => 'checkStatus($data->status)',
But your will work too) I will accept answer)
Well I have this form that adds user, admin.I've given to user the right to add another user but not add an admin or so!
$roles = db_fetch_all("role") ;
$TabRoles = array(''=>'-- Choose --');
foreach($roles as $value)
{
$TabRoles[$value['rid']] = $value['name'];
}
and with print_array($TabRoles); it displays me :
Array
(
[] => -- Choose --
[admin] => Admin
[seo] => SEO
[user] => User
)
The condistion is if (user('rid') == 'admin'|'seo') . I tried this :
foreach($roles as $value)
{
if (user('rid') == 'admin'|'seo')
{
$TabRoles[$value['rid']] = $value['name'];
}else{
$TabRoles[$value['rid']] = $value['name'][2];
}
}
But it displays me the position "2" of the array !
Array
(
[] => -- Choose --
[admin] => m
[seo] => O
[user] => e
)
I want it to display like this :
Array
(
[] => -- Choose --
[user] => User
)
Any solution for this ? Many Thanks!
The solution : thanks to #DanFromGermany:
foreach($roles as $value)
{
if (user('rid') == 'admin' || user('rid') == 'seo')
{
$TabRoles[$value['rid']] = $value['name'];
}elseif (user('rid') == $value['rid']){
$TabRoles[$value['rid']] = $value['name'];
}
}
if (user('rid') == 'admin'|'seo')
| is a bitwise operator, but I guess you want a logical comparison here:
if (user('rid') == 'admin' || user('rid') == 'seo')
Explanation on using array brackets on strings:
$test = 'abcd';
echo $test[0]; // prints a
echo $test[1]; // prints b
echo $test[2]; // prints c
That's why you get only a letter instead of the element you want.
I think you are looking for something like:
foreach($roles as $value) {
if (user('rid') == $value['rid'])
$TabRoles[$value['rid']] = $value['name'];
}
}
or
foreach($roles as $value) {
$TabRoles[$value['rid']] = $value['name'];
}
if (user('rid') != 'seo' && user('rid') != 'admin')) { // inverse/negate the logic!
unset($TabRoles['seo']);
unset($TabRoles['admin']);
}
I'm just wondering if there's a way to simplify this code?
foreach ($parent_data as $ind_port_record) {
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
$record_to_include['remote_id'] = $ind_port_record['remote_id'];
$record_to_include['remote_name'] = $ind_port_record['remote_name'];
$record_to_include['remote_object_id'] = $ind_port_record['remote_object_id'];
$record_to_include['remote_object_name'] = $ind_port_record['remote_object_name'];
break;
}
}
//make sure you have something in remote object details
if ( ! isset($record_to_include['remote_id']) ){
$record_to_include['remote_id'] = '';
$record_to_include['remote_name'] = '';
$record_to_include['remote_object_id'] = '';
$record_to_include['remote_object_name'] = '';
}
I just need to make sure the values inside the $record _to_include are not uninitialized or NULL.
Thanks.
First off, simplify the if()
You currently have a lot of conditions
(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')
Let's make that check an array
in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )
Now to check if $record_to_include are not uninitialized or NULL
Let's loop through the array and do a simple check.
foreach($record_to_include as $record => $value) {
$record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
$record = array_filter($parentData, function (array $record) {
return isset($record['port_name']) && preg_match('!^(GI/2|G2|GI2)$!i', $record['port_name']);
});
$recordToInclude = $record ? current($record) : array(
'remote_id' => null,
'remote_name' => null,
'remote_object_id' => null,
'remote_object_name' => null
);
$gi_constraint = array('GI/2', 'G2', 'GI2'); // you are checking one and the same variable for different values, so you can use in_array here
foreach ($parent_data as $ind_port_record) {
if (isset($ind_port_record['port_name']) && in_array(strtoupper($ind_port_record['port_name']), $gi_constraint)){
foreach ($ind_port_record as $k=>$v) {
$record_to_include[$k] = $v; // as they have the same keys, you can specify the key and assign to the value of $in_port_record
}
break;
}
}
//make sure you have something in remote object details
if (!isset($record_to_include['remote_id']) ){
foreach ($record_to_include as $k => &$v) {
$v = ''; // when using reference, it will change the original array
}
}
Explanations in the code
Try
$arr = array('remote_id','remote_name','remote_object_id','remote_object_name');
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
foreach($arr as $ar){
$record_to_include[$ar] = (isset($ind_port_record[$ar]) && isset($record_to_include['remote_id']))?$ind_port_record[$ar]:NULL;
}
}
Is there a short way of doing this?
if ((isset($a['key']) && ($a['key'] == 'value')) {
echo 'equal';
// more code
}
else {
echo 'not equal';
// more code
}
I need to test lots of values on an array that can or cannot exist. I feel that this method is too verbose.
I could remove the isset() and mute the notices... but then I feel dirty.
Edit:
Answering Jack's question: "Could you give an example how you would test lots of values in an array?"
example:
if (isset($_GET['action']) && $_GET['action'] == 'view') {
//code
}
if (isset($_GET['filter']) && $_GET['filter'] == 'name') {
//code
}
if (isset($_GET['sort']) && $_GET['sort'] == 'up') {
//code
}
if (isset($_GET['tag']) && $_GET['tag'] == 'sometag') {
//code
}
etc...
For anyone still stumbling upon this question...
You could use PHP's coalescing operator:
if (($a['key'] ?? '') === 'value') {
echo 'equal';
// more code
}
else {
echo 'not equal';
// more code
}
See this question: using PHP's null coalescing operator on an array
I don't like to answer my own questions but I feel that the best and cleaner way to do this kind of checkings is to write a "helper funcion" like:
function iskeyval(&$a, $k, $v) {
return isset($a['key']) && ($a['key'] == 'value');
}
and then:
if (iskeyval($a, 'key', 'value')) {
...
}
else {
...
}
I have added comments to explain the code. Here is the code :
//this array maps the function with the get parameters
$functions = array (
"action" => "do_actions" ,
"filter" => "do_filters"
);
foreach ($_GET as $key=>$value) {
//check if this field is corresponding functions or not
if ( array_key_exists($key , $functions) ) {
call_user_func($functions[$key] , $key,$value);
}
}
function do_actions ($key , $value) {
//place your code here to play with this value
echo 'do_actions is called with ' . $key . 'and' . $value . "</br>";
}
function do_filters ($key , $value) {
//place your code here to play with this value
echo 'do_filters is called with ' . $key . ' and ' . $value . "</br>";
}
?>
$list = array(
0 => 'one',
1 => 'two',
2 => 'one',
3 => 'three',
4 => 'one',
);
if( #$list['xxx'] !== 'three')
echo 'Not ';
echo 'Equal';
Suppress the error reporting.