Hillary Posted March 6, 2008 Share Posted March 6, 2008 i have a question about include command. i am new to all this PHP stuff, i am taking a class at school that i am doing alright in but sometimes i just need guidance if you know what i mean. anyway, im wondering how i can include only part of a file. i have a file dedicated to functions and i only need to use 3 of the functions on my new page. how would i do that? but i do not enjoy retyping the same thing all the time... ughh so frustrating, i wish i knew more!!! thanks for your help!! Quote Link to comment Share on other sites More sharing options...
matto Posted March 6, 2008 Share Posted March 6, 2008 contents of functions.php file <?php function do_something() { //do something usful } function do_something_else() { //do something else usful } function hello_world() { echo "Hello World"; } ?> -------------------------------------------------- contents of index.php file that has access to any of the functions in functions.php <?php include("functions.php"); hello_world(); ?> The index.php file has access to all the functions contained within functions.php (even if you choose to only use one function contained with the functions.php file). Quote Link to comment Share on other sites More sharing options...
Hillary Posted March 6, 2008 Author Share Posted March 6, 2008 My assignment is to create a PHP web page with a title and several links on it. The title might be, "My favorite links". Create a function to format the title and another function to format the link. For the title function, provide a parameter to pass the text of the title. The function should surround the text with an opening and closing h1 tag and return the result as a string. The program should then output the string. For the links, create a function that takes two parameters. One is the text that is displayed in Blue and the other is the URL of the link. The output should be a formatted link. Include 5 links on the page, creating each one with a link function. This is what I typed up: ------------------------------------------------------------------------------------------------ myfavoritelinks.php <html> <head> <title>My Favorite Links</title> </head> <body> <?php include "functions.php"; $h = title("My Favorite Links"); print $h; $l = link("http://www.youtube.com","YOUTUBE"); print $l; $l = link("http://www.ebay.com", "EBAY"); print $l; $l = link("http://www.yahoo.com", "YAHOO"); print $l; $l = link("http://www.phpfreaks.com", "PHP FREAKS"); print $l; $l = link("http://www.purevolume.com", "PUREVOLUME"); print $l; //end functions ?> </body> </html> ------------------------------------------------------------------------------------------------ functions.php <html> <body> <?php print "<p>Enter a string</p>\n"; $s = form_open("$action"); print $s; $t = text_area("line", 5, 20); print $t; $i = submit(); print $i; $h = title("$title"); print $h; $l = link("$link, $site"); print $l; print "</form>"; function link($link, $site) { $link = "<li><a href=\"$link\"> \"$site\" </a></li>\n"; return $link; } function title($title) { $title = "<h1>$title</h1>\n"; return $title; } function form_open($action) { $myform = "<form method=GET action=\"$action\">\n"; return $myform; } function text_area($name, $rows, $cols) { $textarea = "<textarea rows=\"$rows\" cols=\"$cols\" name=\"$name\"></textarea>\n"; return $textarea; } function submit() { $submit = "<input type= \"submit\">"; return $submit; } ?> </html> </body> --------------------------------------------------------------------------------------------------- This is what it tells me when it view it on the web: Enter a string Notice: Undefined variable: action in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 5 Notice: Undefined variable: title in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 11 Notice: Undefined variable: link in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 13 Notice: Undefined variable: site in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 13 Warning: Missing argument 2 for link(), called in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 13 and defined in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 22 Notice: Undefined variable: site in C:\Inetpub\wwwroot\StudentWeb\WIS-120-SEN01-SPRING2008\student4\week8\functions.php on line 24 "" My Favorite Links "YOUTUBE" "EBAY" "YAHOO" "PHP FREAKS" "PUREVOLUME" The part here in bold is the only part i need on the new page. sorry im not too bright when it comes to this stuff, but i was proud of myself today when things at least showed up on my page. hahaha i just dont undersatnd a lot of this. thanks again. and i apologize for this post being so long. Quote Link to comment Share on other sites More sharing options...
KrisNz Posted March 6, 2008 Share Posted March 6, 2008 You're almost there, functions.php shouldn't contain any html or calls to the functions, just the functions themselves. From the description of your assignment it sounds like the only functions you need are link() and title(). So you can reduce your functions.php file to <?php function link($link, $site) { $link = "<li><a href=\"$link\"> \"$site\" </a></li>\n"; return $link; } function title($title) { $title = "<h1>$title</h1>\n"; return $title; } ?> Also, it's not necessary to assign a value to a variable before returning it. you can return the literal value e.g return "<h1>$title</h1>\n"; Good luck. Quote Link to comment 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.