doukezi4576 2017-07-27 20:03
浏览 41
已采纳

试图创建一个生成javascript文件的php foreach循环

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) . "
", 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条回答 默认 最新

  • dongliangkeng1056 2017-07-28 09:03
    关注

    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 .= "
    fileList[\"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"];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?