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!
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
<?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;
}
?>
<?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);
?>
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!
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.
<?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;
}
?>
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.
<?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;
}
?>
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?
<?php
$tickers = Check_Quotes($username);
print_r ($tickers);
// Make the array into a string using implode()
$ticker_list = implode(' ', $tickers);
echo $ticker_list;
?>