Jump to content

[SOLVED] call function in middle of a variable?


vynsane

Recommended Posts

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.

 

Link to comment
Share on other sites

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  :'( :-\ ;D

Link to comment
Share on other sites

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 ;):D

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.