Page 1 of 2

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

Posted: Sun Feb 20, 2005 9:52 pm
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.

Posted: Sun Feb 20, 2005 9:56 pm
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');

Posted: Sun Feb 20, 2005 9:57 pm
by timvw
you have a return statement in a foreach loop... why?

Posted: Sun Feb 20, 2005 9:58 pm
by feyd
:oops: I didn't see that part.. damn my speed reading. :P

Posted: Sun Feb 20, 2005 10:05 pm
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.

Posted: Sun Feb 20, 2005 10:06 pm
by feyd
you have to capture the returned data.. it doesn't just magically appear as the same when you returned it.

Posted: Sun Feb 20, 2005 10:10 pm
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.

Posted: Sun Feb 20, 2005 10:19 pm
by feyd
my first reply has an example of capturing the returned data... along with many many many of the examples on php.net

Posted: Sun Feb 20, 2005 10:22 pm
by danf_1979
Thanks, i will test for that tomorrow, i have to sleep know. Thanks, again.
Daniel.

Posted: Mon Feb 21, 2005 7:34 am
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?

Posted: Mon Feb 21, 2005 7:35 am
by danf_1979
I also tested the return command in other places, with no better results....

Posted: Mon Feb 21, 2005 8:46 am
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;

Posted: Mon Feb 21, 2005 9:44 am
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...

Posted: Mon Feb 21, 2005 9:45 am
by danf_1979
And there are currently only 3 questions in mysql database...

Posted: Mon Feb 21, 2005 9:49 am
by feyd
the foreach is useless. print_r($get_edit_pregunta);