Search code examples
phpglobscandir

php globe array, separate first file of directory from


I'm trying to get .php files in a directory to an array using globe(). Works fine. Than I would like to have the first file in one div and all the others in another div.

    <?php
    $files = glob("kalenderitems/*.php");
    $num_files = count($files);
    $i = 0;
    ?>
    <div class="kal-item-<?php echo $num_files?>-col-left">
    <?php
    foreach (glob("kalenderitems/*.php") as $filename){
    if ($i == 1) {
    ?>
    <div class="kal-item-<?php echo $num_files?> nr-<?php echo $i++ ?> equaliser">
    <?php
    include $filename
    ?>
    </div>
    <?php
    };
    } 
    ?>
    </div>
    <!-- end DIV 1 -->
    <div class="kal-item-<?php echo $num_files?>-col-right">
    <?php
    foreach (glob("kalenderitems/*.php") as $filename){
    if ($i++ > 1) {
     ?>
    <div class="kal-item-<?php echo $num_files?> nr-<?php echo $i++ ?> equaliser">
    <?php
    include $filename
    ?>
    </div>
    <?php
    };
    }    
    ?>
    </div>

Array content (glob("kalenderitems/*.php")) gives me: insert1.php, insert2.php, insert3.php, insert4.php

Goal Structure

    <div class="col-left">
      <div class="element 1">include (insert1.php)</div>
    </div>
    <div class="col-right">
      <div class="element 2">include a file</div>
      <div class="element 2">include a file</div>
      <div class="element 2">include a file</div>
    </div>    

EDIT A php file must be included in the div's. Provided solution is only able to build the structure and put html in between.


Solution

  • I believe you are looking for array_shift(). It removes and returns the first element in an array, so this should achieve what you're aiming for:

    <?php
    
    $files      = glob("kalenderitems/*.php");
    $num_files  = count($files);
    
    ?>
    <div class="kal-item-<?php echo $num_files; ?>-col-left">
         <div class="kal-item-<?php echo $num_files; ?> nr-1 equaliser">
            <?php include(array_shift($files)); ?>
         </div>
    </div>
    
    <!-- end DIV 1 -->
    <div class="kal-item-<?php echo $num_files?>-col-right">
        <?php foreach($files as $filename): ?>
            <?php include($filename); ?>
        <?php endforeach; ?>
    </div>