Returning Array values from an include.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Returning Array values from an include.

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
tr3s
Forum Newbie
Posts: 17
Joined: Mon May 19, 2003 10:29 am
Location: Philippines
Contact:

Post 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); 
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

tr3s - did you read what scorphus posted?

Mac
tr3s
Forum Newbie
Posts: 17
Joined: Mon May 19, 2003 10:29 am
Location: Philippines
Contact:

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

1 Variable

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Caught it.

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

That did it!

Appreciate all the help.

Regards.
Post Reply