Page 1 of 1

Help using arrays

Posted: Wed Jun 07, 2006 10:45 am
by spudmclard
Hi
Im VERY new to PHP
Im trying to get my head around using arrays with php.
So far ive slapped together the following code but it doesnt output any results.
Id appreciate any help anyone can give with this as its doing my head in.
MAIN

Code: Select all

echo "<form method ='post' action='action.php'>";
echo "<TABLE>";
echo "<td align='center' bgcolor='#0E0E17'><input type='checkbox' name='arr[]' value='1' 1></td>";
echo "<td align='center' bgcolor='#0E0E17'><input type='checkbox' name='arr[]' value='2' 2></td>";
echo "<td align='center' bgcolor='#0E0E17'><input type='checkbox' name='arr[]' value='3' 3></td>";
echo "<input type='submit' value='-GO-'>";
echo "</table>";
action.php

Code: Select all

$checkboxarray = $_POST['arr[]'];
$count = 0;
while ($checkboxarray[$count]) {
	echo "$checkboxarray[$count]";
	echo "<br>";
	$count++;
} else {
echo "FINISHED";

Posted: Wed Jun 07, 2006 10:55 am
by GM
I haven't tested, but try:

Code: Select all

$checkboxarray = $_POST['arr'];

foreach($checkboxarray as $key=>$data) {
   echo "Checkbox $key = $data<BR \>";
}

echo "FINISHED!"

Re: Help using arrays

Posted: Wed Jun 07, 2006 10:55 am
by ok
spudmclard wrote:

Code: Select all

$checkboxarray = $_POST['arr[]'];
$count = 0;
while ($checkboxarray[$count]) {
	echo "$checkboxarray[$count]";
	echo "<br>";
	$count++;
} else {
echo "FINISHED";
Bad!!!

Code: Select all

$checkboxarray = $_POST['arr']; //arr(!) not arr[].
for($count = 0; $count < count($checkboxarray); $count++)
{
  echo "$checkboxarray[$count]";
  echo "<br>";
}
echo "FINISHED";
1. You cannot use "}else{" with "while()"!!!.
2. Use "for()" loop when you know how many times you need to loop!

Posted: Wed Jun 07, 2006 10:56 am
by Skittlewidth

Code: Select all

$_POST['arr[]']
should just be

Code: Select all

$_POST['arr']

$arr[] is shorthand for array_push($arr, $new_array_item);

Next try something like:

Code: Select all

$arr = $_POST['arr'];
foreach ($arr as $arrayitem){
echo $arrayitem."<br/>";
  
}
or

Code: Select all

$arr = $_POST['arr'];

for ($i = 0; $i < count($arr); $i++){
   echo $arr[$i];
}
I'd recommend you look at http://uk2.php.net/manual/en/language.c ... ctures.php. You seem to have the right idea, and maybe just need to look at the differences between each control structure. :)

Re: Help using arrays

Posted: Wed Jun 07, 2006 11:05 am
by calevans
spudmclard wrote:Hi
action.php

Code: Select all

$checkboxarray = $_POST['arr[]'];
$count = 0;
while ($checkboxarray[$count]) {
	echo "$checkboxarray[$count]";
	echo "<br>";
	$count++;
} else {
echo "FINISHED";
Ok, first, this code won't run. You've got an

Code: Select all

} else {
but no

Code: Select all

if (something) {
I think what you are trying to do is:

Code: Select all

<?PHP
if ($_POST) {
	// for debugging purposes only.
	echo "<pre>";
	var_dump($_POST);
	echo "</pre>";
}
for($lcvA=0;$lcvA<count($_POST['arr']);$lcvA++) {
        echo $_POST['arr'][$lcvA]." was checked.<br />";
}

?>
<hr />
<form method ='post' action='action.php'>
<TABLE border=1>
	<tr>
		<td align='center'><input type='checkbox' name='arr[]' value='CheckBox 1'> 1</td>
		<td align='center'><input type='checkbox' name='arr[]' value='CheckBox 2'> 2</td>
		<td align='center'><input type='checkbox' name='arr[]' value='CheckBox 3'> 3</td>
	</tr>
	<tr>
		<td align="center" colspan=3><input type='submit' value='-GO-'></td>
	</tr>
</table>
Sorry, I re-wrote a little of your code. The main thing you were missing is that the array comes in as

Code: Select all

$_POST['arr']
not

Code: Select all

$_POST['arr[]']
Paste all the code above in action.php, save it and call it. It calls itself to process.
HTH,
=C= 8)