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;
}
Related
I'm trying to add data from database to 2-dimensional array and then unpacking.
This function is in a folder named imsConnection.php
function getCurrency() {
global $cn;
$sql = "select * from Currency";
$res = mysqli_query($cn, $sql);
$a = array();
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_array($res)) {
$a[] = array($row['currencyID'], $row['currencyName']);
}
}
return $a;
}
And to unpack it into drop box:
<select name="drpCurrency" required>
<?php
require_once("imsConnection.php");
$a = getCurrency();
foreach($a as $i) {
echo "<option value='$i'>$a[$i]</option>";
}
?>
</select>
To make a formal answer: If you create multi dimentaion array and you loop with foreach you need to echo the value according the key you need. In you case:
foreach($a as $i)
echo "<option value='" . $i["currencyID"] ."'>" . $i['currencyName'] . "</option>";
I recommend you change your array do be according key - change the getCurrency function as:
while($row = mysqli_fetch_array($res)){
$a[$row['currencyID']] = $row['currencyName'];
Then you can use it as:
$a = getCurrency();
foreach($a as $k => $i)
echo "<option value='$k'>$i/option>";
Assuming you want the value as currency ID and the option content as the currency name
I'm working on a simple HTML form that will include a drop down list for the selected Salesperson. The list is coming from a database and is in an array that is stored in a variable $ids and looks like this:
Array ( [John Jones] => JJ [Sally Smith] => SS [Victor Howards] => VH [Barnie Kemp] => BK )
Here's how the select list is currently constructed:
<?php
$output = "";
$selected = false;
foreach ($ids as $id => $value) {
$output .= "<option value=\"$value\"";
if ($id == $record->getField('initials')) {
$selected = true;
$output .= " selected";
$selectedID = $record->getField("id");
}
$output .= ">$id</option>";
}
if (!$selected) {
$chosen = $record->getField("initials");
$output = "<option value=\"$chosen\" selected>$chosen</option>$output";
}
echo $output;
?>
I'm trying to update the select list to show the current value for this field as the selected option. I have the name/value stored in these variables:
$initials
$name
So if $initials = 'SS' I would like Sally Smith to be the selected value in the list.
Try to use as simple as follow
<?php
$output = "";
foreach ($ids as $id => $value) {
$selected = "";
if ($id == $record->getField('initials')) {
$selected = "selected";
}
$output .= "<option value='$value' $selected>$id</option>";
}
echo $output;
?>
Do not make your code complex.. do it like this one:
$value=$record->getField('initials');
foreach ($ids as $id => $valueData) {
if($id==$value)
echo "<option value='".$id."' selected='selected' >$id</option>";
else
echo "<option value='".$id."'>$id</option>";
}
I keep getting array to string conversion in this code.. please help me
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
echo "<tr>\n";
foreach ($row as $item)
{
//echo " <td>".($item !== null ? htmlentities($item, ENT_QUOTES):" ")."</td>\n";
echo " <td>".($item);
if (is_numeric($item)){
$quantity = oci_fetch_array($qty_parse, OCI_ASSOC);
echo '/'.$quantity.'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
Looks like oci_fetch_array() returns array (array in function name should tell you something ;)).
You can use var_dump($quantity); to see what was returned by this function.
I guess that what you need to do is something like this: echo '/'.$quantity['qty'].'<meter value=10 min="2" max="10"></meter>';
First of all, your $row variable is not defined. You can use next solution:
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
while ($item = oci_fetch_array($qty_parse, OCI_ASSOC))
{
echo " <td>".($item['qty']);
if (is_numeric($item['qty'])){
echo '/'.$item['qty'].'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
P.S. When oci getting associated array via OCI_ASSOC - your script gets $item variable like:
$item['qty'] = 'value';
If you want to get value from $item as string variable, redefine you variable on loop like:
$item = current($item);
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.
I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}
It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}
If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}