if else statement not work in option input - php

HERE is my code :
var $myarray =array ('black','blue','brown','yellow')
if ( $row['color']=="") {
echo"<option value='' selected>---select---</option>";
}
foreach ($myarray as $color) {
if ($row['color']!="" && $row['color']==$color) {
echo"<option value='$color' selected>$color</option>";
}else {
echo"<option value='$color'>$color</option>";
}
}
My question is how to get rid of the (<option value='' selected>---select---</option>)
if $row['color'] is not null or not empty?
I´ve tried many ways...but nothing helped. :S

<?php
$myarray =array ('black','blue','brown','yellow');
$row['color']='';
?>
<select name='whatever'>
<?php
if ($row['color']=='' || $row['color']==null){
echo "<option value=''>----Select----</option>";
}
foreach ($myarray as $color){
if ($row['color']==$color){
echo"<option value='$color' selected>$color</option>";
}
else{
echo"<option value='$color'>$color</option>";
}
}
?>

You declared the variable in the first line with var, which is not a part of PHP. You need to declare it like all your other variables, by putting a $ at the start of it. You also reference $mysrray instead of $myarray

Try this. it should work.
$myarray = array('black','blue','brown','yellow')
foreach ($myarray as $color) {
if ($row['color'] != "" && $row['color']==$color)
{
echo '<option value="'.$color.'" selected="selected">'.$color.'</option>';
}
elseif($row['color']=="")
{
echo '<option value="" selected="selected">---select---</option>';
}
}

Related

avoid $ sign on php for print array value from json

I have to Simple code retrieve data from json.i got problem with json properties that contains like department$_identifier. 'department$_identifier' properties contains the value.but php count it as a variable.how to escape this.
Here is my code :
foreach ($data as $key => $Attendance) {
if ($Attendance->department$_identifier == 'Administration') {
echo $Attendance->department$_identifier;
echo $Attendance->attendanceDate;
echo $Attendance->status;
echo $Attendance->_entityName;
}
}
How to solve this
use {} like as
echo $Attendance->{'department$_identifier'};
http://php.net/manual/en/function.json-decode.php
Try this
foreach ($data as $key => $Attendance) {
if ($Attendance["department$_identifier"] == 'Administration') {
echo $Attendance["department$_identifier"];
echo $Attendance["attendanceDate"];
echo $Attendance["status"];
echo $Attendance["_entityName"];
}
}

Print out something only when value is within specific group during loop in PHP

I have a big array that is being used throughout the site and changing its structure just for the following task would be a pain. I want to make several select boxes with another array. The output I want to get is like this:
/* Example Array:
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals",
"Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
*/
<select name='Amphibian'>
<option value='0'>Frogs</option>
<option value='1'>Toads</option>
</select>
<select name='Mammal'>
<option value='2'>Bats</option>
<option value='3'>Elephants</option>
<option value='4'>Rats</option>
<option value='5'>Seals</option>
</select>
<select name='Reptile'>
<option value='6'>Crocodilians</option>
<option value='7'>Turtles</option>
</select>
I can't figure out how to print out the options only when the value is within the specific animal group during each iteration of $group. I have tried each() to get the next animal $endAnimal from $group and then break the internal loop if $animal matches the $endAnimal, but I also need to make the loop start printing options at specific values.
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
foreach($group as $thisAnimal=>$category){
$nextKey = (each($group));
$endAnimal = $nextKey['key'];
print "<select name='$category'>";
foreach($allAnimals as $idx=>$animal){
print "<option value='$idx'>$animal</option>";
if($endAnimal === $animal){
break;
}
}
print "</select>";
}
?>
Please check this out:-
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
$group_keys = array_keys($group); // get the keys of group array
$j = 0;
foreach($group_keys as $key => $group_k){
print "<select name='$group[$group_k]'>";
if(isset($group_keys[$key+1])){
$new_value = $group_keys[$key+1];
}else{
$new_value = '';
}
if($new_value ==''){
foreach($allAnimals as $key => $allAnm){
print "<option value='$j'>$allAnm</option>";
unset($allAnimals[$key]);
$j ++;
}
}else{
$key_from = array_search($new_value,$allAnimals);
for($i = 0; $i<$key_from; $i++){
print "<option value='$j'>$allAnimals[$i]</option>";
unset($allAnimals[$i]);
$j ++;
}
}
$allAnimals = array_values($allAnimals);
print "</select>";
}
Output:- https://eval.in/379462 (eval.in)
local end :- (for better showing) :- http://prntscr.com/7fmtbi
Array of Array is the solution I think
$group = array( "Amphibian" => array("Frogs", "Toads"), "Mammal" => array("Bats", "Elephants", "Rats", "Seals"), "Reptile" => array("Crocodilians", "Turtles") );
foreach($group as $thisAnimal=>$category){
print "<select name='$category'>";
foreach($category as $idx=>$animal){
print "<option value='$idx'>$animal</option>";
}
print "</select>";
}
Thank you for your responses and answers. I just sort of get that to work by checking whether $thisAnimal from $group is the same as $animal from $allAnimals during the internal loop. If it's the same, turn $passThis to true and then proceed to print out the option until $endAnimal matches $animal. Example
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
foreach($group as $thisAnimal=>$category){
$nextKey = (each($group));
$passThis = false;
$endAnimal = $nextKey['key'];
print "<select name='$category'>";
foreach($allAnimals as $idx=>$animal){
if($animal === $thisAnimal){
$passThis = true;
}
if($endAnimal === $animal){
break;
}
if($passThis === true)
{
print "<option value='$idx'>$animal</option>";
}
}
print "</select>";
}
?>

Loop a variable function argument

Is it possible to loop a function variable from this? I am trying to loop the $item[$a-1]
echo load_func($hid, $item[$a-1]);
And make it something like this but I know this is wrong (just an idea):
echo load_func($hid, for($a=1;$a<=$addctr;$a++){$item[$a-1]});
This is the actual but fail because it loops the whole function.
echo "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
for($a=1;$a<=$addctr;$a++){
echo load_func($id, $item[$a-1]);
}
echo "</select>";
The purpose of the function is to automatically select an option based from the record saved on a table.
Try to pass the whole item to load_function();
echo load_func($hid, $item);
And deal with every item in the function itself.
function load_func($hid, $item) {
$return = "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
foreach ($item as $option) $return .= $option;
$return .= "</select>";
return $return;
}
From what I am understanding from your question, you should pass the array $addctr to the function. And inside the function you should put your for loop and do the calculations.
something like:
function load_func($id, $items) {
$strOptions = "";
for($a=1;$a<=$items;$a++){
if($items[$a-1] != $id)
$strOptions .= "<option>Your value</option>";
else
$strOptions .= "<option selected>Your value</option>";
}
return $strOptions;
}

Get value from array

I've following php code which store value to an Array called ch[].
echo "<input type='radio' name='ch[$roll]['$sname']['$class']' value='1' /> ";
echo "<input type='radio' name='ch[$roll]['$sname']['$class']' value='0' />";
Now I can get only $roll and value wtih following code
foreach($_POST['ch'] as $id=>$value)
{
echo "id = $id ";
echo "VAlue = $value; <br/>";
}
but I want to get the value of $sname, $class variable. Is there anyway to get these value. Can you guys give me a idea or solutions ?
Thank You.
Updated:
foreach ($_POST['ch'] as $roll => $arr1)
{
echo $roll;
foreach ($arr1 as $name => $arr2)
{
echo $name;
foreach ($arr2 as $class => $value)
{
echo $class;
echo $value;
$sql = mysql_query("INSERT INTO e_attendence VALUES('', '$sname', '$roll', '$class',
'$value', '$current_date')");
}
}
}
Give this a try
foreach ($_POST['ch'] as $roll => $arr1)
{
echo $roll;
foreach ($arr1 as $name => $arr2)
{
echo $name;
foreach ($arr2 as $class => $value)
{
echo $class;
echo $value;
}
}
}
If you are using same code as written bellow :
echo "<input type='radio' name='ch[$roll]['$sname']['$class']' value='1' /> ";
echo "<input type='radio' name='ch[$roll]['$sname']['$class']' value='0' />";
Then please remove ' from ch[$roll]['$sname']['$class'] because if you can inspect the html you will find that the radio buttons are not created properly and never use inverted commas in html input array.
After fixing this please try
echo (int)$_REQUEST['ch'][$roll][$sname][$class];
May this helps you.

foreach with two kind of values

<?php
foreach ($array['response']['data']['Offers'] as $arr) {
$gegevens = array(
$arr['Offer']['id'],
$arr['Offer']['name'],
$arr['Advertiser']['company'],
$arr['Offer']['advertiser_id'],
$arr['Offer']['offer_url'],
$arr['Offer']['default_payout'],
$arr['Offer']['expiration_date']
);
echo '<tr>';
foreach ($gegevens as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>\n";
}
?>
This is the code I have.
How can I search for two kind of values inside the foreach($array['response']['data']?
It has to be foreach($array['response']['data'][**Offers**] and also foreach($array['response']['data'][**Advertisers**].
I need this so that I can echo out $arr['**Offer**']['name'], $arr['**Advertiser**'] ['company']
Can someone help me with this?
In its simplest:
foreach($array['response']['data']['Offers'] as $key => $offer_arr){
$advertiser_arr = $array['response']['data']['Advertisers'][$key];
}
You then have offer array and advertiser array of the same index
Doesn't sound like you need to put them all into an array. This should suffice:
<?php
foreach($array['response']['data']['Offers'] as $arr) {
echo '<tr>';
echo "<td>{$arr['Offer']['name']}</td>";
echo "<td>{$arr['Advertiser']['company']}</td>";
echo "</tr>\n";
}
?>

Categories