Functions, Fors, While, Foreach, Arrays: Problem [SOLVED]

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

danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Functions, Fors, While, Foreach, Arrays: Problem [SOLVED]

Post by danf_1979 »

I got this problem with my code. If the code is not in a function I can use the result variable in other parts of the script with no problems.
This is the code:

Code: Select all

if ($edit !== 0){
	for ($x = 1; $x <= 8; $x++) &#123;
	$query_edit_pregunta = "SELECT pregunta FROM enc_preguntas WHERE idvotacion='$edit' AND idpregunta='$x'";
	$result_edit_pregunta = mysql_query($query_edit_pregunta) or die ("Error in query: $query_edit_pregunta. ".mysql_error());
		while ($registro_edit_pregunta = mysql_fetch_row($result_edit_pregunta))
			&#123;
				foreach($registro_edit_pregunta as $clave6&#1111;$x])
					&#123;
						$get_edit_pregunta&#1111;$x]=$clave6&#1111;$x];

					&#125;
				&#125;
			&#125;
		&#125;
As you can see, it is no function, and i can call with no problemas the $get_edit_pregunta[$x] variable with a for loop in other part of the script. BUT if I use a function like:

Code: Select all

function test ($edit) &#123;
if ($edit !== 0)&#123;
	for ($x = 1; $x <= 8; $x++) &#123;
	$query_edit_pregunta = "SELECT pregunta FROM enc_preguntas WHERE idvotacion='$edit' AND idpregunta='$x'";
	$result_edit_pregunta = mysql_query($query_edit_pregunta) or die ("Error in query: $query_edit_pregunta. ".mysql_error());
		while ($registro_edit_pregunta = mysql_fetch_row($result_edit_pregunta))
			&#123;
				foreach($registro_edit_pregunta as $clave6&#1111;$x])
					&#123;
						$get_edit_pregunta&#1111;$x]=$clave6&#1111;$x];
                                                return $get_edit_pregunta&#1111;$x];
					&#125;
				&#125;
			&#125;
		&#125;
&#125;

I cannot get anymore $get_edit_pregunta[$x]. I dont know so much about functions, I been programming in PHP for about 5 days.
The code I use to read the variable is this one, and as i said, it works OK if the above code is not embeded in a function:

Code: Select all

for ($z=1; $z <=8; $z++) &#123;
	echo "</tr><tr>";
	echo "<td align='left'><font color='$color_numeracion'><b>$z</b>)</td></font>";
	
		if ($edit !== 0)&#123;				
			echo "<td align='left'><INPUT type='text' name='sub_pregunta&#1111;$z]' size='35' maxlength='50' value='$get_edit_pregunta&#1111;$z]'></td>";
		&#125;
		else &#123;
			echo "<td align='left'><INPUT type='text' name='sub_pregunta&#1111;$z]' size='35' maxlength='50' value=''></td>";
	&#125;
&#125;
Any help will be very appreciated. Thanks in advance.
Last edited by danf_1979 on Mon Feb 21, 2005 2:14 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to use data that is created/modified in a function, the function must return the data in some fashion. Either through a passed variable reference, or through the return statement.

Code: Select all

function function_name($argument1)
&#123;
  // do stuff
  return $resultant_data;
&#125;

$result = function_name('blah');
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you have a return statement in a foreach loop... why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: I didn't see that part.. damn my speed reading. :P
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

timvw wrote:you have a return statement in a foreach loop... why?
I've tested the return in other parts too, with no success. I've put the return in the for loop only and the result is the same, i can`t read the $get_edit_pregunta[$x] variable. I dont really know what to do with that, and tutorials and other info have been no help this time.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to capture the returned data.. it doesn't just magically appear as the same when you returned it.
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Any readings about that you could refer to me? or any command you could mention so i could search for more info... please. Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my first reply has an example of capturing the returned data... along with many many many of the examples on php.net
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Thanks, i will test for that tomorrow, i have to sleep know. Thanks, again.
Daniel.
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Ok I'm back again.
I'll going to show my tests here, so you can tell me what you think about the problem:

Code: Select all

function prueba ($edit) &#123;
if ($edit !== 0)&#123;
	for ($x = 1; $x <= 8; $x++) &#123;
	$query_edit_pregunta = "SELECT pregunta FROM enc_preguntas WHERE idvotacion='$edit' AND idpregunta='$x'";
	$result_edit_pregunta = mysql_query($query_edit_pregunta) or die ("Error in query: $query_edit_pregunta. ".mysql_error());
		while ($registro_edit_pregunta = mysql_fetch_row($result_edit_pregunta))
			&#123;
				foreach($registro_edit_pregunta as $clave6&#1111;$x])
					&#123;
						$get_edit_pregunta&#1111;$x]=$clave6&#1111;$x]; 

					&#125;
return $get_edit_pregunta&#1111;$x];		&#125;

			&#125;

		&#125; 
	
	&#125; 

	
$var1 = prueba ($edit);
echo $var1."<br>";

	
$var&#1111;$x] = prueba ($edit);
for ($x = 1; $x <= 8; $x++) &#123;
echo $var&#1111;$x];
&#125;
In both cases, $var1 and for $var[$x] returns only the first registry in mysql field pregunta from table enc_preguntas. If I dont use functions, then I can get all mysql values using a FOR loop and not just the first like now happens...
Any Ideas?
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

I also tested the return command in other places, with no better results....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$get_edit_pregunta[$x] is a single element in an array. Why would you expect more?

At the end of the function (after the loops) return $get_edit_pregunta.

you can lose the foreach.. it'll mess up the results. I'd also recommend not looping the query. Instead use the MySQL operator 'IN' after combining all the numbers you wish to read from.

rough, nonworking example

Code: Select all

while($registro_edit_pregunta...)
  $get_edit_pregunta&#1111;] = $registro_edit_pregunta;
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

But with this code

Code: Select all

if ($edit !== 0)&#123;
	for ($x = 1; $x <= 8; $x++) &#123;
	$query_edit_pregunta = "SELECT pregunta FROM enc_preguntas WHERE idvotacion='$edit' AND idpregunta='$x'";
	$result_edit_pregunta = mysql_query($query_edit_pregunta) or die ("Error in query: $query_edit_pregunta. ".mysql_error());
		while ($registro_edit_pregunta = mysql_fetch_row($result_edit_pregunta))
			&#123;
				foreach($registro_edit_pregunta as $get_edit_pregunta&#1111;$x])
					&#123;
					echo $get_edit_pregunta&#1111;$x]."<br>";

					&#125;
				&#125;
			&#125;
		&#125;
I get this output:
¿Qué busca en nuestro sitio web?
¿Cómo conoció nuestra empresa?
A usted le interesaría…
That are $get_edit_pregunta[1], $get_edit_pregunta[2] and $get_edit_pregunta[3]

With this code:

Code: Select all

if ($edit !== 0)&#123;
	for ($x = 1; $x <= 8; $x++) &#123;
	$query_edit_pregunta = "SELECT pregunta FROM enc_preguntas WHERE idvotacion='$edit' AND idpregunta='$x'";
	$result_edit_pregunta = mysql_query($query_edit_pregunta) or die ("Error in query: $query_edit_pregunta. ".mysql_error());
		while ($registro_edit_pregunta = mysql_fetch_row($result_edit_pregunta))
			&#123;
				 $get_edit_pregunta&#1111;] = $registro_edit_pregunta;

					&#125;
				&#125;
			&#125;

for ($x = 1; $x <= 8; $x++) &#123;			 
echo "&#123;$get_edit_pregunta&#1111;$x]&#125;<br>";
&#125;
I get this output:
Array
Array
And notice I only get 2 arrays, and not 3.
So I guess the foreach loop is neccesary? But I dont know really...
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

And there are currently only 3 questions in mysql database...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the foreach is useless. print_r($get_edit_pregunta);
Post Reply