0

I am working on a php code as shown below in which I am trying to retrieve mp4 and xml files name.

$src_dir    = 'incoming_folder';  /* Place where mp4 file is present */
$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir)); /* Line #Y */
print_r($mp4_files);     /* Line #A */ 
$xml_files = preg_grep('~\.(xml)$~', scandir($src_dir)); /* Line #Z */
print_r($xml_files); /* Line #B */   

Line#A prints the following:

Array ( [2] => 45017P.mp4 [3] => 45031P.mp4 [5] => hello.mp4 

Line#B prints the following:

Array ( [2] => 45017.xml [5] => 45031P.xml [7] => hello.xml )

I have integrated Line#Y and Line#Z code in the html below in order to retrieve the file names from a directory.

<table width="100%">
   <tr>
      <!-- This inline css will go inside addiional css -->
   </tr>
   <th style="width:8%;">House#</th>
   <th style="width:8%;">Date</th>
   <th style="width:8%;" >Time</th>
   <th style="width:8%;">MP4</th>
   <th style="width:8%;" >XML</th>
   <?php    foreach ($mp4_files as $file ) {
      foreach ($xml_files as $file2 ) {     
      ?>
   <tr>
      <td style="width:8%; text-align:center;"> <?php echo basename($file, ".mp4"); ?></td>
      <td style="width:8%; text-align:center;"><?php echo date("F d Y",filemtime("incoming_folder/$file")); ?></td>
      <td style="width:8%; text-align:center;"><?php echo date("H:i:s",filemtime("incoming_folder/$file")); ?></td>
      <td style="width:8%; text-align:center;"> <?php echo basename($file); ?></td>
      <td style="width:8%; text-align:center;"> <?php echo basename($file2); ?></td>
   </tr>
   <?php
      }
      }
      ?>
</table>

The above html/php code prints the following:

House#      Date          Time         MP4         XML
45017P  April 30 2019   10:21:12    45017P.mp4  45017P.xml
45017P  April 30 2019   10:21:12    45017P.mp4  45031P.xml
45017P  April 30 2019   10:21:12    45017P.mp4  hello.xml
45031P  May 01 2019     14:56:56    45031P.mp4  45017P.xml
45031P  May 01 2019     14:56:56    45031P.mp4  45031P.xml
45031P  May 01 2019     14:56:56    45031P.mp4  hello.xml
hello   April 30 2019   10:21:12    hello.mp4   45017P.xml
hello   April 30 2019   10:21:12    hello.mp4   45031P.xml
hello   April 30 2019   10:21:12    hello.mp4   hello.xml

Problem Statement:

In the o/p above for the column House#, I can see two 45017P, two 45031P, and two hello. I am wondering how I can avoid the redundancy so that it shows something like this:

House#    Date           Time           MP4       XML
45017P  April 30 2019   10:21:12    45017P.mp4  45017P.xml 
45031P  May 01 2019     14:56:56    45031P.mp4  45031P.xml
hello   April 30 2019   10:21:12    hello.mp4   hello.xml
flash
  • 1,455
  • 11
  • 61
  • 132
  • 1
    inside loop should be receiving the field from the result of outside loop other wise what is the point?In your case what is the relationship between two arrays other than they have similar values – zod May 16 '19 at 19:05
  • @zod Yes inside loop should be receiving the field from the result of outside loop. I am not sure how can we do it ?. Can you give me some pointers ? – flash May 16 '19 at 19:10
  • $files = []; foreach ($mp4_files as $file ) { $basename = basename($file, ".mp4"); $files[$basename]['basename'] = $basename $files[$basename]['date'] = $date("F d Y",filemtime("incoming_folder/$file")) $files[$basename]['time'] = $date("H:i:s",filemtime("incoming_folder/$file")) $files[$basename]['mp4'] = basename($file) } – Astrea May 16 '19 at 19:17
  • foreach ($xml_files as $file ) { $basename = basename($file, ".xml"); if(!array_key_exists($basename, $files) { $files[$basename]['basename'] = $basename $files[$basename]['date'] = $date("F d Y",filemtime("incoming_folder/$file")) $files[$basename]['time'] = $date("H:i:s",filemtime("incoming_folder/$file")) } $files[$basename]['xml'] = basename($file) } – Astrea May 16 '19 at 19:17
  • foreach($files as $file) { ?> – Astrea May 16 '19 at 19:17
  • Sorry for that code, but I can’t do better in a comment... Your problem happen because you run all of your elements from `$xml_files` for each element of your `$mp4_files` – Astrea May 16 '19 at 19:19
  • @Astrea You have pasted three codes. I am wondering which one I should be using ? – flash May 16 '19 at 19:51
  • All of them, in the same order. I'm not able to past the full code in only one comment. And also, remove `$` from `$date(...)` – Astrea May 16 '19 at 19:53
  • and add `;` in each lines when is missing (first and second foreach) – Astrea May 16 '19 at 20:01
  • @Astrea Something like this http://jsfiddle.net/nmtdv2yq/1/ I pasted your code inside fiddle. – flash May 16 '19 at 20:05
  • @flash Yes, nice! http://jsfiddle.net/20yogvub/2/ – Astrea May 16 '19 at 20:09
  • @Astrea Its not working. Am I missing anything ? – flash May 16 '19 at 20:12
  • Hum, I found some bugs on my code. maybe I can try if you fill the arrays (`$mp4_files` and `$xml_files`) http://jsfiddle.net/20yogvub/4/ – Astrea May 16 '19 at 20:19
  • @flash I have make tests and optimizations. You can also do this http://jsfiddle.net/20yogvub/5/ – Astrea May 17 '19 at 13:43

0 Answers0