Page 1 of 1

Push and Pop With array - strange problem

Posted: Tue Mar 27, 2007 6:04 pm
by waradmin
Here is my full code:

Code: Select all

<?php include("../config.php"); ?> 
<?php 
function search() { //start of the search function 
        //begin variables 
        //we will add some security checks sometime 
        $section  = $_GET['section']; 
        $term     = $_GET['term']; 
        $sendback = ''; //this variable is what we will use to return values 
        $table = ''; 
        //end variables 
        
        /* 
                This array defines: field => table 
        */ 
        $whatwhere = array( //begin array 
                'sex'                   => 'mini_info', 
                'interested_in'         => 'mini_info', 
                'relationship_status'   => 'mini_info', 
                'looking_for'           => 'mini_info', 
                'birthday'              => 'mini_info', 
                'hometown'              => 'mini_info', 
                'religious_views'       => 'mini_info', 
                'activities'            => 'personal_info', 
                'interests'             => 'personal_info', 
                'favorite_music'        => 'personal_info', 
                'favorite_tv_shows'     => 'personal_info', 
                'favorite_movies'       => 'personal_info', 
                'favorite_books'        => 'personal_info', 
                'favorite_quotes'       => 'personal_info', //probably will never get used 
                'about_me'              => 'personal_info', //probably will never get used 
                'high_school'           => 'education_info', 
                'high_school_grad_year' => 'education_info', 
                'college'               => 'education_info', 
                'college_grad_year'     => 'education_info', 
                'company'               => 'work_info', 
                'position'              => 'work_info', 
                'location'              => 'work_info', 
                'description'           => 'work_info' 
        ); //end array 
        $table = $whatwhere[$section]; //take in the section, get the table, and return it as $table    
        //core of searching 
        $sendback = array(); //create array 
        $result = mysql_query("SELECT global_id FROM $table WHERE $section='$term'") or die(mysql_error()); 
        while( $row = mysql_fetch_assoc( $result ) ){ //begin the loop 
                array_push($sendback, $row['global_id']); //push onto the array 
        } //end loop 
        print_r($sendback);
        /* 
                Now we need to build the array of information that will be displayed. 
                Doing so will be simple, take the global idea and build an array with 
                the values coupled with it. 
        */ 
        $gblArray = array(); 
        $fnArray = array(); 
        $lnArray = array(); 
        $result = mysql_query("SELECT global_id, Fname, Lname FROM loginphp WHERE global_id IN(". implode(', ', $sendback).")") or die(mysql_error());
        $num_rows = mysql_num_rows( $result ); //total rows 
        while( $row = mysql_fetch_assoc( $result ) ){  //begin the loop to build array 
                array_push($gblArray, $row['global_id']); 
                array_push($fnArray, $row['Fname']); 
                array_push($lnArray, $row['Lname']); 
        } //end loop 
		echo "<br />Now we are going to try to display the array's: <br />";
		print_r($gblArray);
		echo "<br />";  
		print_r($fnArray);
		echo "<br />";
		print_r($lnArray);  
		echo "<br />";
		$i = 0;
		while($gblArray) { //while gblArray still has values
			$tmpGid = $gblArray[$i];
			$tmpFn  = $fnArray[$i];
			$tmpLn  = $lnArray[$i];
			echo "$tmpGid: $tmpFn, $tmpLn <br />"; //display the temporary values
			array_pop($gblArray); //pop off of gblArray
			$i++; //incrememnt i
		}
} //end search function 
?> 
<?php 
        echo "Doing search function: <br />"; 
        search(); 
?>
Here is the generated output:

Code: Select all

Doing search function: 
Array ( [0] => 8511657 [1] => 7676666 [2] => 1646570 [3] => 2633572 [4] => 2632321 ) 
Now we are going to try to display the array's: 
Array ( [0] => 8511657 [1] => 7676666 [2] => 1646570 [3] => 2633572 [4] => 2632321 ) 
Array ( [0] => Steve [1] => Gordon [2] => Test [3] => John [4] => Test ) 
Array ( [0] => Morrissey [1] => Freeman [2] => Member [3] => K [4] => User ) 
8511657: Steve, Morrissey 
7676666: Gordon, Freeman 
1646570: Test, Member 
: John, K 
: Test, User
It should look like

Code: Select all

Doing search function: 
Array ( [0] => 8511657 [1] => 7676666 [2] => 1646570 [3] => 2633572 [4] => 2632321 ) 
Now we are going to try to display the array's: 
Array ( [0] => 8511657 [1] => 7676666 [2] => 1646570 [3] => 2633572 [4] => 2632321 ) 
Array ( [0] => Steve [1] => Gordon [2] => Test [3] => John [4] => Test ) 
Array ( [0] => Morrissey [1] => Freeman [2] => Member [3] => K [4] => User ) 
8511657: Steve, Morrissey 
7676666: Gordon, Freeman 
1646570: Test, Member 
2633572 : John, K 
2632321 : Test, User
(notice how the global_id's for John K and Test User are missing in the real output?)

Whats wrong?

Notice how the 3 arrays under "Now we are going to display the array's:" match up when echo'ing them bellow (so the first [0] value goes with the other 2 array's [0] value, etc)? Well, the ID's are in the array, but for John K and Test User the ID's are not displaying. But why is this? I am simply trying to pop the values off the array but for some reason it stops poping the ID off. Whats wrong.

Thanks in advance, I have looked and found nothing wrong.

Note, main focus is on this not working properly:

Code: Select all

while($gblArray) { //while gblArray still has values 
                        $tmpGid = $gblArray[$i]; 
                        $tmpFn  = $fnArray[$i]; 
                        $tmpLn  = $lnArray[$i]; 
                        echo "$tmpGid: $tmpFn, $tmpLn <br />"; //display the temporary values 
                        array_pop($gblArray); //pop off of gblArray 
                        $i++; //incrememnt i 
                }

Posted: Tue Mar 27, 2007 9:34 pm
by John Cartwright
As I suggested previously, why don't we try and turn this mess into a single query (and loop). I find it really difficult to follow what your trying to do.

Posted: Tue Mar 27, 2007 11:13 pm
by waradmin
I did what you suggested to a point:

Code: Select all

$result = mysql_query("
	SELECT 
	loginphp.global_id, loginphp.Fname, loginphp.Lname, mini_info.profile_photo_url, mini_info.global_id 
	FROM 
	loginphp 
	JOIN 
	mini_info 
	ON 
	loginphp.global_id=mini_info.global_id 
	WHERE 
	loginphp.global_id 
	IN(". implode(', ', $sendback).")") or die(mysql_error()); 
	$num_rows = mysql_num_rows( $result ); //total rows 
	while( $row = mysql_fetch_assoc( $result ) ){  //begin the loop to build array 
		array_push($gblArray, $row['global_id']); //push onto gblArray
		array_push($fnArray, $row['Fname']);  //push onto fnArray
		array_push($lnArray, $row['Lname']);  //push onto lnArray
		array_push($pArray, $row['profile_photo_url']); //push onto pArray
	} //end loop
That is the query, pushes it all onto an array.

Here is me trying to assign the values to variables:

Code: Select all

$i = 0; //define the counter control
	while(each($gblArray)) { //while gblArray is still returning TRUE
		//define some temporary variables for ease of use
		$tmpGid = $gblArray[$i]; 
		$tmpFn  = $fnArray[$i]; 
		$tmpLn  = $lnArray[$i]; 
		$tmpPi  = $pArray[$i];
		echo "$tmpGid: $tmpFn, $tmpLn, $temPi <br />"; //display the temporary values 
		buildresults($tmpGid, $tmpFn, $tmpLn, $temPi);
		$i++; //incrememnt i (via i++)
	}
However when I attempt to echo out $temPi I get NOTHING. Every other value echo's fine but temPi gives me no value at all. It is in the form of:

Code: Select all

imagename.jpg
Could the .jpg be messing with the array or something? I just cant seem to get $temPi to echo at all, it just shows nothing. But if I echo out $row['profile_photo_url'] I see the result just fine, its like once I try to push to the array, it all goes to hell.