View Full Version : call a function in array()
gilgalbiblewhee
07-06-2008, 06:44 PM
Is it possible to call a function in an array?
$names = array(bookTitles());
oesxyl
07-06-2008, 06:48 PM
Is it possible to call a function in an array?
$names = array(bookTitles());
bookTitles() return a string, a array or a hash array?
regards
gilgalbiblewhee
07-06-2008, 07:01 PM
The array list $row['book_title'] comes from a database table:
function bookTitles(){
while($row = mysql_fetch_array($result)){
return '"'.$row['book_title'].'", ';
}
}
$names = array(bookTitles());
//print_r ($names);
And I get the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
If I remove:
//while($row = mysql_fetch_array($result)){
while($row = $result){
No array displays.
oesxyl
07-06-2008, 07:08 PM
this is not correct because:
1. return a string not a array
2. will return after first result from database
function bookTitles(){
while($row = mysql_fetch_array($result)){
return '"'.$row['book_title'].'", ';
}
}
correct the function:
function bookTitles(){
$booklist = ''
while($row = mysql_fetch_array($result)){
$boolist .= '"'.$row['book_title'].'", ';
}
return $booklist;
}
then you can construct $names using:
$names = explode(',', bookTitles());
PS: this will not work if $row['book_title'] values from database contain comma.
regards
gilgalbiblewhee
07-06-2008, 07:30 PM
this is not correct because:
1. return a string not a array
2. will return after first result from database
function bookTitles(){
while($row = mysql_fetch_array($result)){
return '"'.$row['book_title'].'", ';
}
}
correct the function:
function bookTitles(){
$booklist = ''
while($row = mysql_fetch_array($result)){
$boolist .= '"'.$row['book_title'].'", ';
}
return $booklist;
}
then you can construct $names using:
$names = explode(',', bookTitles());
PS: this will not work if $row['book_title'] values from database contain comma.
regards
I'm getting this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
shyam
07-06-2008, 07:45 PM
you should run the query that actually fetches from the database....
$result = mysql_query("select * from some_table");
so that there is some result for mysql_fetch_array to work upon...
oesxyl
07-06-2008, 07:47 PM
I'm getting this:
that's because you don't have a query:
$query = "....";
$result = mysql_query($query) or die(.....);
that means that this function never work until now, I guess.
regards
gilgalbiblewhee
07-06-2008, 08:02 PM
that's because you don't have a query:
$query = "....";
$result = mysql_query($query) or die(.....);
that means that this function never work until now, I guess.
regards
This is what I have:
$result = mysql_query("SELECT DISTINCT book_title FROM bible ORDER BY id");
oesxyl
07-06-2008, 08:09 PM
This is what I have:
$result = mysql_query("SELECT DISTINCT book_title FROM bible ORDER BY id");
ok, in my opinion is better that bookTitles to return a array. If you don't use it in other places where you need to return a string you can do this way:
function bookTitles(){
$booktitlesarray = array();
$query = "SELECT DISTINCT book_title FROM bible ORDER BY id";
$result = mysql_query($query);
if($result){
while($row = mysql_fetch_assoc($result)){
$booktitlesarray[] = $row['book_title'];
}
}
return $booktitlesarray;
}
to build $names become simple because it return a array:
$names = bookTitles();
to build a string, comma separated:
$stringnames = join(", ",bookTitles());
regards
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.