grucker Posted February 5, 2010 Report Share Posted February 5, 2010 Good morning, Just out of interest, in the following code only the top eight lines are unique. What code would I use to file the common code in another file and insert where necessary. I have tried includes and output but nowt works. As I was advised earlier the code gets very repetitious. Thank you David. > function render_products_from_FACE() { // starts the product table $output = "</pre> <table> ' . $product->title . ' ' . $product->description . ' ' . $product->info . ' ' . $product->size . ' £' . $product->price .' s if there aren't enough products to completely // fill up the table if ($i == 1) $output .= '</table>';<br><br> return $output;<br> Quote Link to comment Share on other sites More sharing options...
quyen Posted February 5, 2010 Report Share Posted February 5, 2010 (edited) Hello, you can use "require", "include" to include files. Its pretty hard to look at your code right now, next time could you please put it in Your php code will show here and also share us a full php file. Did you mean that is there anyone can help you to find the common code from your example and put it in a new file? so later you can reuse this file? Or did you mean something else? Quyen, Edited February 5, 2010 by quyen Quote Link to comment Share on other sites More sharing options...
administrator Posted February 5, 2010 Report Share Posted February 5, 2010 Hi, Use functions. Place repetitious code into functions and then call the functions where you need to. This by-the-way is a common programming strategy called 'composition'. ... This usually applies to object oriented programming, but it still works here. Stefan Quote Link to comment Share on other sites More sharing options...
grucker Posted February 5, 2010 Author Report Share Posted February 5, 2010 I am not sure what the lady means. I showed all the code which is a function. I am using this function many times. The only differences are the top eight lines so I wanted to just code the top eight lines for the next function and add the rest from another file. I have tried various insert modes with no success. Regards David Quote Link to comment Share on other sites More sharing options...
grucker Posted February 5, 2010 Author Report Share Posted February 5, 2010 Still struggling. Latest effort goodsbox.php includes all the rest of my code from the first posting I get the following error Parse error: syntax error, unexpected $end in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 722 This following code is on lines 4 to 12 Advice please. Regards <?php function render_products_from_FACE() { // starts the product table $output = " // sets loop to 0 $i = 0; // starts looping through products foreach (get_xml_face() as $product) { include("../includes/goodsbox.php"); } ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 5, 2010 Report Share Posted February 5, 2010 My guess is that the last couple lines of your include file are: } return $output; That needs to be placed at the end of the render_products_from_FACE() function. At the moment, your foreach loop is missing that closing "}" Quote Link to comment Share on other sites More sharing options...
grucker Posted February 6, 2010 Author Report Share Posted February 6, 2010 I now get Parse error: syntax error, unexpected T_STRING in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 27 The template file is this and the goodsbox is the inbetween bitsIf I copy and paste the goodsbox back to the template it works. any other ways? Kindest regards David function render_products_from_FACE() { // starts the product table $output = " // sets loop to 0 $i = 0; // starts looping through products foreach (get_xml_face() as $product) { $output .= ' include("../includes/goodsbox.php"); return $output; } Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 6, 2010 Report Share Posted February 6, 2010 The line $output .= ' Needs to be the first line within the include. And you still need to have a closing "}" after the include but before the "return $output" that closes the foreach loop. Quote Link to comment Share on other sites More sharing options...
grucker Posted February 6, 2010 Author Report Share Posted February 6, 2010 The line $output .= ' Needs to be the first line within the include. And you still need to have a closing "}" after the include but before the "return $output" that closes the foreach loop. I now get Parse error: syntax error, unexpected $end in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 726 So I left the} above the $output and pasted the goodsbox in and got the following Notice: Undefined variable: output in E:\domains\s\sites4eyes.co.uk\user\htdocs\Paypalwebstore\htdocs\mulberry\functions\templates.php on line 78 The } is after the$output on all the other pages I have put it back and the page loads. Is there another way of loading the code? The gaffer suggested something but I wasnt sure. Thanks for your help. Regards David Quote Link to comment Share on other sites More sharing options...
quyen Posted February 6, 2010 Report Share Posted February 6, 2010 (edited) Hi, I am not sure about your foreach "(get_xml_face() as $product) " ,so I just try this simple example with include or require, I don't know if this can help you a bit! File test.php: <?php function render_data($arr) { $output = ''; return $output; } $arr = array("Homepage","About Us","Portfolio","Help"); print render_data($arr); ?> Here is the file that I use to include or require include.php: <?php for($i= 0; $i<=3; $i++){ $output .= ''.$arr[$i]; $output .= ''; } ?> Quyen, Edited February 6, 2010 by quyen Quote Link to comment Share on other sites More sharing options...
grucker Posted February 8, 2010 Author Report Share Posted February 8, 2010 Still no nearer finding a solution the link is sound and I can add it before the call. could someone show me the exact include and where to stick it. Really fed up. David Quote Link to comment Share on other sites More sharing options...
grucker Posted March 3, 2010 Author Report Share Posted March 3, 2010 Just to let you know the include thing is no go. Found an opposite way to do what I would like, i.e. keep the common code and use variable to insert individual code. This solution is better than I hoped as I only need one function for everything. This is the function begining the rest is added function render_products($area) { $output = " $i = 0; $funcname = "get_xml_" . $area; foreach ($funcname() as $product) { I also altered the call to or whatever page I want so That is the only file I have to change the function stays the same no matter how many pages I produce. <?= render_products('face');?> Thanks for all the advice Regards David 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.
Note: Your post will require moderator approval before it will be visible.