Page 1 of 1

loop problem [solved]

Posted: Mon Apr 10, 2006 1:00 pm
by dougsm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello folks!
I´m a beginner at this php stuff programming. Could anyone explain me why this variable $z at second loop never change? $z = 0 forever at the second loop, but at the first $z works fine...
Why php don´t allow you to use a variable outside of the loop?
Thank you guys!

Code: Select all

$i = 1;
		$z = 0;
		while ($z < count($arrTipos)) { //first loop
			while ($row = mysql_fetch_assoc($rs)) {	//second loop
				print_r($row);
				if ($i <= 2) {
					if ($row["idTipo"] == $arrTipos[$z]["idTipo"]) {
						echo $z."-".$row["idTipo"]."<br />";
						$i = $i + 1;
					}
				} else {
					//next($arrTipos);
					$i = 1;
				}
			}
			//echo $arrTipos[$z]["Tipo"]."<br />";
			$z = $z + 1;
		}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Apr 10, 2006 1:08 pm
by feyd
your code only alters $z after the inner loop completes a run.

loop problem

Posted: Tue Apr 11, 2006 5:43 pm
by dougsm
I know, but at the second run wouldn´t $z = 1 and so on? It´s always $z = 0....
i don´t understand why...

Posted: Tue Apr 11, 2006 5:53 pm
by feyd
Your code is written such that all records found in the query will be processed in the first pass. There are no other fields to process afterward.

loop problem

Posted: Tue Apr 11, 2006 6:02 pm
by dougsm
But the second loop will run every time the first loop runs, won´t it? I mean, no matter how many times the second loop runs, but the first should runs about 6 times I think. And every time the first runs, the second should be altered too...
Sorry if I´m wrong, I just can´t figure out the problem...

Posted: Tue Apr 11, 2006 6:57 pm
by feyd
The inner loop will attempt to run as many times as the outer loop runs, however after the first run, mysql_fetch_assoc() will return false, thus stopping the code from running the inner loop.

loop problem

Posted: Tue Apr 11, 2006 7:33 pm
by dougsm
Oh I see, do you have any suggestion? I´m trying to get the information checking the idTipo (a kind of class), want to display 2 of each idTipo from a giant array. The array is something like this:

Code: Select all

array(array 0 ("idTipo" => 1, "Tittle" => "teste", "Date" =>"05/06/2005"), array 1("idTipo" => 1, "Tittle" => "teste", "Date" =>"05/06/2005") );
I´m trying to search for examples on google, but have no success yet....

loop problem [solved]

Posted: Mon Apr 24, 2006 4:33 pm
by dougsm
Well, finally I´ve got a solution. It´s a little big, but at least I can load the whole page with only 2 queries. I hope you enjoy.

Code: Select all

$sql = "SELECT * FROM tbTipos WHERE ExibeHome=1 Order BY OrdemTipo asc "; 

	$rs = mysql_query($sql)
			  or die("Couldn't execute query."); 
				
	$Nreg = mysql_num_rows($rs); 

	$arrTipos = array();

	if ($Nreg > 0)  {
		while ($row = mysql_fetch_assoc($rs)) {		//build an array arrTipos
			array_push($arrTipos, $row["idTipo"]);
		}
	}

	$sql = "SELECT * FROM tbMaterias INNER JOIN tbTipos ON tbMaterias.IdTipo = tbTipos.IdTipo WHERE tbTipos.ExibeHome=1 Order BY DataMateria desc  "; 
		   
	$rs = mysql_query($sql)
			  or die("Couldn't execute query."); 
				
	$Nreg = mysql_num_rows($rs); 
	
	if ($Nreg > 0)  {
	
		$arrTiposTemp = array();
		$arrRows = array();
			
			while ($row = mysql_fetch_assoc($rs)) {		//select just 2 rows of each idTipo
			
				array_push($arrTiposTemp, $row["idTipo"]);

				$arrCountTemp = array_count_values($arrTiposTemp);

				if ($arrCountTemp[$row["idTipo"]] <= 2) {
					array_push($arrRows,  $row);
				}
			}

			

			$Pickfloat = 0; //controla a posição do float no stylesheet
			$z = 0; //controla a aparição de UL
			$html = "";

			foreach ($arrTipos as $key => $value) {
				for ($i = 0; $i < count($arrRows); $i++) {
					if ($arrTipos[$key] == $arrRows[$i]["idTipo"]) {
							$mudaUL = 1;
							
							if ($z % 2 == 0) { // changes UL for layout effects

								if ($Pickfloat == 0) {
									$float = "left";
									$Pickfloat = 1;
								} else {
									$float = "right";
									$Pickfloat = 0;
								}
								
								$ul = '<ul style="float:'.$float.'; background: url(images/layout/'.$arrRows[$i]["ImagemTipo"].') no-repeat left top;">';
								
							} else {
								$mudaUL = 0;
							}

							if ($mudaUL == 1) {
								$html = $html.$ul;
							}
							
							$html = $html.'<li><a title="'.$arrRows[$i]["TituloMateria"].'" href="#">'.$arrRows[$i]["TituloMateria"].'</a><br />'.$arrRows[$i]["ResumoMateria"].'</li>';

							if ($mudaUL == 0) {
								$html = $html."</ul>";
							}
							$z++;
					}
				}			
			}

			
	} else {
		$html = "";
	}