PHP loop within a loop, option selected? - php

Using PHP I echo out table rows in a loop like this:
<?php
/* SQL STUFF */
while ($row = mysql_fetch_array($select_courseelements)) {
echo "<tr>\n";
echo "<td>".$row['scpe_name']."</td>\n";
echo "<td>".$row['scpe_days']."</td>\n";
echo "</tr>\n";
}
Now I would like to include a <select> element with 5 predefined <option> values inside a <td> running with the loop. The option values will be 1 to 5.
There is also a column inside the $row loop that holds a value of 1 to 5 ($row['scpe_grades_status']).
Each time this value is equal to the one in the <select> I want it to change it to selected='selected'.
Would this be possible?
My <select> will look something like this when it's being run in the loop:
echo "<td>\n";
echo "<select id='elements_grade'>\n";
echo "<option value='1'>Registrerad</option>\n";
echo "<option value='2'>Ej påbörjad</option>\n";
echo "<option value='3'>Pågående</option>\n";
echo "<option value='4'>Godkänd</option>\n";
echo "<option value='5'>Deltagit</option>\n";
echo "<option value='6'>Ej deltagit</option>\n";
echo "</select>\n";
echo "</td>\n";

Sure, build the values from a loop. and you can compare the values from that part.
for($i = 1; $i<=5; $i++) {
echo "<option value='$i'";
echo ($row['scpe_grades_status'] == $i) ? " selected='selected'": "";
echo ">...."</option>"
}

$array = array('Registrerad' => 1, 'Ej påbörjad' => 2, 'Pågående' => 3, 'Godkänd' => 4, 'Deltagit' => 5, 'Ej deltagit' => 6);
foreach ($array as $key=>$value) {
if ($value == $row['scpe_grades_status'])
echo '<option value="'.$value.'" selected>'.$key.'</option>';
else
echo '<option value="'.$value.'">'.$key.'</option>';
}
Something like that?

Related

Basic PHP Array in html table

I was doing a simple PHP array table, but the result of my code is not what I've expected,
<?php
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo '<table border="1">';
foreach($names as $name => $indv_name) {
echo '<tr>';
echo "<th>$name</th>";
foreach($indv_name as $per_name) {
echo '<td>',$song, '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
The result of the array of this code is echoed horizontally, can anyone help me to echo it vertically?
Here's my output example:
firstname -> value, value, value
middlename -> value, value, value
lastname -> value, value, value
Output that I want to expect:
firstname - middlename - lastname
value - value - value
value - value - value
value - value - value
- value - value
- value
And whenever I add value in the array, it won't break the line.
Sorry for the late edit
A basic way
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo "<table>";
echo "<thead>";
echo "<tr>";
foreach ($names as $key => $value) {
echo "<th>$key</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($names as $key => $value) {
echo "<tr>";
foreach ($value as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";

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>";
}
?>

multiple select in 2 dimensional array in php

In PHP, I am having 2 array
$ex1 = array(a,b,c,d,e,f,g,h);
$ex2 = array(c,e,f);
Here how can I integrate this with multiple select option in PHP page
Here ex1 is the multiple select array like
<select multiple name=slt[]>
</select>
And ex2 values are the chosen listing options
Something like:
<?php
$ex1 = array('a','b','c','d','e','f','g','h');
$ex2 = array('c','e','f');
echo "<select multiple name=slt[]>";
foreach($ex1 as $val){
//in_array() checks if value from 1st array ($val) is present
//anywhere in the second array ($ex2)
//if yes, that option will be selected. I'm using ternary operator
//here instead of if statement
$selected = (in_array($val,$ex2))?' selected':'';
echo "<option value='".$val."'$selected>".$val."</option>";
}
echo "</select>";
?>
PHP Demo
Output Fiddle
I'm not sure about what you want to do, but here's an idea :
$ex1 = array("a","b","c","d","e","f","g","h");
echo "<select multiple>";
foreach( $ex1 as $value ){
echo "<option value='$value'>$value</option>";
}
echo "</select>";
Try running it:
<?php
$ex1 = array('a','b','c','d','e','f','g','h');
$ex2 = array('c','e','f');
?>
<select multiple name=slt[]>
<?php
foreach ($ex1 as $option) {
$active = false;
foreach($ex2 as $selected){
if($option == $selected){
$active = true;
}
}
?>
<option value="<?php echo $option; ?>" <?php if($active===true) echo "selected"; ?>><?php echo $option; ?></option>
<?php
}
?>
</select>

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";
}
?>

Help with PHP and associative arrays

I have to do a simple calculator in php based on user's input and choice from select field, something like this:
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$array = array( "option1" => 0.1,
"option2" => 0.15,
"option3" => 0.3,
"option4" => 3,
"option5" => 3,
"option6" => 16,
"option7" => 16,
"option8" => 16
);
echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
echo "<option value='".$v."'>".$k."</option>";
}
echo "</select> ";
echo "<input type='submit' value='='> ";
$total_volume = $a * $b;
echo $total_volume;
echo "</form>";
?>
Well, for now everything works fine, but the idea is that after user submits form, the page reloads with sent amount in input field and selected option which user actually selected...
First thing is easy: I just put value="a" in my input field, but I'm not sure how to make a selected option in <select> field???
I started with this:
foreach ($array as $k => $v) {
echo "<option value='".$v."'";
if ($b == $v) {
echo " selected ";
}
echo ">".$k."</option>";
}
...but this is obviously not working as expected... Please help me with this easy one :)
Thanks!
You need to echo something like 'selected="selected"'. The rest of the code seems fine to me.
On second thought there is something structurally wrong as multiple options return the same value, making it impossible to select the right one after submitting the form.
You will need to send $k as the value in the select in your loop and for your calculations you just use $array[$b] instead of $b.
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$array = array( "option1" => 0.1,
"option2" => 0.15,
"option3" => 0.3,
"option4" => 3,
"option5" => 3,
"option6" => 16,
"option7" => 16,
"option8" => 16
);
echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
echo "<option value='".$k."'";
if ($b == $k) {
echo ' selected="selected"';
}
echo ">".$k."</option>"; // or $v if you want to show the number
}
echo "</select> ";
echo "<input type='submit' value='='> ";
$total_volume = $a * $array[$b];
echo $total_volume;
echo "</form>";
?>
Try this:
foreach ($array as $k => $v) {
$selected= ($b == $v) ? 'selected="selected"' : '';
echo "<option value='$v' $selected>$k</option>\n";
}
You have to catch the data in $_GET.
Try
foreach ($_GET as $k => $v) {
echo "<option value='".$v."'";
if ($b == $v) {
echo " selected ";
}
echo ">".$k."</option>";
}
EDIT:
On my Computer it worked here is the dump of GET
<form action='' method='get'>
<input type='text' name='a' value='121'> of <select name='b'>
<option value='0.1'>option1</option>
<option value='0.15'>option2</option>
<option value='0.3'>option3</option>
<option value='3'>option4</option>
<option value='3'>option5</option><option value='16'>option6</option><option value='16'>option7</option><option value='16'>option8</option></select> <input type='submit' value='='> 363
</form>
The Url
http://localhost/test.php?a=121&b=3
And parameters
a 121
b 3

Categories