Forum Moderators: coopster

Message Too Old, No Replies

how to handle insertion in variable

php insert

         

zpupster

1:19 pm on Dec 10, 2014 (gmt 0)

10+ Year Member



i have a file bm_logo.php which is div'd like this
$data = '<div class= "col-md-6 col-md-push-6"><a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image(DIR_WS_IMAGES . 'store_logo.png', STORE_NAME) . '</a></div>'.


'<div class="col-md-6 col-md-pull-6">
</div>';


inside that empty div i want to add php code from another file.(header.php),
which contains login info , account info etc.

i have been trying making the code an include file , i do not think i am doing it right.
i need to know the best way to handle this.
here is the code i am trying to place in the div.
<ul>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo (tep_session_is_registered('customer_id')) ? sprintf(HEADER_ACCOUNT_LOGGED_IN, $customer_first_name) : HEADER_ACCOUNT_LOGGED_OUT; ?></a>
<ul class="dropdown-menu">
<?php
if (tep_session_is_registered('customer_id')) {
echo '<li><a href="' . tep_href_link(FILENAME_LOGOFF, '', 'SSL') . '">' . HEADER_ACCOUNT_LOGOFF . '</a>';
}
else {
echo '<li><a href="' . tep_href_link(FILENAME_LOGIN, '', 'SSL') . '">' . HEADER_ACCOUNT_LOGIN . '</a>';
echo '<li><a href="' . tep_href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL') . '">' . HEADER_ACCOUNT_REGISTER . '</a>';
}
?>
<li class="divider"></li>
<li><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNT, '', 'SSL') . '">' . HEADER_ACCOUNT . '</a>'; ?></li>
<li><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL') . '">' . HEADER_ACCOUNT_HISTORY . '</a>'; ?></li>
<li><?php echo '<a href="' . tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL') . '">' . HEADER_ACCOUNT_ADDRESS_BOOK . '</a>'; ?></li>
<li><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNT_PASSWORD, '', 'SSL') . '">' . HEADER_ACCOUNT_PASSWORD . '</a>'; ?></li>
</ul>
</li>
<?php
if ($cart->count_contents() > 0) {
?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo sprintf(HEADER_CART_CONTENTS, $cart->count_contents()); ?></a>
<ul class="dropdown-menu">
<li><?php echo '<a href="' . tep_href_link(FILENAME_SHOPPING_CART) . '">' . sprintf(HEADER_CART_HAS_CONTENTS, $cart->count_contents(), $currencies->format($cart->show_total())) . '</a>'; ?></li>
<?php
if ($cart->count_contents() > 0) {
echo '<li class="divider"></li>';
echo '<li><a href="' . tep_href_link(FILENAME_SHOPPING_CART) . '">' . HEADER_CART_VIEW_CART . '</a></li>';
}
?>
</ul>
</li>
<?php
echo '<li><a href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL') . '">' . HEADER_CART_CHECKOUT . '</a></li>';
}
else {
echo '<li class="nav navbar-text">' . HEADER_CART_NO_CONTENTS . '</li>';
}
?>
</ul>

penders

3:16 pm on Dec 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How are you currently trying to include the contents of your "include" file into your variable? Or what have you tried so far?

zpupster

4:40 pm on Dec 10, 2014 (gmt 0)

10+ Year Member



 '<div class="col-md-6 col-md-pull-6">' .
<?php include("menu.php"); ?> .'</div>';


putting all the code in menu.php. i am trying to insert the include into the div but the syntax is wrong, i have tried different combinations
but i can not get it to work.

penders

9:02 pm on Dec 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah, that's not going to work the way you think it should. (In fact, the syntax you've used is likely to result in a parse error.) "include" does not actually return the contents of the included file. It "includes" it (for want of a better term) and returns (int)1 on success (unless you explicitly return something else). By "include" we mean that any PHP is evaluated (in the current scope) and everything else (ie. HTML in this case) is sent to the standard output.

You might be able to see better what is going on by adjusting your syntax (note that this is still not correct, this is just for academic purpose):

$data = '<div>'.(include 'menu.php').'</div>';


"include" is not a function. It is a function-like language construct. Parentheses are optional around its argument and in some cases you need to be very careful where you use the parentheses - as in this example.

To achieve what you want you need to use the "output buffer" to capture the output. This could be wrapped in a utility function. For example:

function getFile($filename) { 
ob_start();
include $filename;
return ob_get_clean();
}


And called like so:
$data = '<div>'.getFile('menu.php').'</div>';


However, there are other ways to approach this. But that really depends on your applications structure...

For instance, if your HTML DIV section is contained in a separate file (a "template"), then yes, you could simply include your "menu.php" in that. For example, in "main.php":

<div> 
<?php include 'menu.php' ?>
</div>


At some later point in your code, or your main "page template" you would include 'main.php'.

zpupster

4:53 pm on Dec 13, 2014 (gmt 0)

10+ Year Member



thank you ,

i think i am going to punt on inserting code to a variable.

i am taking a different direction, thanks for your help.