Page 1 of 1

Counter problem, arrays

Posted: Mon Feb 21, 2005 4:18 pm
by danf_1979
Hi again:
I've got the following problem:
I've got a counter with this code:

Code: Select all

for ($x = 1; $x <=8; $x++)&#123;
	if (!$obtengo_preguntas&#1111;$x]&#1111;0] == '')&#123;
		$idpregunta = $x;
	for ($z = 1; $z <= 11; $z++)&#123;
		if (!$form&#1111;$x]&#1111;$z] == '') &#123;
			$idrespuesta = $z;
			$query = "UPDATE enc_respuestas SET contador="(int)($obtengo_votos_respuestas&#1111;$x]&#1111;$z]&#1111;0] + 1)" WHERE (idvotacion='$idvotacion' AND idpregunta='$idpregunta' AND idrespuesta='$idrespuesta')"; 
    		$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
				&#125;
			&#125;
		&#125;
	&#125;
The problem is that I dont seem capable of adding one (1) to $obtengo_votos_respuestas[$x][$z][0], an array.

$obtengo_votos_respuestas = obtener_votos_respuestas ();

and the function obtener_votos_respuestas () is:

Code: Select all

function obtener_votos_respuestas () &#123;
global $idvotacion;
for ($x = 1; $x <= 8; $x++)&#123;				
	for ($z = 1; $z <= 11; $z++) &#123;
		$query_votos_respuesta = "SELECT contador FROM enc_respuestas WHERE idvotacion='$idvotacion' AND idpregunta='$x' AND idrespuesta='$z'";
		$result_votos_respuesta = mysql_query($query_votos_respuesta) or die ("Error in query: $query_votos_respuesta. ".mysql_error());
		while ($registro_votos_respuesta = mysql_fetch_row($result_votos_respuesta))
				&#123;
					$get_contador_respuesta&#1111;$x]&#1111;$z] = $registro_votos_respuesta;
									&#125;
			&#125;
		&#125;
	return $get_contador_respuesta;
&#125;
Any help would be appreciated... thanks

Posted: Mon Feb 21, 2005 4:30 pm
by feyd
you're likely to run out of database memory with all those queries flying around.. for instance your function can perform the entire set of queries in one simple query:

Code: Select all

SELECT contador FROM enc_respuestas WHERE idvotacion='$idvotacion' AND (idpregunta BETWEEN 1 AND 8) AND (idrespuesta BETWEEN 1 AND 11)
your update query is trying to use php code inside the query string...

Code: Select all

UPDATE enc_respuestas SET contador='" . ((int)($obtengo_votos_respuestas&#1111;$x]&#1111;$z]&#1111;0] + 1) . "' WHERE (idvotacion='$idvotacion' AND idpregunta='$idpregunta' AND idrespuesta='$idrespuesta'

Posted: Mon Feb 21, 2005 4:41 pm
by danf_1979
I'll check on that mysql query... hey, I dont know how to say thanks, but thanks.

Posted: Mon Feb 21, 2005 6:26 pm
by danf_1979
Well, I did test the mysql_query, and it did not work.
Here it is:

Code: Select all

for ($x = 1; $x <= 8; $x++)&#123;				
	for ($z = 1; $z <= 11; $z++) &#123;
		$query_votos_respuesta = "SELECT contador FROM enc_respuestas WHERE idvotacion='$idvotacion' AND idpregunta='$x' AND idrespuesta='$z'";
		$result_votos_respuesta = mysql_query($query_votos_respuesta) or die ("Error in query: $query_votos_respuesta. ".mysql_error());
		while ($registro_votos_respuesta = mysql_fetch_row($result_votos_respuesta))
				&#123;
					$get_contador_respuesta&#1111;$x]&#1111;$z] = $registro_votos_respuesta;
									&#125;
			&#125;
		&#125;
	return $get_contador_respuesta;
&#125;		
$test = obtener_votos_respuestas ();
echo $test&#1111;1]&#1111;1]&#1111;0];
returns: 2, according to the counter. It is correct.
With

Code: Select all

$query_votos_respuesta = "SELECT contador FROM enc_respuestas WHERE idvotacion='$idvotacion' AND (idpregunta BETWEEN 1 AND 8) AND (idrespuesta BETWEEN 1 AND 11)";
It returns cero (0).

This is my reading script:

Code: Select all

for ($x = 1; $x <= 8; $x++) &#123;
	if (!$obtengo_preguntas&#1111;$x]&#1111;0] == '')&#123;
		echo "<td align='left' valign='center'>";
		echo "<font color='$color_pregunta'><br><b>$x) ".$obtengo_preguntas&#1111;$x]&#1111;0]."</b><br><br></td></tr></font>";
		for ($z = 1; $z <= 11; $z++) &#123;
			if (!$obtengo_respuestas&#1111;$x]&#1111;$z]&#1111;0] == '')&#123;
				echo "<tr>";
				echo "<td align='left' valign='center'>";			
				echo "<b><font color='$color_numeracion'>$z-.  </font></b><font color='$color_respuesta'>".$obtengo_respuestas&#1111;$x]&#1111;$z]&#1111;0]."&nbsp;&nbsp;</td></font>";
				echo "<td align='left' valign='center'>";				
				echo "<font color='$color_votos'><b>".$obtengo_votos_respuestas&#1111;$x]&#1111;$z]&#1111;0]." votos&nbsp; </b><br></td></font>";
				echo "<td align='left' valign='center'>";
				for ($y = 0; $y < (int)($obtengo_votos_respuestas&#1111;$x]&#1111;$z]&#1111;0]*15/$max_votacion); $y++) &#123;
					echo "O";
					&#125;
				echo "</td>";
				echo "</tr><tr>";		
				&#125;
			&#125;
			echo "<td align='right' valign='center'>";	
			echo "<font color='$color_total'><b>TOTAL&nbsp;</font></b></td>";
			echo "<td align='center' valign='center'>";				
			echo "<font color='$color_total_num'><b>".$obtengo_votos_preguntas&#1111;$x]&#1111;0]."</b><br></td></font>";
			echo "</tr><tr>";
	&#125;
&#125;
I also checked putting the SELECT blabla between blabla and the mysql_query outside the for loop, without the expected results.

Posted: Mon Feb 21, 2005 6:32 pm
by danf_1979
Oh, in the last script, $obtengo_votos_respuestas[$x][$z][0] is the guilty guy...