Page 1 of 1

Strange foreach loop issues with my array

Posted: Sun Apr 16, 2006 1:47 am
by robster
Hi there, I am having troubles, to start with here is the code:

Code: Select all

//get the transactions that are of type 'service' and stick them in an array
	//===========================================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM transactions WHERE type = 'services' ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$ShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$ShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$transaction_type_id = $Xcontent["type_id"];  //get the service name
		$transaction_price = $Xcontent["price"];  //get the service price
		
		//stick the transaction id and price into an array
		$transaction_name_array["$transaction_type_id"] = $transaction_price;
		
	$Xcontent = mysql_fetch_array($content);
	}
		
	

	
	
	
		foreach($transaction_name_array as $type_id => $price)  
		{ 
			$temp_counter++;
			echo "<br>($temp_counter)type_id: $type_id, price: $price"; 
		}
Now it should be known (and can be proven :)) that the loop through the database provides over 3000 results. There are that many transactions where the type = 'services'.

The problem:
The array, when displaying it with the foreach loop at the bottom, only shows 28 transactions!!!! erm... why? I do have code above this that sticks data into another array (that puts exactly 28 transactions into that other array) but even when i completely REMOVE that earlier code, I still get it. Is it a memory problem? do i need to flush the variables or something strange like that?


I will provide the full code for the page here, for those that are interested in seeing it in its whole:

Thanks, any advice apprecaited,

Rob

Code: Select all

//set up counters and misc vars
	$temp_counter = "0";
	
	
	//connect to database
	$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
	
	
	//get the service names database and stick it in an array
	//========================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM services ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$service_name = $Xcontent["name"];  //get the service name
		$service_id = $Xcontent["id"];  //get the service name
		
		//stick the service name into an array
		$service_name_array["$service_name"] = $service_name;
		$service_id_array["$service_id"] = $service_id;
		
	$Xcontent = mysql_fetch_array($content);
	}
	
	
	
	//get the transactions that are of type 'service' and stick them in an array
	//===========================================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM transactions WHERE type = 'services' ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$ShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$ShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$transaction_type_id = $Xcontent["type_id"];  //get the service name
		$transaction_price = $Xcontent["price"];
		
		//stick the transaction id into an array
		$transaction_name_array["$transaction_type_id"] = $transaction_price;
		
	$Xcontent = mysql_fetch_array($content);
	}
		
	

	
	
	
		foreach($transaction_name_array as $type_id => $price)  
		{ 
			$temp_counter++;
			echo "<br>($temp_counter)type_id: $type_id, price: $price"; 
		}

Posted: Sun Apr 16, 2006 2:01 am
by AKA Panama Jack
Your database may have 3000 results but the way you are doing things I bet there are only 28 different transaction_type_id's.

Code: Select all

$transaction_name_array["$transaction_type_id"] = $transaction_price;
That line will overwrite any previous entries with the same transaction_type_id.

Posted: Sun Apr 16, 2006 2:50 am
by robster
that was it!

Tahnks so much, it now works and looks like this:


Rob

Code: Select all

//get the transactions that are of type 'service' and stick them in an array
	//===========================================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM transactions WHERE type = 'services' ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$ShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$ShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$transaction_id = $Xcontent["id"];  //get the service name
		$transaction_type_id = $Xcontent["type_id"]; 
		$transaction_price = $Xcontent["price"];
		
		//stick the transaction id into an array
		$transaction_name_array["$transaction_id"] = $transaction_type_id;
		
	$Xcontent = mysql_fetch_array($content);
	}
		
	

	
	
	
		foreach($transaction_name_array as $type_id => $price)  
		{ 
			$temp_counter++;
			echo "<br>($temp_counter)type_id: $type_id, price: $price"; 
		}