-2

i have these lines of code:

<div class="signals">
  <ul>
    <li><a href="#signal1" class="fancybox">First Signal</a></li>
    <li><a href="#signal2" class="fancybox">Second Signal</a></li>
    <li><a href="#signal3" class="fancybox">Third Signal</a></li>
  </ul>
</div>

<div id="signal1" style="display:none;">
  <p style="color:#fff">First comment for #signal1 id - it will open in a fancybox -.</p>
  <div id="signal2" style="display:none;">
    <p style="color:#fff">Second comment for #signal2 id - it will open in a fancybox -.</p>
  </div>
  <div id="signal3" style="display:none;">
    <p style="color:#fff">Third comment for #signal3 id - it will open in a fancybox -.</p>
  </div>

Here it is the jsfiddle code: JsFiddle

Right now when i want to show different comments, i open my html file and edit the "id #signal , adding more id (or deleting them) when i need it.
The problems is: the signals can be more than the three that are showed up, or even less.
So my question is:
There is a way to generate automatically the divs that i need in a second sheet, where i will insert the comment and all the id's? (a sort of backend)
For example: if one day i need just 2 signals, i will create the #signal1 and #signal2 div, i'll insert the comments and save the secondary sheet.
When i do that, the primary sheet with the html stuff, will show 2 "li" lines:

  • First Signal
  • Second Signal
  • and when i click on of them, the fancybox will open and show the comment, just as the code into the jsfiddle.

    I don't know much of php, but there is a way i can do that using it? Or there is a better way?
    Hope i can learn from your help.

    Praveen Kumar Purushothaman
    • 164,888
    • 24
    • 203
    • 252
    Andrea
    • 152
    • 1
    • 9
    • you can for example store them in db and when you need some the divs and li's will automatically as your db and if you want to delete some just delete it from db and you will have just the wanted. – Alaa M. Jaddou Dec 27 '15 at 15:50
    • use jquery, make an ajax call and retrieve the data and populate your list. you will need to have a php file that can access the data on your db first. There heaps of examples for this on the internet, here's one: http://stackoverflow.com/questions/29204934/how-to-populate-dependable-drop-down-using-ajax-and-php – toing_toing Dec 27 '15 at 15:54

    1 Answers1

    0

    It seems that you need PHP foreach loop.

    You can write the comments in a PHP array, and read them using PHP foreach.

    Here is the sample code:

    <?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.');       
        //You could use a 2D array to store both comments Labels and comments Texts in one array, but since you are new to PHP, I put them in 2 different arrays to make it easy.
        //You can add more comments texts/labels to the arrays in double/single quotes separated with ','
        //For now, the arrays are filled manually. in future, you can add a DB to store comments and fetch the values from there.
    
        $counter = 0; 
        //we need a counter to make the comment ids dynamic
    ?>
    
    <!--GENERATING LABELS-->
    <div  class="signals"> 
        <ul>
    
        <?php
        foreach ($commentsLabels as $commentLabel) { //loop starts
            $counter++; //increasing $counter by 1, for each iteration      
            //We have to add HTML tags outside the php code block.
        ?>
                <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 hope this helps you start. :-)

    Faraz Sh.
    • 377
    • 2
    • 8
    • Great comments into the code, i understood how it works! Thank you very much! I tried to use your code over the one that i posted. I saved the file with a .php extension and loaded on my web hosting. unfortunately it doesn't appear nothing, maybe i'm missing something? Second question: can i move the "generating popups" code into a secondary php sheet, and recall it into the first one? Thank you anyway, you gave me a big help. – Andrea Dec 27 '15 at 17:56
    • You are welcome Andrea. That was a quick coding, so let me run it on my server right now and let you know. You can also put the popups in another php file and use php include(file) function to load it from the external source. – Faraz Sh. Dec 27 '15 at 19:41
    • Oh, found that. I had missed 's' at the end of the variable names '$commentsLabel', and '$commentsText' (at line 2 and 3). They should be '$commentsLabels', and '$commentsTexts'. I've edited them and the code is now working fine. The code in the answer is fixed to show the correct variable names too. Please check and confirm at your end too. Thank you – Faraz Sh. Dec 27 '15 at 19:48
    • I'm really sorry for the delay, i was working :)... i tried it just now, and it worked flawlessly! i'm sorry, i didn't noticed the 's' error. Thank you very much! i will study the (file) function and will try to export the php popup content in another sheet. My objective is to create a future "front end" on this secondary php sheet, so i want to separate the two. Again. Thank you very much for your support and your explanations – Andrea Dec 28 '15 at 13:30
    • You are welcome Andrea. If this is solved your issue, I appreciate if you mark the answer. Regards – Faraz Sh. Dec 28 '15 at 13:49
    • Absolutely: i just did it. Thanks again – Andrea Dec 28 '15 at 14:49