grucker Posted February 24, 2010 Share Posted February 24, 2010 Good afternoon, I use the following code on many different pages. The top six lines are the only ones that change and I wanted to add the other lines as an include. I have tried several methods and all have failed. Just want to know if it's possible, People say it is but never provide an answer. So is it and how? Kind regards. function render_products_from_FACE() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_face() as $product) { $output .= ' <td valign="top"> <b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b> <div id="' . $product->id . '" class="product"> <div class="productdescription_face"> <h3>' . $product->title . '</h3> <br/> <p style="text-align:center;"> <img src="' . $product->img . '" alt="'.$product->alt.'" /></p> <p>' . $product->description . '</p> </div> <div style="margin-bottom:15px"> <p><b>' . $product->info . ' </b></p> </div> <div class="price"> <p><b>' . $product->size . ' £' . $product->price .'</b></p> </div> <div class="addToCart"> <form action="../addToCart.php" method="post"> <p> <input type="hidden" name="id" value="'.$product->id.'" /> <input class="red" type="submit" value="Add To Shopping Basket" /> </p> </form> </div> </div> <b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b> </td> '; if ($i >= 2){ // start new row $output .= "</tr><tr>"; // reset loop $i = 0; } else $i++; } if ($i == 1) $output .= '<td></td><td></td>'; else if ($i == 2) $output .= '<td></td>'; $output .= '</tr></table>'; return $output; } Quote Link to comment Share on other sites More sharing options...
schilly Posted February 24, 2010 Share Posted February 24, 2010 so why do you want to put some of the fn as an include? you've already modularized it as a function. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 24, 2010 Author Share Posted February 24, 2010 Sorry for being unclear. I have umpteen pages and this code is the same for each page except for the top few lines. I wanted to place the common code in a file and then call it into the function . Really just to tidy everything up and make it easier to find the page I want. Everything works just fine I just wanted to know if it was possible and how. Thanks for your reply. Regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 24, 2010 Share Posted February 24, 2010 oh. so something like: function render_products_from_FACE() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_face() as $product) { include_once('path/to/file/common_code.php'); } your path may change depending on the dir your files are in, unless you use the absolute path. preferably i would put the whole function in a file and include it then pass in variables to the function depending on what page it's on. then you only have one instance of the entire function and it will output the proper code depending on the variables you pass it. having the same function but different on every page is a little unpractical but i'm not exactly sure how that function works in your case. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks for your reply. Tried that and got line 716 is the closing ?> in the template file. As I say if I leave everything in it works fine. Perhaps its not possible after all. Parse error: syntax error, unexpected $end in E:\domains\u\ultimateskincare.org.uk\user\htdocs\mulberry\functions\templates.php on line 716 Kindest regards David Quote Link to comment Share on other sites More sharing options...
grucker Posted February 25, 2010 Author Share Posted February 25, 2010 Good afternoon, so is this possible or not? I really need advice. If it is not possible I can move on. Please let me know. Regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 what does the function look like now? Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 The function is as the first post as that works. I just want to include the bottom of the function as this is common to lots of similar functions and would tidy the template file where the function is filed regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 well what you want to do is completely possible but I can't help you any further without seeing how you actually create the include. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 The function is called in a file called face.php thus. <?= render_products_from_FACE();?> The function is in a file called templates thus function render_products_from_FACE() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_face() as $product) { include_once('goodsbox.php'); } goods box contains all the rest of the code and is filed in the same folder as templates. if I leave it as is everything works but when I try to include I get Parse error: syntax error, unexpected $end in E:\domains\u\ultimateskincare.org.uk\user\htdocs\mulberry\functions\templates.php on line 716 line 716 is the final ?> in the template file. Is it still possible? Regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 can you put the foreach call in the include? the php engine might not like that the closing bracket is in a separate file. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 Not really, I have loads of pages such as face body bath and all the code is the same in all the functions apart from the first few lines. The for each line is different for each page. Regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 you mean like function render_products_from_BODY() { $output = "<table class='body'><tr>"; $i = 0; foreach (get_xml_face() as $body) { Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 The code for each page is as follows, I really appreciate your assistance and am sure you will come up with a solution but could I say I am not very good at php, as you can tell, and I may not be able to understand anything too technical. Kindest regards David function render_products_from_FACE() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_face() as $product) function render_products_from_BODY() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_body() as $product) function render_products_from_MEN() { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_men() as $product) Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 ok so the only difference is the function name and the xml call? Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 yes I believe so. Thanks David Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 function render_products($area) { $output = "<table class='products'><tr>"; $i = 0; foreach (get_xml_$area() as $product) { $output .= ' <td valign="top"> <b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b> <div id="' . $product->id . '" class="product"> <div class="productdescription_face"> <h3>' . $product->title . '</h3> <br/> <p style="text-align:center;"> <img src="' . $product->img . '" alt="'.$product->alt.'" /></p> <p>' . $product->description . '</p> </div> <div style="margin-bottom:15px"> <p><b>' . $product->info . ' </b></p> </div> <div class="price"> <p><b>' . $product->size . ' £' . $product->price .'</b></p> </div> <div class="addToCart"> <form action="../addToCart.php" method="post"> <p> <input type="hidden" name="id" value="'.$product->id.'" /> <input class="red" type="submit" value="Add To Shopping Basket" /> </p> </form> </div> </div> <b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b> </td> '; if ($i >= 2){ // start new row $output .= "</tr><tr>"; // reset loop $i = 0; } else $i++; } if ($i == 1) $output .= '<td></td><td></td>'; else if ($i == 2) $output .= '<td></td>'; $output .= '</tr></table>'; return $output; } that should work. pass in the variable depending on what page it's on. ie. render_products("face"); now you can put the whole function in an include. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 26, 2010 Author Share Posted February 26, 2010 I can understand a bit, but where is the variable area stated. I said I am not very bright, just old. The function.psp is as follows does this have any bearing on the matter or does it just complicate matters? Your patience is valued. Regards David /** Define Globals **/ define('STORE_XML_FACE_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/face.xml')); define('STORE_XML_BODY_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/body.xml')); define('STORE_XML_MEN_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/men.xml')); define('STORE_XML_BATH_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/bath.xml')); define('STORE_XML_EXTRA_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/extra.xml')); define('STORE_XML_TRIAL_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/trial.xml')); define('STORE_XML_NAT_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/nat.xml')); define('STORE_XML_REST_FILE' , ($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/products/xml/poppyred/rest.xml')); define('PAYPAL_BUSINESS', 'dmaxey_1261514279_biz@btinternet.com'); define('PAYPAL_URL' , 'https://www.sandbox.paypal.com/cgi-bin/webscr'); /** DEFINE REFERENCES **/ require_once(($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/functions/templates.php')); require_once(($_SERVER['DOCUMENT_ROOT'] .'/Paypalwebstore/htdocs/mulberry/classes/ShoppingCart.php')); /** Functions **/ SESSION_START(); function get_xml_face() {return new SimpleXMLelement(file_get_contents(STORE_XML_FACE_FILE));} function get_xml_body() {return new SimpleXMLelement(file_get_contents(STORE_XML_BODY_FILE));} function get_xml_men() {return new SimpleXMLelement(file_get_contents(STORE_XML_MEN_FILE));} function get_xml_bath() {return new SimpleXMLelement(file_get_contents(STORE_XML_BATH_FILE));} function get_xml_extra() {return new SimpleXMLelement(file_get_contents(STORE_XML_EXTRA_FILE));} function get_xml_trial() {return new SimpleXMLelement(file_get_contents(STORE_XML_TRIAL_FILE));} function get_xml_nat() {return new SimpleXMLelement(file_get_contents(STORE_XML_NAT_FILE));} function get_xml_rest() {return new SimpleXMLelement(file_get_contents(STORE_XML_REST_FILE));} function get_shopping_cart() {if (! isset($_SESSION['cart'])) return new ShoppingCart(); else return unserialize($_SESSION['cart']); } function set_shopping_cart($cart) { $_SESSION['cart'] = serialize($cart); } function product_exists($product_id) { foreach(get_xml_face() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_men() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_bath() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_extra() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_body() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_trial() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_nat() as $product) { if ($product->id == $product_id) return true; } foreach(get_xml_rest() as $product) { if ($product->id == $product_id) return true; } return false; } function get_item_cost($product_id) { foreach(get_xml_face()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_men()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_bath()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_extra()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_body()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_trial()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_nat()as $product) { if ($product->id == $product_id) return $product->price; } foreach(get_xml_rest()as $product) { if ($product->id == $product_id) return $product->price; } throw new Exception('item not found:'. $product_id); } ?> Quote Link to comment Share on other sites More sharing options...
schilly Posted February 26, 2010 Share Posted February 26, 2010 The $area variable depends on whatever page you are calling the function. instead of <?= render_products_from_FACE();?> just use <?= render_products('face');?> just make sure to include the function before the call. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 27, 2010 Author Share Posted February 27, 2010 Get the following now. Parse error: syntax error, unexpected T_VARIABLE in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 6 Line 6 is foreach (get_xml_$area() as $product) { Does the function.php file that I posted earlier have any bearing on this problem. Would it assist the solution to send you all the files as are working for you to see the bigger picture? I incllude the function thus on face.php before any html <?php require_once '../../functions/functions.php'; $shopping_cart = get_shopping_cart(); ?> Kindest regards David Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 The error is caused by a $sign in the function name foreach (get_xml_$area() as $product) { should that be foreach (get_xml_area() as $product) { Quote Link to comment Share on other sites More sharing options...
grucker Posted February 27, 2010 Author Share Posted February 27, 2010 That brings the following Fatal error: Call to undefined function get_xml_area() in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 6 I am still a little uneasy regarding the function.php, it has no mention of area and I know nothing. Regards David Quote Link to comment Share on other sites More sharing options...
schilly Posted March 1, 2010 Share Posted March 1, 2010 can't you add a variable into the function name to make it a dynamic function call? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted March 1, 2010 Share Posted March 1, 2010 you can have a variable as the whole function name, but to use a variable for part of the function name I would imagine the best way to go would be to put it all into a variable first. $funcname = 'get_xml_'.$area; foreach($funcname() as $product) { } Quote Link to comment Share on other sites More sharing options...
schilly Posted March 1, 2010 Share Posted March 1, 2010 ah good call. grucker give that a try. should work. 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.