View Full Version : Function inside a function.
^BuGs^
09-03-2002, 02:10 AM
Ok. I am a pretty advanced PHP writer, but I knew I would have troubles when I got to this part in designing my site or coding my site.
Ok. To get right to the point...
function createnewsitem($header,$date,$newstext) { ?>
<table width="95%" height="0%" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#C0C0C0">
<tr bgcolor="#C0C0C0">
<td width="76%" align="left" valign="top" bgcolor="#C0C0C0"><font class="newsheader"><?php echo $header; ?></font></td>
<td width="24%" align="right" valign="top" bgcolor="#C0C0C0"><font class="newsheader"><?php echo $date; ?></font></td>
</tr>
<tr bgcolor="#808080">
<td colspan="2" align="left" valign="top" bgcolor="#808080"><?php echo $newstext; ?></td>
</tr>
</table> <?php
}
^^^ NEWS CREATION FUNCTION ^^^
function createtextlink($text,$url,$desc) {
echo "$text ('$url')";
}
^^^ LINK CREATION ^^^
createnewsitem("Site Semi-Offically Open","September 2, 2002","This is a example. You can createtextlink("email","mailto:bugs@bugssite.org","email me"); me.");
^^^ THAT DOESN'T WORK ^^^
Anyone have any suggestions to how about to do a function inside a function cause I have more than one function. I have about 4 others that create certain things. From Image placers & Bordered Items.
Strike
09-03-2002, 11:02 AM
What are your error messages (or problems)? Shouldn't you put the function call outside the quotes and string concatenate it? Like so:
createnewsitem("Site Semi-Offically Open","September 2, 2002","This is a example. You can " . createtextlink("email","mailto:bugs@bugssite.org","email me"); . " me.");
imported_Gryphon
09-03-2002, 01:08 PM
Hiya Bugs :D
^BuGs^
09-03-2002, 04:25 PM
ah. I didn't think of that. I will have to try that. I'll get back to you if the way it outputs doesn't make it work, but it should :)
^BuGs^
09-03-2002, 04:33 PM
Ok. I had to change 2 things for anyone else who wants to do this.
1) return instead of echo in the createtextlink(); function
Code:
function createtextlink($text,$url,$desc,$return) {
if ($return) {
return "$text ('$url')";
} else {
echo "$text ('$url')";
}
}
2) add an extra var to be passed through if it going to be returned or echo'ed
function createtextlink($text,$url,$desc,$return);
That worked. Now I gotta test to see if I add that code in a DB will it phrase it correctly.
And Gryphon, hiya :)
BTW.. this is a great place to find help and help out. :D
^BuGs^
09-04-2002, 12:21 AM
Originally posted by Strike
What are your error messages (or problems)? Shouldn't you put the function call outside the quotes and string concatenate it? Like so:
createnewsitem("Site Semi-Offically Open","September 2, 2002","This is a example. You can " . createtextlink("email","mailto:bugs@bugssite.org","email me"); . " me.");
Since you seem to be the resident expert on functions inside a function, how would I complete the same operation but the text from the 3rd part of the createnewsitem(); been somehow gotten from a DB?
roninblade
09-04-2002, 01:18 AM
createnewsitem("Site Semi-Offically Open","September 2, 2002","This is a example. You can " . createtextlink("email","mailto:bugs@bugssite.org","email me"); . " me.");
i just want to comment on this statement : its much better to seperate each function in different lines that way its less confusing and easier to debug. ;)
how would I complete the same operation but the text from the 3rd part of the createnewsitem(); been somehow gotten from a DB?
you can do something like...<?
// your db connections here
// this assumes you have one record result for your query condition
$text = @mysql_result(@mysql_query("select text from $my_table where $your_conditions"), 0);
createnewsitem("Site Semi-Offically Open","September 2, 2002", $text);
?>
^BuGs^
09-04-2002, 10:32 AM
/* Start of Content */
$news_array = mysql_query("SELECT * FROM newsdata ORDER BY `newsid` DESC LIMIT $newslimit")
while ($i=mysql_fetch_array($news_array)) {
$text = @mysql_result(@mysql_query("SELECT newstext FROM newsdata WHERE newsid='$i[newsid]'"), 0);
createnewsitem("Site Semi-Offically Open","September 2, 2002", $text);
}
/* End of Content */
and it $text returns:
"This is a example. You can " . createtextlink("email","mailto:bugs@bugssite.org","email me"); . " me."
So that didn't work. I could have just changed $text into $i[newstext] and I would have gotten the same thing.
^BuGs^
09-04-2002, 10:35 AM
Doesn't even work with <?php echo "test"; ?> as the in the DB. I might have to just ferget the creation and create some HTML properties or javascript like the VB controls to create the HTML code for me. :-/
^BuGs^
09-05-2002, 05:04 PM
If you are just looking at this message FOR THE FIRST TIME PLEASE READ THE POSTS BEFORE HAND. It sets the tone to this post.
Synopis: Trying to get a function to act correctly, when it gets outputed by the MySQL so it creates what needs to be there instead of the function displaying as NORMAL text.
Again.. the createtextlink(); function:
function createtextlink($text,$url,$desc,$return) {
if ($return) {
return "$text ('$url')";
} else {
echo "$text ('$url')";
}
}
bugssite.newsdata MySQL Table
Setup & Data
CREATE TABLE newsdata (
newsid int(11) NOT NULL auto_increment,
newsheadline text,
newsdate text,
newstext text,
UNIQUE KEY newsid (newsid)
) TYPE=MyISAM COMMENT='News Data';
INSERT INTO newsdata VALUES (1, 'Test', 'September 2, 2002', 'Hope you enjoy the site for which has taking a long time to create.\r\n\r\nYou can send me an <?php createtextlink("email me","mailto:bugs@bugssite.org","email me",""); ?>.');
The stuff inside main.php
$news_array = mysql_query("SELECT * FROM newsdata ORDER BY `newsid` DESC LIMIT $newslimit");
while ($i=mysql_fetch_array($news_array)) {
createnewsitem($i[newsheadline],$i[newsdate], $i[newstext]);
}
What happens is that:
createtextlink("email me","mailto:bugs@bugssite.org","email me","");
either displaies as it is even without the PHP tags and with them on the text disaperres and it blanks where that word WOULD be, but it just keeps on going if there is anything afterwords. If anyone what's a crack at it, I can provide you an URL that will show you the source of any PHP page on my site so if there are any challengers contact me via bugs@bugssite.org
Thanks again for your help in advance! I would really not like to create buttons like the ones here in the "Post Reply" in my news amdin script.
roninblade
09-05-2002, 11:22 PM
hey, you can either attach your pages here, or email them to me at archon@digitelone.com.
^BuGs^
09-06-2002, 10:32 AM
Never mind now. I finally got it in the 11th hour of programming. It works fine now. What I did was simple. :) Thanks to Halide who suggested the idea & your support and help. :)
/* Start of Content */
while ($i=mysql_fetch_array($news_array)) {
$news_functions_num = $i[newsfuncnum]; // number of function that NEED TO BE EXACUTED!!
$news_functions_code = $i[newsfunctions]; // holding the functions
$char = "/~/"; // what seperates the different function in the table field
$func = preg_split($char,$news_functions_code); // for testing purposes: $func[0] is created
$news_text = $i[newstext]; // sets this var as holder of text
$inc = 0; // just to make sure that this is 0 when it goes through the different functions
while ($news_functions_num > $inc) {
eval("\$func[$inc] = ".$func[$inc].";");
$inc++;
}
eval("\$news_text = \"$news_text\";");
createnewsitem($i[newsheadline],$i[newsdate],"$news_text");
}
/* End of Content */
While you've solved the problem, you seem to have done so by doing it in an entirely different way, so I'm going to address this:
INSERT INTO newsdata VALUES(
blah blah,
<?php createtextlink("email me","mailto:bugs@bugssite.org","email me",""); ?>'
);
This didn't work becuse you're passing "", which invokes the "echo" functionality of "createtextlink", which echos the result to the output (or stdout in commandline mode), rather than causing the function call to evaluate to its output.
Basically, your implementation of "createtextlink" is overcomplicated and silly. Instead, it (and functions like it) should always return the string. You can then apply other functions, such as echo, to the result. This would prevent errors like the one above.
^BuGs^
09-08-2002, 12:38 PM
There isn't even anything to discuss. I figured that out on my own to. I am not dumb. I just needed some suggestions Hawk. I try everything even if it's not working.
Halide
09-10-2002, 10:43 AM
hey hawk, thanks for that info... I'm new to coding and I need to start using return more :)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.