2

In this stackoverflow discussion a user told me that i could access externally to a php containing the portion of code that at the moment i have on the same .php file.
This is my file1.php:

     <?php
    $commentsLabels = array('First Signal','Second Signal');
    $commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');       

    $counter = 0; 

     ?>

<!--GENERATING LABELS-->
<div  class="signals"> 
    <ul>

    <?php
    foreach ($commentsLabels as $commentLabel) {
        $counter++;
    ?>
            <li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php echo $commentLabel; ?></a></li> 
    <?php
        } //loop ends       
    ?>  

    </ul>
</div>

<!--GENERATING POPUPS-->
<?php
    $counter = 0; //reset counter

    foreach ($commentsTexts as $commentText) { //loop starts
        $counter++; 
?>
        <div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php
    } //loop ends
?>

I tried to move into a "file2.php" the portion of code that creates my labels and texts:

<?php
    $commentsLabels = array('First Signal','Second Signal');
        $commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');       
?>   

Then i created an include function followed by the echo $variable into the file1.php:

<li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php include 'file2.php'; echo $commentLabel; ?></a></li> 

<div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php include 'file2.php'; echo $commentText; ?></p></div>

When i try it, it shows nothing: can you tell me why the include function doesn't work?

Andrea
  • 152
  • 1
  • 9

1 Answers1

1

Try this:

File2.php

<?php
     $commentsLabels = array('First Signal','Second Signal');
     $commentsTexts = array('First comment for #signal1 id - it will   open in a fancybox.',
                     'Second comment for #signal2 id - it will open in a fancybox.');       
?>  

File1.php

<?php
     include 'file2.php';
     $counter = 0;
?>
<!--GENERATING LABELS-->
<div  class="signals"> 
  <ul>

  <?php
       foreach ($commentsLabels as $commentLabel) {
       $counter++;
  ?>
     <li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php    echo $commentLabel; ?></a></li> 
  <?php } ?>  
  </ul>
</div>

<!--GENERATING POPUPS-->
<?php
    $counter = 0; //reset counter

    foreach ($commentsTexts as $commentText) { //loop starts
       $counter++; 
?>
    <div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php } ?>
Shantanu
  • 692
  • 5
  • 20