Page 1 of 1

Simple question - return function value as array

Posted: Mon Mar 26, 2007 3:25 pm
by waradmin
Im sure this is simple but I am asking anyway:

I have a function that builds an array from a query:

Code: Select all

<?php
//******************************************//
//This is the search function for project//
//******************************************//

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
	//end variables
	switch($section) { //begin section switch
		case "sex":
			switch($term) { //begin sex switch
				case "male":
					$result = mysql_query("SELECT global_id FROM mini_info WHERE sex='male'") or die(mysql_error());
					$num_rows = mysql_num_rows( $result ); //total rows
					$row = mysql_fetch_array( $result );
					$i = 0; //initialize count variable
					while($i <= $num_rows) { //begin the loop to build array
						$sendback[$i] = $row['global_id'];
						$i++; //increment counter
					} //end loop
				break;
				case "female":
				
				break;
			}
		break;

	} //end section switch
} //end search function

?>
How do I return all values of $sendback[] so that they can be used outside the function? Do I just do return $sendback[]; then echo them doing another loop, or is there a better way?

Posted: Mon Mar 26, 2007 4:09 pm
by feyd
Have you tried it?

Posted: Mon Mar 26, 2007 4:15 pm
by Kieran Huggins
remember, $sendback[] indicates an assignment... $sendback is the array.

Posted: Mon Mar 26, 2007 5:48 pm
by waradmin
I have decided to remove all switch statements as they were not needed, and I streamlined the whole code.

However I now am wondering how to pass array values into a query.

I am trying to build a query that takes each ID in the array $sendback and does a query on it. I would test all of this but my server is down and apache is having problem on my local system. I am throwing my idea's out of left field here, they are just guesses.

So: $sendback is an array, populated with the loop:

Code: Select all

while($i <= $num_rows) { //begin the loop to build array
		$sendback[$i] = $row['global_id'];
		$i++; //increment counter
	} //end loop
Now my query needs to fetch values Fname, Lname from the database for each id in array $sendback, however doing a for loop seems like it would be using WAY too many queries, would this work:

Code: Select all

$fnlArray = array(); //Define array
	$result = mysql_query("SELECT Fname, Lname FROM loginphp WHERE global_id='$sendback'") or die(mysql_error());
	$num_rows = mysql_num_rows( $result ); //total rows
	$row = mysql_fetch_array( $result );
	$i = 0; //initialize count variable
	while($i <= $num_rows) { //begin the loop to build array
		//here I will push onto the array, code to come.
		$i++; //increment counter
	} //end loop

Posted: Mon Mar 26, 2007 5:57 pm
by John Cartwright

Code: Select all

$result = mysql_query("SELECT Fname, Lname FROM loginphp WHERE global_id IN(". implode(', ', $sendback).")") or die(mysql_error());

Posted: Mon Mar 26, 2007 9:55 pm
by waradmin
Alright, so I think I got the structure set up as I want it, now it just comes to populating the arrays:

Code: Select all

$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
	$row = mysql_fetch_array( $result );
	$i = 0; //initialize count variable
	while($i <= $num_rows) { //begin the loop to build array
		$tmp1 = array('$i' => $row['global_id']);
		$tmp2 = array('$i' => $row['Fname']);
		$tmp3 = array('$i' => $row['Lname']);
		array_push($gblArray, $tmp1);
		array_push($fnArray, $tmp2);
		array_push($lnArray, $tmp3);
		echo $gblArray[$i];
		$i++; //increment counter
	} //end loop
I am trying to build 3 arrays, 1 for global_id, one for Fname, and one for Lname. Then populate them numbering them from $i = 0 to $num_rows in the first spot in the array, then the database query result in the second spot.

So say the query produced:
GLOBAL_ID | FNAME | LNAME
1234 | Steve | Jackson
4321 | Bob | Griffin
3241 | John | Locke

I would want gblArray to have in it:
[0] => 1234
[1] => 4321
[2] => 3241

fnArray:
[0] => Steve
[1] => Bob
[2] => John

lnArray:

[0] => Jackson
[1] => Griffin
[2] => Lock

So in turn I could echo them, for example echo $gblArray[0] and see 1234 as the result. However when I do it I am seeing the word Array as the result. Why is this?

Posted: Mon Mar 26, 2007 10:39 pm
by Kieran Huggins

Code: Select all

$result = mysql_query("SELECT global_id, Fname, Lname FROM loginphp WHERE global_id IN(". implode(', ', $sendback).")") or die(mysql_error());
while( $row = mysql_fetch_assoc( $result ) ){
     array_push($gblArray, $row['global_id']);
     array_push($fnArray, $row['Fname']);
     array_push($lnArray, $row['Lname']);
}
?

Posted: Mon Mar 26, 2007 10:39 pm
by waradmin
Yeah, i noticed lol, im sorry I am running on 3 hours of sleep.

Posted: Mon Mar 26, 2007 10:57 pm
by waradmin
Sorry for the double post but this is driving me nuts. Yeah, I have gotten 3 hours of sleep but I feel like I am losing my mind:

Code: Select all

<?php include('../top.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
	echo "THIS IS THE FAIL POINT";
	$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
	
	/*
		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
	$row = mysql_fetch_array( $result );
	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		
} //end search function
?>
<?php
	echo "Doing search function: <br />";
	search();
?>
Firsfox just shows the page as loading and does not display anything as if it is stuck in a infinite loop, but I dont see where that would be.

Posted: Mon Mar 26, 2007 11:06 pm
by John Cartwright
Without looking too closely at the problem (sorry just visiting for a moment), why not just perform a single join instead of all this looping? Unless I don't quite understand what your doing..

You shoud also consider passing your request variables through mysql_real_escape_string() to avoid any sql injection

Posted: Mon Mar 26, 2007 11:18 pm
by waradmin
My abilities with MySQL as far as off-the-basics useage is shaky at best. I have had bad luck doing joins and likely would fail here. However it seems that in my testing I crashed my web-host's server, so I am sure there will be hell to pay.