Result Function

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
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Result Function

Post by Straterra »

I have this function, but only the first result prints anything! Help!

Code: Select all

function pickmusic() {
global $cook, $dbname, $menupage, $dbname, $table, $menupage, $musicpage, $addmusicpage, $addmusicpageform, $cookiepage, $deletemusicpage, $deletemusicpageform, $loop, $autostart, $visible;
return "<center>Please select what song you would like to play.<form method="post" action="".$cookiepage.""><select name="dropdown"><option value="none" selected>None</option><option value="none">----------------</option>";
if ($db = sqlite_open($dbname, 0666, $sqliteerror)){ //Tries to open the database.
$result = sqlite_query($db, "select cookiename from $table ORDER BY artist, songname"); //Gets all of the music from the database, ordered first by Arist, then by songname
while ($row=sqlite_fetch_array($result)) {
$moo = $row['cookiename'];
$result2 = sqlite_query($db, "select * from $table where cookiename = '$moo'");//Gets the information from the database where the cookiename is that of the one stored in the cookie
$result2 = sqlite_fetch_array($result2);
return "<option value="".$row['cookiename']."">".stripslashes($result2['artist'])." - ".stripslashes($result2['songname'])."</option>";
}//Closes the while statement (By default, its on line 13)
?>
return "</select><br><input type="submit" name="Submit" value="Submit">
<?php
} else {
  die ($sqliteerror);//If the database could not be opened, it will return this error
}//Closes the database opening if statement (By default, its on line 11)
}
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Sorry man, looking at how this code is formatted is like looking under my sofa. Can't you put in a couple of linebreaks, please?
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

I guess my question is, can I return more than 1 line of code in a function?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Assign as many lines of code to a variable, then return it.

The dirty solution - and it's quite dirty, I find - is to use heredoc for multiple lines of text.

e.g.

return <<<dirtydirtydirty This is what I want to return, and it's a river of no return and Marilyn Monroe is in it as well and she sings a song about a river of no return and she has no clue about life on the frontier but the she still sings the song of the river of no return
dirtydirtydirty;

Now I have to go wash my hands ;)

Why you would want to use heredoc is beyond me - assigning stuff to a variable is makes life much, much nicer. Cleaner. Better.

Aaaaaaaaaaaah :)
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

*listens for Nay* I think I know what I will do, thanks for your help!
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Actually, I have a problem. I am wanting to print HTML though a function, but it uses a while statement. There are things I want to happen both before and after the while statement. I think I am screwed, no?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

What are you trying to print? If you've got some functional code we could maybe run through it and refactor it - or just start from scratch.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

If you are printing HTML to the browser from functions() and stuff it might be worth buffering the page until it's finished doing it's thing.

You could check-out [php_man]ob_start[/php_man] for more info...
Post Reply