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.
Related
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>';
}
}
<?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";
}
?>
In the following code $stu is declared an array however PHP reports invalid argument for foreach(). Why?
echo "<table align='center' border='1px'><tr><td>";
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>";
$students=array("Jack","John","Ryan");
foreach ($students as $key=>$stu)
{
echo "Please select a grade for $stu:";
echo "<select name='grade'>";
echo "<option>Grade A</option>";
echo "<option>Grade B</option>";
echo "<option>Grade C</option>";
echo "<option>Grade D</option>";
echo "<option>Grade E</option>";
echo "</select><br/>";
}
for ($i=0;$i<count($students);$i++)
{
echo "<input type='hidden' name='stu[]' value='$students[$i]'>";
}
foreach($stu as $arr_contents)
{
echo "$arr_contents";
}
echo "<input type='hidden' name='posted' value='true'>";
echo "<input type='submit' value='Enter'>";
echo "</form>";
echo "</tr></td></table>";
?>
$stu is defined in scope of the first foreach, which is closed before it is called in its own foreach. At the end of the first foreach loop, it will contain the last used string value, 'Ryan'.
// $stu is only known inside this block
foreach ($students as $key=>$stu)
{
}
If you want to echo the contents of $stu you will have to do it inside the first foreach loop.
The variable $students is not declared as an associative array with value as an array. It should be something like:
$students = array( "Jack" => array( 'array', 'contents' ), "John" => array( 'other', 'content') );
I've tried converting this ASP code to PHP, and would like to know if it is correct:
ASP:
Set emp = Server.CreateObject("Scripting.Dictionary")
EM_GENERAL=0
EM_AUDIO=1
EM_VIDEO=2
emp.Add EM_GENERAL, "General"
emp.Add EM_AUDIO, "Audio"
emp.Add EM_VIDEO, "Video"
For each em in Emp
Response.Write "<option value=" + CStr(em)
If em = CInt(IT_FIELD) Then
Response.Write " selected"
End If
Response.Write ">"
Response.Write Emp.Item(em)
Next
PHP:
$EM_GENERAL=0;
$EM_AUDIO=1;
$EM_VIDEO=2;
$emp = array();
$emp[$EM_GENERAL] = "General";
$emp[$EM_AUDIO] = "Audio";
$emp[$EM_VIDEO] = "Video";
foreach ($emp as $em) {
echo "<option value=" + ($em);
if ($em == intval($IT_FIELD)) {
echo " selected";
}
echo ">";
echo $em;
}
In VB, the For Each loop iterates over the Dictionary keys, not the values, so in your ASP code, em is the numeric key of your emp entries. In PHP, foreach iterates over the values, so $em is the value, not the key. Your if ($em == intval($IT_FIELD)) check will not work as expected. Fortunately the php foreach loop also supports the syntax foreach ($array as $key => $value), where $value would be equivalent to Dictionary.Item(key).
Try this instead:
foreach ($emp as $em => $value) {
echo "<option value=" + ($em);
if ($em == intval($IT_FIELD)) {
echo " selected";
}
echo ">";
echo $value;
}
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