Help using arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spudmclard
Forum Newbie
Posts: 16
Joined: Thu Oct 27, 2005 3:06 am

Help using arrays

Post 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";
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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!"
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Help using arrays

Post 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!
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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. :)
calevans
Forum Newbie
Posts: 4
Joined: Tue Jun 06, 2006 3:59 pm
Location: Nashville, TN

Re: Help using arrays

Post 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)
Post Reply