... foreach() on line 5

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
Larucha3000

Mon May 13, 2013 6:58 pm

:-x I'm reading a book because I want to learn something about php. In chapter 8 teach something about the forms, then I copied and ran the sample code, but it shows an error.
This is the php code, the 'warning' error is at line 8.
I can't see what is happen.. If you ask me I can put here the html code too. Thanks!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.wc3.org/TR/html4/loóse.dtd">
<html>
<body>
<p>Datos introducidos:</p>
<?php foreach ($_GET as $indice => $valor) {echo "$indice: $valor<br>";}
echo "<br>GUSTOS:<br>";
$gustos = $_GET["gustos"];
foreach ( $gustos as $indice => $valor) {
echo "$indice : $valor<br>";
}

?>
</body>
</html>

pbs
New php-forum User
New php-forum User
Posts: 53
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India
Contact:

Mon May 13, 2013 7:19 pm

to pass data type in foreach it must be an array, so please pass proper array in foreach on line

Code: Select all

$gustos = $_GET["gustos"];
foreach ($gustos as $indice => $valor) { // code }

Larucha3000

Mon May 13, 2013 11:48 pm

I can´t understand what you are trying to explain me.
Last edited by Larucha3000 on Tue May 14, 2013 10:35 am, edited 4 times in total.


User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Tue May 14, 2013 12:03 pm

Hi,

Refer the php manual for foreach control structure.
http://php.net/manual/en/control-structures.foreach.php

below is one example:
<?php
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>


So whatever you pass to foreach should be an array or object.

In below your code $_GET["gustos"] should be an array.
$gustos = $_GET["gustos"];
foreach ( $gustos as $indice => $valor) {
echo "$indice : $valor<br>";
}

you can do gettype($_GET["gustos"]); to check whether its an array or not.
you can do print_r($_GET["gustos"]); to get what are the values of this array.

Thanks,
Sandeep Agrawal
Mindfire Solutions.

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Tue May 14, 2013 4:02 pm

please read and learn about arrays in php, what is an array, how to access elements within an array, how to loop through through the array etc etc

Larucha3000

Tue May 14, 2013 5:18 pm

Thanks!
I tried these functions and they worked. Finally the code was fine when I put something in parentheses.
Finally was:

$gustos= ( $_GET ["gustos"] );

And the foreach ( ), shows:

0: checkbox 1
1: checkbox 3

While checkbox one and three were checked.

Thanks! I will come again if I found more fails in the book.
A dedicated student.

Larucha3000

Tue May 14, 2013 5:19 pm

yeah!! That's it.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests