koc koc - 1 year ago 72
MySQL Question

I cannot count total files in folder which dose not exist in database and without ignore files by php

I want to list all my files in a particular folder which files is not entry at mysqli database. After done this well, want make a pagination for 20 files par page.

Here is my problem: I cannot count total files which dose not exist in database and without ignore files also.

Here my pagination not display any output also.

// Query in database
$sqlFind = 'SELECT `link` FROM `movie`';
$result = mysqli_query($dbh, $sqlFind);
$db = [];
while ($row = mysqli_fetch_row($result))
array_push($db, $row[0]);

// Check files
$ignore = array('.','..','cgi-bin','.DS_Store');
$limit = 23;
$page = (int)$_GET['page'];
if(!($page>0)) $page = 1;
$offset = ($page-1)*$limit;

$directory = C:/xampp/htdocs/movie/;
$files = scandir($directory,1);

/****** Make a limit display result at par page *****/
$files1 = array_slice($files, $offset, $limit);

if ( $files1 !== false ) {
$y = 0;
foreach ($files1 as $i => $value) {
if (!in_array($value, $ignore)) { // check ignore type file
if (!in_array($value, $db)) { // check with database
// File not exists in both
echo $y.': '.$value."<br>";
$total = count($files1); // How to count total without ignore and with database match
$totalPages = ceil( $total/ $limit ); //calculate total pages

$pagLink = "<ul class='pagination'> Go to: ";
for ($z=1; $z<= $total_pages; $z++) {
// Can't see anything at pagination
$pagLink .= "<li class=''><a href='".$thispage."? url=".$thisurl."&page=".$z."'>".$z."</a></li>";
echo $pagLink . "</ul>";

} else { echo 0;} //!==false

Answer Source

Use array_dff to remove all the names from the DB and ignored names before you do the pagination.

$files = scandir($directory, 1);
$files = array_diff($files, $db, $ignore);
$files1 = array_slice($files, $offset, $limit);

Then you don't need all the in_array() checks in the loop. It should just be:

foreach ($files1 as $i => $value) {
    echo "$i: $value<br>";
$total = count($files);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download