Jump to content

include common code


grucker

Recommended Posts

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>

Link to comment
Share on other sites

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 by quyen
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");

}

?>

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by quyen
Link to comment
Share on other sites

  • 4 weeks later...

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...