Jump to content

include into function


grucker

Recommended Posts

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;

}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

   

}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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) 
{
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.