Push and Pop With array - strange problem
Posted: Tue Mar 27, 2007 6:04 pm
Here is my full code:
Here is the generated output:
It should look like
(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
<?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();
?>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, UserCode: 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, UserWhats 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
}