Page 1 of 1

Why array don't contain all times?

Posted: Thu Oct 14, 2004 12:35 pm
by ljCharlie
Can anyone tell why this doesn't work?

Code: Select all

<?php
	if ($totalRows_rsSection >0 && $pixID == ""){
		if ($numOfCells ==1){
			//echo "<br>";			
			echo '<table width="535" border="0" cellpadding="0" cellspacing="0">';
			$numbOfRows = 1;
			$numbCellsFilled = 1;
			}
		while($row_rsSection = mysql_fetch_assoc($rsSection)){
			if ($numbOfRows == 1){
				echo '<tr>';
				$numbOfRows = 0;
				}		
			$fileName = $row_rsSection["fileName"];
			$location = $row_rsSection["path"];
			$picID = $row_rsSection["imgID"];
			$sectID = $row_rsSection["sectionID"];
			if ($numbCellsFilled <= 3){
				createThumbNail($picID, $self, $sectID, $location, $fileName);
				$numbCellsFilled++;
				}
			elseif($numbCellsFilled ==4){
				createThumbNail($picID, $self, $sectID, $location, $fileName);
				echo '</tr>';
				$numbCellsFilled = 1;
				$numbOfRows = 1;
				}
			$numPicsID = array($picID);
			}
			foreach($numPicsID as $value){
				echo "num pics ID: ".$value."<br>";
				}
		echo '</table>';
		}
?>
My question is why when I have this, $numPicsID = array($picID);, as show above, only the last item is store in array. However, if I put this same code, $numPicsID = array($picID);, in the function, createThumbNail($picID, $self, $sectID, $location, $fileName);, then the array does store all of the items. Should either case be working? All I care is so I can get all the items into the array and access that variable from other place on the page.

ljCharlie

Posted: Thu Oct 14, 2004 2:53 pm
by mudkicker
you have to do :

Code: Select all

<?php
$numPicsID[] = $picID;
?>

Posted: Fri Oct 15, 2004 3:09 am
by twigletmac
By using

Code: Select all

$numPicsID = array($picID);
you keep overwriting the value of $numPicsID with a new array as mudkicker rightly points out, you need to add to the array instead:

Code: Select all

$numPicsID[] = $picID;
Mac

Posted: Fri Oct 15, 2004 3:09 am
by mudkicker
yeah this time i was faster ;)