Page 1 of 1

Returning Array values from an include.

Posted: Thu May 22, 2003 9:52 pm
by Etherguy
Okay.. what I have is a function that reads quotes from a database and lists them as per user. I want to take those values and plug them into another script that will do a lookup on them. It is not working.. The Array is doing it's job and outputting the data fine, but all that is returned to the main screen is either a value of "Array" or nothing at all.

Here is the function located in a file called functions.php

Code: Select all

<?php
function Check_Quotes($username){
$sql = mysql_query("SELECT stock_sym FROM module_stocks  WHERE uname ='$username'");
$numrows = mysql_num_rows($sql);
if($numrows != "0"){
print " Here is the current list of active stocks for $username<br>";
while (list ($stock_sym) = mysql_fetch_row ($sql)) {
   print ("$stock_sym  \n");
$tickers=array();
array_push($tickers, $stock_sym);
}
return $tickers;
}
?>
The main script that calls it does it like so...

Code: Select all

<?php
include('includes/functions.php');
$tickers = Check_Quotes($username);

print "here is $tickers";
?>
Any help would be greatly appreciated :)

Regards.

Posted: Thu May 22, 2003 11:45 pm
by scorphus
When you echo an array, instead of its content you get the word 'Array' printed. Take the following script as an example:

Code: Select all

<?php
$array = array(1, 2, 3, 4, 5, 6);
echo $array;
echo "\n\nContents of \$array:\n";
print_r($array);
?>
which outputs:

Code: Select all

Array

Contents of $array:
Array
(
    &#1111;0] =&gt; 1
    &#1111;1] =&gt; 2
    &#1111;2] =&gt; 3
    &#1111;3] =&gt; 4
    &#1111;4] =&gt; 5
    &#1111;5] =&gt; 6
)
Your function returns an array, so it should be working. All you have to do is interpret this array and extract the crucial data from it.

Take a look at the Variable and Array functions of the PHP Manual. They are a 'hand on the wheels'.

Cheers,
Scorphus.

Posted: Fri May 23, 2003 6:21 am
by tr3s

Code: Select all

<?php
function Check_Quotes($username){ 
$sql = mysql_query("SELECT stock_sym FROM module_stocks  WHERE uname ='$username'"); 
$numrows = mysql_num_rows($sql); 
if($numrows != "0"){ 
print " Here is the current list of active stocks for $username<br>"; 
$tickers=array();
while (list ($stock_sym) = mysql_fetch_row ($sql)) { 
   print ("$stock_sym  \n");  
array_push($tickers, $stock_sym); 
} 
return $tickers; 
}

$tickers = Check_Quotes($username)
print_r($tickers); 
?>

Posted: Fri May 23, 2003 6:36 am
by twigletmac
tr3s - did you read what scorphus posted?

Mac

Posted: Fri May 23, 2003 7:42 am
by tr3s
Mac,

sorry for that wreckless post but one bug for Etherguy's script is that $tickers=array();inside the while loop, (that i should have emphasized) that actually don't push data into the array. the print_r portion here is already mentioned by scrophus so we all should be happy with that.

running the original script produces an array with only one element which makes no sense so i believe this is one thing that Etherguy has overlooked. bury me if i'm wrong... Etherguy

again, sorry for that wild post; but i hope i explained my part!

Posted: Fri May 23, 2003 7:54 am
by twigletmac
Thanx :D - it's difficult to spot differences in code when there's no explanation, ta for expanding on what you were trying to get across.

Mac

1 Variable

Posted: Fri May 23, 2003 7:54 am
by Etherguy
Seems to only return one value back. I need all the values... I have looked at the functions manual on php.net extensivly. I must simply be missing something.

Posted: Fri May 23, 2003 8:00 am
by twigletmac
Check out what tr3s posted - in your original code you have this line in the while loop:

Code: Select all

$tickers=array();
and that has the effect of overwriting the array each time the loop runs, therefore you only get one value.

Mac

Caught it.

Posted: Fri May 23, 2003 8:16 am
by Etherguy
Mac,

I caught that on the original post (thanx tr) but for some reason only the first value gets displayed. This is what I currently have

Code: Select all

<?php
function Check_Quotes($username){
$sql = mysql_query("SELECT stock_sym FROM module_stocks  WHERE uname ='$username'");
$numrows = mysql_num_rows($sql);
if($numrows != "0"){
print " Here is the current list of active stocks for $username<br>";
$tickers = array();
while (list ($stock_sym) = mysql_fetch_row ($sql)) {
   print ("$stock_sym  \n");

array_push($tickers, $stock_sym);
}
return $tickers;
}

?>
And on the main script I have this.

Code: Select all

<?php

$tickers = Check_Quotes($username);

list ($tickers)= $tickers;

print_r ($tickers);


$tickers = explode(" ", $tickers);


?>
The explode portion is just the begining of the script that will do the look-ups. The prints are strictly for debugging, which is how i know only one value is being passed.

Regards.

Posted: Fri May 23, 2003 8:25 am
by twigletmac
Do you get the full list of stocks printed when the function is running since you've got a print statement in there too?

It looked like there was a brace missing in the code you posted, maybe try this:

Code: Select all

<?php 
function Check_Quotes($username){ 
	$sql = mysql_query("SELECT stock_sym FROM module_stocks  WHERE uname ='$username'"); 
	$numrows = mysql_num_rows($sql); 
	if($numrows != 0) { 
		print "Here is the current list of active stocks for $username<br>"; 
		$tickers = array(); 
		while (list($stock_sym) = mysql_fetch_row($sql)) { 
			print "$stock_sym  \n"; 
			$tickers[] = $stock_sym; 
		}
	} 
	// check the array before leaving the function
	echo '<pre>';
	print_r($tickers);
	echo '</pre>';

	return $tickers; 
} 

?>
Mac

Posted: Fri May 23, 2003 8:49 am
by Etherguy
Brace is there I just didn't bother showing it since it is on the back side on an else statement. At any rate, I do get a full list before leaving the array with the print_r($tickers) statement. but still only the first stock is printed on the main screen. perhaps I need a foreach or while statement on the main script?

Regards.

Posted: Fri May 23, 2003 9:08 am
by twigletmac
Oops, missed this:

Code: Select all

list ($tickers)= $tickers;
by doing that you create a new variable called $tickers which contains only the first element of the array returned from the function.
Try:

Code: Select all

<?php 
$tickers = Check_Quotes($username); 
print_r ($tickers); 

// Make the array into a string using implode()
$ticker_list = implode(' ', $tickers); 
echo $ticker_list;
?>
Mac

Posted: Fri May 23, 2003 9:21 am
by Etherguy
That did it!

Appreciate all the help.

Regards.