vynsane Posted May 25, 2007 Share Posted May 25, 2007 I'm migrating to a pseudo-template system (header/footer includes) and have created a variable that will send the title of that page to the <title> in the header include. The problem I'm facing is that some of my pages had functions in the title tag, and I'm having a problem getting the function into the new variable. An example of the code prior would've been: <title>Spider-man Comics <?php comicTitle($ComicTable['title'], $Vol); echo "Issue $IssueNum"; ?></title> the function looks something like this: function comicTitle($fulltitle, $volNum) { $query = mysql_query("SELECT title FROM ComicTable WHERE title = '".$fulltitle."'"); $rows = mysql_num_rows($query); $Title = stripslashes($fulltitle); echo "$Title"; if($rows >1) { echo " Vol. ".$volNum.""; } } What I would like to be able to do on my other page (the function is in an include) is something like this: $pageTitle = "Spider-man Comics" comicTitle($ComicTable['title'], $Vol); "Issue $IssueNum"; but that's obviously not working... I've tried variations of the $pageTitle variable, and variations on the comicTitle function, including using "compact()" and "extract()" or just trying to string my two variables ($Title and $Vol) into one variable and returning that one variable, but it's not working. Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/ Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 you need to escape it... like so... $pageTitle = "Spider-man Comics ".comicTitle($ComicTable['title'], $Vol)." Issue $IssueNum"; Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261552 Share on other sites More sharing options...
vynsane Posted May 25, 2007 Author Share Posted May 25, 2007 Thanks for the quick reply, but that doesn't work - it just spits out the function before everything else on the page, doesn't contain it inside the variable "$pageTitle". Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261555 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 that would be because in the variable your echoing the information... you need to return; it Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261559 Share on other sites More sharing options...
vynsane Posted May 25, 2007 Author Share Posted May 25, 2007 huh... weird, when i tried that before, it didn't work, but now it is. function comicTitle($fulltitle, $volNum) { $query = mysql_query("SELECT title FROM ComicTable WHERE title = '".$fulltitle."'"); $rows = mysql_num_rows($query); $Title = stripslashes($fulltitle); if($rows >1) { $comicFullTitle = "$Title Vol. $volNum"; } else { $comicFullTitle = "$Title"; } return $comicFullTitle; } I think I had $comicFullTitle = "".$Title." Vol. ".$volNum.""; instead... that might've been the culprit. now I just have to go back to everywhere else I use that function and change it to use the return instead of the echo :'( :-\ Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261562 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 yupyup! functions as they "should" be... with a few exceptions... should always return the data, never echo... you'll save yourself ALOT of problems in the long run... also then you can use functions within functions Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261564 Share on other sites More sharing options...
vynsane Posted May 25, 2007 Author Share Posted May 25, 2007 Yeah, I'm going to look into that, too. Found an even better way to do it than $pageTitle = "Spider-man Comics ".comicTitle($ComicTable['title'], $Vol)." Issue $IssueNum"; since I have to use it multiple times, with "return" I can turn the function into a variable, and call that variable elsewhere: $comicTitle = comicTitle($ComicTable['title'], $Vol); $pageTitle = "Spider-man Comics $comicTitle Issue $IssueNum"; ... .... .... ... echo "<div class=\"header\">$comicTitle Issue $Issue</div>\n"; Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/52958-solved-call-function-in-middle-of-a-variable/#findComment-261566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.