-4

Hi guys sorry for the bad english :) i have this code below that echo the some divs defined inside the array i want to include a php page (example below ) inside those divs :

<?php $items =array(         
        '<div id="o-31" class="col-sm-6 col-md-4 col-lg-3" > <div id="mydid"  >   </div></div>',  


             '<div id="o-36" class="col-sm-6 col-md-4 col-lg-3"  ><div  class="demo-content bg-alt" >.col-sm-8</div></div>', 


             '<div id="o-37" class="col-sm-6 col-md-4 col-lg-3"  ><div class="demo-content bg-alt" >.col-sm-8</div></div>', 


      '<div id="o-38" class="col-sm-6 col-md-4 col-lg-3"><div id="mydid"  > </div></div>'
     );

?> 
      <?php 
        for($i = 0; $i <= 3; $i++){

        echo $items[$i];

        }
      ?>

i cant see how i can add a php code inside the div i tried :

 '<div id="o-31" class="col-sm-6 col-md-4 col-lg-3" > <div id="mydid"  > 

         <?php
      $ccc= "ff";
  $tablelink2=$_SESSION['hes_2'];
 $tabletitle2=$_SESSION['hes_1'];
 $tableimg2= $_SESSION['hes_3'];
     include('hes_2.php');
     ?>

        </div></div>',  

and

'<div id="o-31" class="col-sm-6 col-md-4 col-lg-3" > <div id="mydid"  >'



     <?php
          $ccc= "ff";
      $tablelink2=$_SESSION['hes_2'];
     $tabletitle2=$_SESSION['hes_1'];
     $tableimg2= $_SESSION['hes_3'];
         include('hes_2.php');
         ?>

            '</div></div>', 

also not working :( ?

user2686117
  • 11
  • 1
  • 1
  • 7

3 Answers3

0

Try making a separate PHP file and passing it a parameter via address line: somefile.php?a=5&b=7&c=hello etc, then read those parameters in the target file using $_GET['varname'] like $_GET['a']. If that ['a'] is not going to be fixed, you can use double quotes and a variable name: $_GET["$varname"].

Hope this helps.

nurchi
  • 770
  • 11
  • 24
0

You cannot embed PHP within PHP:

<?php $items =array(         
        '<div id="o-31" [...snip...]
         <?php ... ?>

You're already in PHP mode on the first line, and in the middle of a PHP string, which means that the SECOND <?php tag is not a PHP tag. It's the characters <, ?, p, etc... becoming part of this string you're building.

You have to break OUT of the string, do whatever you want, then go BACK into string mode. e.g.

$str = "foo <?php bar() ?> baz"; // will not work
$str = "foo " . bar() . " baz"; //works fine
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

First of all,

I'll recommend using sprintf and vsprintf to tidy up your code,

$items =array(         
    '<div id="o-31" class="col-sm-6 col-md-4 col-lg-3"><div id="mydid">%s</div></div>',  
    '<div id="o-36" class="col-sm-6 col-md-4 col-lg-3"><div class="demo-content bg-alt">.col-sm-8</div></div>', 
    '<div id="o-37" class="col-sm-6 col-md-4 col-lg-3"><div class="demo-content bg-alt">.col-sm-8</div></div>', 
    '<div id="o-38" class="col-sm-6 col-md-4 col-lg-3"><div id="mydid">%s</div></div>'
);

$items[0] = sprintf($items[0], 'Item 0');
$items[3] = sprintf($items[3], 'Item 3');

echo vsprintf(str_repeat("%s\n", count($items)), $items);

Next, about the include file. There is a work around here,

How to load a php file into a variable

To implement,

function inc2var($file){
    ob_start();
    include $file;
    $var = ob_get_contents();
    ob_end_clean();
    return $var;
}

$ccc = "ff";
$tablelink2 = $_SESSION['hes_2'];
$tabletitle2 = $_SESSION['hes_1'];
$tableimg2 = $_SESSION['hes_3'];

$items[0] = sprintf($items[0], inc2var('hes_2.php'));

Hope this helps.

Community
  • 1
  • 1
Capital C
  • 319
  • 3
  • 13