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?

0

1 Answer 1

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 } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

It Worked! thank you very much for the solution and for the time you spent on it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.