PHP reports invalid argument for foreach(). Why? - php

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') );

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

Add +1 number to an array

session_start();
echo "<form method='post'>";
echo "<input type='text' name='random' placeholder='Product' >";
echo "<input type='submit' value='submit' name='submit'>";
echo "</form>";
if(!$_SESSION['list']) {
$_SESSION['list'] = array(); // create session
}
if(isset($_POST['submit']) && empty($_POST['random'])) { // Check if input is empty
echo "* Input is empty!";
} elseif(isset($_POST['submit']) && isset($_POST['random'])) {
$_SESSION['list'][] += 1; // add +1 to array
}
foreach ($_SESSION['list'] as $value) {
echo $value . "<br>"; // shows the list/array
}
So I tried to make array that add +1 number on a submit, but my array keeps being 1, so it doesn't go like: 1,2,3,4,5... but it goes like: 1,1,1,1,1,1 . They don't add up. How do I fix this?
Given an array $arr, or $_SESSION['list'] in your case, it is possible to append an element to the end of the array as follows.
$arr[] = 'new element';
You try to combine this with the += operator. This will first append 0 to the array and then increment it, resulting in 1 being appended all the time.
It looks like what you actually want to do is this:
$arr[] = end($arr) + 1;
That is, take the last value of the array, add 1 to it and append that to the array.

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.

PHP HTML Resend all GET data with keys on form submit

How can i add a foreach() or a while() loop or anything like that in PHP which will
resend all $_GET keys and values when submitting a form.
Something like:
<?
echo '<form action="" method="get">';
echo '<input type="text" name="text_field_1">';
// LOOP {
<input type="hidden" name="$row_key" value="$row_value">
}
?>
Thanks.
This will loop through each $_GET key and echo a hidden input with the value
foreach($_GET as $key => $value){
echo "<input type=\"hidden\" name=\"$key\" value=\"$value\"/>";
}
foreach ($_GET as $k => $v) {
echo "<input type=\"hidden\" name=\"{$k}\" value=\"{$v}\" />";
}
Note if $_GET has not been set this will show an error in newer versions of PHP.
You can loop through $_GET or you can store the entire $_GET array within a session negating the need to do this.
if(count($_GET) > 0) {
foreach($_GET as $var => $val){
echo "<input type=\"hidden\" name=\"$var\" value=\"$val\"/>";
}
}
or
$_SESSION['get'] = $_GET;
then you can use $_SESSION['get'] as if it were $_GET later on

PHP loop within a loop, option selected?

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?

Categories