0

So I have this script which im trying to create a filelist.js file from in php here's the code:

//CREATE fileslist.js
$thumbssize = "1";
$moviesize= "1";
$thumbarray = array_slice(scandir($tpath), 2);

echo print_r($thumbarray) . "<br/>";

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    $script="
fileList = {};
fileList[\"thumbs\"] = {};
fileList[\"movies\"] = {};
fileList[\"title\"]=\"{$foldername}\";";

foreach($thumbarray as $item) {
print "fileList[\"thumbs\"][1]=["$item['0']","$item['2']","$item['3']","$item['4']","$item['5']","$item['6']"];";
    }

$script2 = "    
fileList[\"thumbs\"][\"size\"]={$thumbssize};
fileList[\"thumbs\"][\"size\"]={$moviesize};";

//$script = preg_replace('/\s\s+/', ' ', $script);
$scriptName= $_SERVER['DOCUMENT_ROOT'] . "/{$foldername}/assets/js/filelist.js";
file_put_contents($scriptName, trim($script . $script2) . "\n", FILE_APPEND);
echo "Generating filelist.js</br>";
    }

This here is the example output of the array:

Array ( [0] => 863_example_r003.mp4 [1] => 863_example_r004.mp4 [2] => 863_example_r005.mp4 [3] => 863_example_r006.mp4 [4] => 863_example_r007.mp4 ) 1

This is the filelist.js output as it stands right now:

fileList = {};
fileList["thumbs"] = {};
fileList["movies"] = {};
fileList["title"]="test7";

fileList["thumbs"]["size"]=1;

fileList["thumbs"]["size"]=1;

So up until this point its doing what i want except I cannot figure out how to make a foreach loop to generate this type of output :

fileList = {};
fileList["thumbs"] = {};
fileList["movies"] = {};
fileList["title"]="863-example";
fileList["thumbs"][1]=["863_example_r048.mp4","863_example_r049.mp4","863_example_r050.mp4","863_example_r051.mp4","863_example_r052.mp4","863_example_r053.mp4"];
fileList["thumbs"][2]=["863_example_r054.mp4","863_example_r055.mp4","863_example_r056.mp4","863_example_r057.mp4","863_example_r058.mp4","863_example_r059.mp4"];
fileList["thumbs"][3]=["863_example_r060.mp4","863_example_r061.mp4","863_example_r062.mp4","863_example_r003.mp4","863_example_r063.mp4","863_example_r064.mp4"];
fileList["thumbs"][4]=["863_example_r065.mp4","863_example_r004.mp4","863_example_r067.mp4","863_example_r068.mp4","863_example_r069.mp4","863_example_r070.mp4"];
fileList["thumbs"][5]=["863_example_r005.mp4","863_example_r071.mp4","863_example_r072.mp4","863_example_r073.mp4","863_example_r074.mp4","863_example_r006.mp4"];
fileList["thumbs"][6]=["863_example_r075.mp4","863_example_r076.mp4","863_example_r077.mp4","863_example_r078.mp4","863_example_r007.mp4","863_example_r079.mp4"];
fileList["thumbs"][7]=["863_example_r080.mp4","863_example_r081.mp4","863_example_r082.mp4","863_example_r008.mp4","863_example_r083.mp4","863_example_r084.mp4"];
fileList["thumbs"][8]=["863_example_r085.mp4","863_example_r086.mp4","863_example_r009.mp4","863_example_r087.mp4","863_example_r088.mp4","863_example_r089.mp4"];

The [1], [2], [3] refers to a page number and there needs to be 6 items per page as the example above

3
  • what's wrong with using json_encode? Commented Jul 27, 2017 at 20:08
  • Did you have an actual question? Or are you just asking us to fix it for you? Commented Jul 27, 2017 at 20:09
  • The question is how would i go about getting what i need to accomplish! Commented Jul 27, 2017 at 21:45

3 Answers 3

1

Thank you for your feedback. Just a minute ago I refined the code and tested it in my localhost. Here is the changed code that gives the result:

$script1 = "";
$i       = 0;
$j       = 0;
$inrow   = 6; // files in row
foreach($thumbarray as $item => $itemv) {

  // beginning of the row
  if($j % $inrow == 0){ 
    $script1 .= "\nfileList[\"thumbs\"][".($i+1)."]=[";
  }

  $script1 .= "\"" . $itemv . "\","; // add filename "863_example_r048.mp4",

  // end of the row
  if(($j + $inrow + 1) % $inrow == 0){
    // Delete comma at end of row
    if(substr($script1,-1) == ","){ 
      $script1 = substr($script1, 0, strlen($script1) - 1);
    }
    $script1 .= "];";
  }
  if($j % $inrow == 0){ 
    $i++;
  }
  $j++;
}

Here is the tested result:

fileList["thumbs"][1]=[".AndroidStudio1.3",".ICEauthority",".PlayOnLinux",".Skype",".Xauthority",".adobe"]; fileList["thumbs"][2]=[".android",".audacity-data",".bash_history",".bashrc",".bibletime",".cache"]; fileList["thumbs"][3]=[".claws-mail",".config",".dbus",".directory",".dmrc",".dosbox"]; fileList["thumbs"][4]=[".emacs",".esd_auth",".fltk",".fontconfig",".fonts",".fonts.conf"]; fileList["thumbs"][5]=[".frozen-bubble",".gimp-2.8",".gnome",".gnome2",".gnome2_private",".gnupg"]; fileList["thumbs"][6]=[".gstreamer-0.10",".gtkrc-2.0",".gvfs",".icewm",".inputrc",".ivy2"]; fileList["thumbs"][7]=[".java",".jsword",".kde",".kde4",".kismet",".local"]; fileList["thumbs"][8]=[".macromedia",".mozilla",".mplayer",".mtpaint",".opera",".pki"]; fileList["thumbs"][9]=[".profile",".putty",".pyhistory",".qt",".remmina",".sbt"]; fileList["thumbs"][10]=[".skel",".ssh",".streamtuner",".sword",".themes",".thumbnails"]; fileList["thumbs"][11]=[".thunderbird",".vnc",".wine",".xboardrc",".xdg_menu_cache",".xim.template"]; fileList["thumbs"][12]=[".xinitrc.template",".xiphos",".xscreensaver",".xsession-errors",".xsession-errors-192.168.1.101:1",".xsession-errors-192.168.1.103:1"]; fileList["thumbs"][13]=[".xsession-errors-:0",".xsession-errors-:1",".y2log",".y2usersettings","20150901_134031.jpg","281436_wpmu-dev-dashboard-3.5.3.zip"]; fileList["thumbs"][14]=["Android","AndroidStudioProjects","Bible Analyzer User Modules","Calibre Library","Desktop","Documents"]; fileList["thumbs"][15]=["Downloads","Java","Kismet-20141124-23-54-29-1.alert","Kismet-20141124-23-54-29-1.gpsxml","Kismet-20141124-23-54-29-1.nettxt","Kismet-20141124-23-54-29-1.netxml"]; fileList["thumbs"][16]=["Kismet-20141124-23-54-29-1.pcapdump","Movies","Music","Phpgtk","Pictures","PlayOnLinux's virtual drives"]; fileList["thumbs"][17]=["Public","Templates","Videos","VirtualBox VMs","WPMUDEV_PLUGINS.zip","battery"]; fileList["thumbs"][18]=["bibleanalyzer","bibledesktop-2.0-beta","bin","ca.key","deklaracia.jpg","deklaracia2.jpg"]; fileList["thumbs"][19]=["dfc","http:⁄⁄www.biblestudytools.com⁄niv⁄.desktop","konq","projects","public_html","scala"]; fileList["thumbs"][20]=["somefile","somefile.txt","viber","wget-log","wp-cron.php?import_key=d&import_id=20&action=processing","Запис0003.amr"];

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah i made them same changes except i couldnt get the first two rows to close correctly with the ]; all is working now exceptionally
0

you need to change below part

$index = 1;
foreach($thumbarray as $item) {
 $script .= "fileList[\"thumbs\"][$index]= ".json_encode($item).";" ;
 $index++;
}

Comments

0

You need two indexes. $i is incremented every time $i is deleted in $inrow times properly with the remainder of 0. If you want 6 rows of files then $i should be increased 6 times less then $j which is increased every time. This way $i would change when $j is 6,12,18,24 and etc. You will need to play a little bit with the code ...

$script1 = "";
$i       = 0;
$j       = 0
$inrow   = 6; // files in row
foreach($thumbarray as $item => $itemv) {

  // beginning of the row
  if($j % $inrow == 0){ 
    $script1 .= "fileList[\"thumbs\"][".($i+1)."]=[";
  }

  $script1 .= "\"" . $itemv . "\","; // add filename "863_example_r048.mp4",

  // end of the row
  if($j % $inrow == 0){
    // Delete comma at end of row
    if(substr($script1,-1) == ","){ 
      $script1 = substr($script1, 0, strlen($script1) - 1);
    }
    $script1 .= "]";
    $i++;
  }
  $j++;
}

6 Comments

At first I wrote a code with one index but this wouldn't work because there can be thousands of files in the directory! The $i index must increment not on every foreach iteration but after the maximum number of files in a row is reached.
willt try this shortly and report back
the output of the JS added below
fileList["thumbs"][".(0+1)."]=["863_example_r003.mp4"]"863_example_r004.mp4","863_example_r005.mp4","863_example_r006.mp4","863_example_r007.mp4","863_example_r008.mp4",fileList["thumbs"][".(1+1)."]=["863_example_r009.mp4"]"863_example_r010.mp4","863_example_r011.mp4","863_example_r012.mp4","863_example_r013.mp4","863_example_r014.mp4",fileList["thumbs"][".(2+1)."]=["863_example_r015.mp4"]"863_example_r016.mp4","863_example_r017.mp4","863_example_r018.mp4","863_example_r019.mp4","863_example_r020.mp4",
almost there thanks alot im sure i can figure it out from here ill get back to you on the acceptance of 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.