Search code examples
phpjquery-toolsforeach-loop-container

Jquery Tools Overlay: php foreach loop not working


I put Jquery Tools's Overlay in my site to show a projects' info in several overlays. This works pretty ok, but I have been trying to 'automate' the code to read new projects and load them in overlays. What happen looks ok, but no matter which project I click, the overlays allways load the content of the first project...

I did a lot of googling around and copy-pasting to get this far, I am not (yet) much of a programmer, I hope the code doesn't scare you guys.. ;-)

Anyway, here's a link: http://www.wgwd.nl/test

If you click 'Projects' a normal div opens that loads all the projects it finds (two, for now). When you click one it opens that content in 3 overlays. As said, unfortunately it allways loads the same content independent of which project you click.

I have tried to assign the JScript a unique function name (generated with php from the project's filename) but that doesn't seem to work.

Any ideas? here's my code :

<?
    //reads projectfolder and distills 
    //a basename out of the project description.txt 
    $textfiles = glob('content/projects/*.txt', GLOB_BRACE);
    foreach ($textfiles as $textfile) { ?>
        <div id="details"> <?
            $pad = pathinfo ($textfile);
            $base_name = basename($textfile,'.'.$pad['extension']);

            // the link that opens the overlays. Don't think the "id" tag is nescessary
            echo '<a id="'.$base_name.'" href="#" onclick="'.$base_name.'()"><img src="'.$base_name.'/main.jpg"/></a>' ?>   

            <!-- defines overlay, hidden by default -->
            <div id="dragwindow1" class="overlay ol1">
                <a class="close"></a>
                <?
                    include ('content/projects/'.$base_name.'/content.txt'); 
                ?>
            </div> 
        </div> 
        <?
        // the description of each project
        include ($textfile);
        ?>

        <script> 
            // within the foreach open all overlays with function name $base_name
            var <?=$base_name?> = function () {
                $("a[rel]").each(function() {
                    $(this).overlay().load();
                });
            }

        </script>
        <hr />
    <? } //end foreach ?>
</div> 

<!-- somehow, without defining these links, the whole 'open all overlay' thing doesn't work -->
<a rel="div.overlay:eq(0)" type="button" style="display: none">first</an>
<a rel="div.overlay:eq(1)" type="button" style="display: none">second</a>
<a rel="div.overlay:eq(2)" type="button" style="display: none">third</a> 

<script type="text/javascript">
$(function projects() {
    // positions for each overlay
    var positions = [
        [120, '15%'], //uppper left, #1
        [70, '60%'], // lower left, #2
        ['60%', '40%'], // lower right, #3
    ];

    // setup triggers
    $("a[rel]").each(function(i) {

        $(this).overlay({
            // common configuration for each overlay
            oneInstance: false,

            // setup custom finish position
            top: positions[i][0],
            left: positions[i][1],
        });
    });
});
</script>

Thx in advance!

EDIT: I edited the code to omit all that's unrelated The question remains: Javascript only returns the content of the first call in the foreach loop. Is there anyway to generate multiple instances of the javascript for each loop in the PHP?


Solution

  • SOLVED! With big, big, help of a friend, who redefined how multiple Overlays from Jquery Tools could work (and should have worked in the first place...)

    Without getting too much into it, here's the code for future reference:

    Basically the trick is:

    // open all overlays
    function openAll(currentOverlays) {
        $(currentOverlays).each(function()
        {
            $(this).overlay().load();
        });
    }
    

    The complete page is now something like this:

    <script type="text/javascript">
    $(function () {
        // positions for each overlay
        var positions = [
            ['60%', 540], // lower right, #3
            [80, '65%'], // lower left, #2
            [120, '12%'], //uppper right, #1
    
            ];
    
        // setup triggers
        $("div.overlay").each(function(i) {
            $(this).overlay({
                // some configuration for each overlay
    
                // positioning the overlays
                top: positions[i % 3][0],
                left: positions[i % 3][1]
            });
        });
    });
    
    // open all overlays
    function openAll(currentOverlays) {
        $(currentOverlays).each(function()
        {
            $(this).overlay().load();
        });
    }
    
    // close all overlays
    function closeAll(currentOverlays) {
        $(currentOverlays).each(function()
        {
            $(this).overlay().close();
        });
    }
    </script>
    
    <div id="projectstarter">
        <h2>Projects</h2>
    
        <div class="maindetails">
            <a class="close"></a> <!-- defines a close button for the overlay -->
    
            <?
            $textfiles = glob('content/projects/*.txt', GLOB_BRACE);
            rsort($textfiles);
            foreach ($textfiles as $textfile) { 
    
                $pad = pathinfo ($textfile);
                $base_name = basename($textfile,'.'.$pad['extension']);
                echo '<a href="#" onclick="openAll(\'div.'.$base_name.'\')">';
                echo '<img src="./content/projects/'.$base_name.'/projectimage.jpg" class="thumb"/></a></div>';
                include '$textfile'; //project description
    
             } // end MAIN foreach ?>
        </div>
    </div>
    
    <div id="projects"> 
    <?
    foreach ($textfiles as $textfile) { 
            $pad = pathinfo ($textfile);
            $base_name = basename($textfile,'.'.$pad['extension']); ?>
                <div id="dragwindow3" class="<?=$base_name?> overlay ol3">
                    <a class="close"></a>
                    <h2>Media</h2>
                    <div class="details">
                        // include media here
                    </div>             
                </div>
    
    
                <div id="dragwindow2" class="<?=$base_name?> overlay ol2">
                    <a class="close"></a>
                    <h2>Credits</h2>
                    <div class="details">
                        // include credits here
                    </div>
                </div>
    
                <div id="dragwindow1" class="<?=$base_name?> overlay ol1 ">
                    <a class="close"></a>
                    <h2>Content</h2>
                    <div class="details">
                        // include content here
                    </div>
                </div>
    
        <? } ?>
    <script>
        $( "#projectstarter" ).overlay();
        $( "#projectstarter" ).draggable().resizable({ghost: true});
        $( ".ol1" ).draggable().resizable({ghost: true});
        $( ".ol2" ).draggable().resizable({ghost: true});
        $( ".ol3" ).draggable().resizable({ghost: true});
    </script>