php中目录列表的分页
我正在尝试获取目录列表脚本的分页 这是函数的原始代码。 此功能列出目录中的所有mp3文件和一些文件信息php中目录列表的分页,php,Php,我正在尝试获取目录列表脚本的分页 这是函数的原始代码。 此功能列出目录中的所有mp3文件和一些文件信息 $songs正在保存数组信息 原始代码 function htmlSongs(&$tmpl, $path_raw, $songs, $custom_pls = false, $c_pls_arr = null, $lofi = false, $various, $dir_write, $song_ratings) { global $z_url_base, $z_self, $z_im
$songs
正在保存数组信息
原始代码
function htmlSongs(&$tmpl, $path_raw, $songs, $custom_pls = false, $c_pls_arr = null, $lofi = false, $various, $dir_write, $song_ratings) {
global $z_url_base, $z_self, $z_img_play, $z_img_lofi, $z_img_down,
$z_admin, $z_img_new, $z_img_edit, $z_self_raw, $zc, $z_db,$img_src;
$i = 0;
$check_boxes = false;
$diff = $zc['new_time']*60*60*24;
if (($z_admin && $zc['cache']) || ($zc['playlists'] && $zc['session_pls']) || $zc['play_sel'] || $zc['cmp_sel']) {
$check_boxes = true;
}
$tmpl = str_replace('{$songs_form_opts}', "name='songs' method='post' action='${z_self}p=$path_raw'", $tmpl);
$temp_section = tmpGetSect($tmpl, "SONGROW");
$html = "";
$rating = ($z_db && $zc['rating_files']);
foreach ($songs as $song => $opts) {
$temp_song = $temp_section;
$valign = ($opts['blurb']) ? "valign='top'" : "";
$song_raw = getURLencodedPath($song);
$song_title = formatSongTitle(basename($song));
$temp_song = str_replace('{$row}', $i, $temp_song);
$temp_song = str_replace('{$valign}', $valign, $temp_song);
$t_blurb = ($dir_write) ? "${opts['blurb']} <a href='${z_self_raw}&pl=".getURLencodedPath(basename($song))."&l=22&m=3'>$z_img_edit</a>" : $opts['blurb'];
$temp_song = str_replace('{$song_blurb}', $t_blurb, $temp_song);
$t_song_opts = "";
$remote = false;
if ($zc['fake'] && isset($opts['fake'])) {
$fi = ($zc['mp3_info']) ? "<td colspan='4'> </td>" : "";
$temp_song = tmpDelSec($temp_song,"SONGINFO", $fi);
$fi = ($rating) ? "<td colspan='2'> </td>" : "";
$temp_song = tmpDelSec($temp_song,"SONGVOTE", $fi);
$temp_song = tmpDelSec($temp_song,"SONGRATING");
$t_song_title = preg_replace("/\.(".$zc['fake_ext'].")$/i","",$song_title);
} else {
$url_play = $url_end = "";
if ($zc['mp3_info'] || $zc['mp3_id3']) {
if ($zc['remote'] && preg_match("/\.(".$zc['remote_ext'].")$/i", $song)) {
$mp3 = new remoteFile($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3']);
$remote = true;
} else {
$mp3 = new mp3($zc['mp3_dir']."/$song", $zc['mp3_info'], $zc['mp3_id3'], $zc['mp3_info_faster']);
}
}
if ($check_boxes) {
$checked = ($custom_pls && in_array($song, $c_pls_arr)) ? " checked='checked'" : "";
$t_song_opts .= "<input type='checkbox' name='mp3s[]' value='$song_raw'$checked /> ";
} else {
$t_song_opts .= " ";
}
if ($zc['download']) {
if ($remote) {
if (isset($mp3->download)) $t_song_opts .= "<a href='".$mp3->download."'>$z_img_down</a> ";
} else {
$surl = ($zc['stream_int']) ? "${z_self}l=12&p=$song_raw" : "$z_url_base/".getURLencodedPath2($song);
$t_song_opts .= "<a href='$surl&Oauth=$newssid'>$z_img_down</a> ";
}
}
if ($zc['play']) {
$url_play = "<a type=\"audio/mpeg\" title='$song_title' href='$mp3->url' >";
$url_end = "</a>";
if($mp3->url != ""){
$t_song_opts .= "$url_play </a> ";
}else{
$t_song_opts .= "";
}
}
if ($zc['low']) {
$tmp = " <a href='${z_self}l=8&p=";
if (isset($opts['lofi'])) {
$t_song_opts .= "$tmp".preg_replace("/\.(".$zc['ext_mus'].")$/i", $zc['low_suf'].".$1", $song_raw).
"&m=1'>$z_img_lofi</a> ";
} elseif($zc['resample'] && preg_match("/\.(".$zc['ext_enc'].")$/i", $song)) {
$t_song_opts .= "$tmp$song_raw&m=6'>$z_img_lofi</a> ";
}
}
if ($zc['new_highlight'] && isset($opts['mtime']) && ($now - $opts['mtime'] < $diff)) {
if ($zc['new_img']) {
$new_end = $z_img_new;
$new_beg = "";
} else {
$new_beg = $zc['new_beg'];
$new_end = $zc['new_end'];
}
} else {
$new_beg = $new_end = "";
}
$t_song_title = "$new_beg";
if ($zc['mp3_id3'] && $mp3->tag) {
if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
$t_song_title .= $mp3->title;
} else {
$subdir_images = "";
$t_song_title .= "$song_title";
}
$t_song_titles = "$new_beg";
if ($zc['mp3_id3'] && $mp3->tag) {
if ($various && !empty($mp3->artist)) $t_song_title .= "$mp3->artist - ";
$t_song_titles .= $mp3->title;
} else {
//$t_song_titles .= "<a href='$surl' type='audio/mpeg' title='$song_title'><img src='$img_src' style='display:none' /></a>";
$t_song_titles .= "";
}
$t_song_title .= "$new_end";
if ($zc['mp3_info']) {
if ($mp3->info == 1) {
$t_time = $mp3->time;
$t_bitrate = "$mp3->bitrate kbps";
$t_frequency = round($mp3->frequency/1000,1). " kHz";
$t_size = sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb";
} else {
$t_size = (!$remote) ? sprintf("%.2f", round($mp3->filesize/1000000,2))." Mb" : " ";
$t_time = $t_bitrate = $t_frequency = " ";
}
$temp_song = str_replace('{$song_time}', $t_time, $temp_song);
$temp_song = str_replace('{$song_bitrate}', $t_bitrate, $temp_song);
$temp_song = str_replace('{$song_frequency}', $t_frequency, $temp_song);
$temp_song = str_replace('{$song_size}', $t_size, $temp_song);
} else {
$temp_song = tmpDelSec($temp_song,"SONGINFO");
}
if ($rating) {
$temp_song = str_replace('{$song_vote}', htmlVote($song), $temp_song);
$rating = (!empty($song_ratings) && isset($song_ratings[$song])) ? htmlVoteFormat($song_ratings[$song]) : " ";
$temp_song = str_replace('{$song_rating}', $rating, $temp_song);
} else {
$temp_song = tmpDelSec($temp_song,"SONGVOTE");
$temp_song = tmpDelSec($temp_song,"SONGRATING");
}
}
$temp_song = str_replace('{$song_title}', $t_song_title, $temp_song);
$temp_song = str_replace('{$song_titles}', $t_song_titles, $temp_song);
$temp_song = str_replace('{$song_opts}', $t_song_opts, $temp_song);
$temp_song = str_replace('{$pageshtml}', $t_pageshtml, $temp_song);
$html .= $temp_song;
$i = ++$i % 2;
}
$tmpl = tmpDelSec($tmpl,"SONGROW",$html);
$t_so_check = $t_so_sel = $t_so_pls = "";
if ($z_admin && $zc['cache']) {
$submit = ($custom_pls) ? _ZUPDATECUSTOM : _ZCREATECUSTOM;
$t_so_check .= "<input type='submit' value='$submit ' onClick=\"SubmitForm('songs','&l=3&z=3',false);\"/>";
}
if ($check_boxes) {
$t_so_check .= " "._ZCHECK.": <a href='javascript:void 0;' onclick=\"CheckBoxes('songs',true);\">"._ZALL."</a> | ".
"<a href='javascript:void 0;' onclick=\"CheckBoxes('songs',false);\">"._ZNONE."</a>";
if ($zc['play_sel'] || $zc['cmp_sel']) {
$url_beg2 = " <a class='htrack' type='audio/mpeg' href=\"javascript:CheckIt('songs',";
$t_so_sel .= _ZSELECTED.": ";
$err = _ZERR_SEL;
if ($zc['play'] && $zc['play_sel']) { $t_so_sel .= "$url_beg2'${z_self}l=8&m=7',true,'$err');\">$z_img_play</a>"; }
if ($lofi) { $t_so_sel .= "$url_beg2'${z_self}l=8&m=7&lf=true',true,'$err');\">$z_img_lofi</a>"; }
if ($zc['download'] && $zc['cmp_sel']) { $t_so_sel .= "$url_beg2'&l=8&m=8',false,'$err');\">$z_img_down</a>"; }
}
}
$title = substr($path_raw,strrpos($path_raw,"/")+1);
$t_so_pls = ($zc['playlists']) ? htmlPlaylistFormElements('songs') : " ";
$tmpl = str_replace('{$row}',$i, $tmpl);
$tmpl = str_replace('{$so_check}',$t_so_check, $tmpl);
$tmpl = str_replace('{$so_select}',$t_so_sel, $tmpl);
$tmpl = str_replace('{$so_playlist}',$t_so_pls, $tmpl);
$tmpl = str_replace('{$movie_title}', $title, $tmpl);
foreach ($songs as $song => $opts)
编辑代码
foreach ($pages[$pgkey] as $song => $opts) {
正常
我自己解决了这个问题
这是解决办法
我使用数组_切片
$i = $stat_prev = 0;
$p = 1;
$currents = htmlspecialchars($_GET['showpage']);
$pages = array_chunk($songs, 10);
$t_pageshtml .= '<center>Page '.($currents+1).' of '.count($pages).' <br>Pages: ';
for($i=1; $i< count($pages)+1; $i++){
$t_pageshtml .= '<a href="?p='.htmlspecialchars($_GET["p"]).'&showpage='.($i-1).'">'.$i.'</a> ';
}
$t_pageshtml .= '</center>';
$pgkey = (int)htmlspecialchars($_GET['showpage']);
$songs = array_slice($songs, $pgkey."0",10);
foreach ($songs as $song => $opts ) {
$i=$stat\u prev=0;
$p=1;
$currents=htmlspecialchars($_GET['showpage']);
$pages=array_chunk($songs,10);
$t_pageshtml.='Page'($currents+1)。'of'。计数($pages)。';
对于($i=1;$i$opts形式的歌曲){
您可以使用以下函数“分页”任何一维数组:
$pages = array_chunk($songs, $perPage = 10);
$pageCount = count($pages);
$currentPageSongs = $pages[$page-1]; # pages are zero-based
哦,天哪!所有这些都是为了列出一堆文件?12个全局文件?您是否能够将受影响的区域修剪成更小的代码段?您的代码中有很多非常强烈的代码味道!1)如果您必须向一个函数传递7个以上的参数,则会出现问题,您需要进行重构,通常这意味着要编写一系列更小的子函数s、 经验法则是每个任务一个函数。2)使用globals通常也是一种味道。它只是缺乏架构,是一种使变量可用的黑客行为,但它打破了所有封装和分离关注点的最佳实践。3)变量名中的下划线和缩写……等等。这是facebook的源代码吗?;)我的眼睛受伤了。@markus—“变量名中的下划线和缩写”现在你只是在挑剔;-)
$i = $stat_prev = 0;
$p = 1;
$currents = htmlspecialchars($_GET['showpage']);
$pages = array_chunk($songs, 10);
$t_pageshtml .= '<center>Page '.($currents+1).' of '.count($pages).' <br>Pages: ';
for($i=1; $i< count($pages)+1; $i++){
$t_pageshtml .= '<a href="?p='.htmlspecialchars($_GET["p"]).'&showpage='.($i-1).'">'.$i.'</a> ';
}
$t_pageshtml .= '</center>';
$pgkey = (int)htmlspecialchars($_GET['showpage']);
$songs = array_slice($songs, $pgkey."0",10);
foreach ($songs as $song => $opts ) {
$pages = array_chunk($songs, $perPage = 10);
$pageCount = count($pages);
$currentPageSongs = $pages[$page-1]; # pages are zero-based