2015-05-27 83 views
-1

我想從'圖像'文件夾創建自動完成。 「圖片」文件夾中有超過50張圖片。如何從文件夾中排列所有圖像名稱?如何從文件夾中排列文件的名稱?

的search.php

<?php 

/* 
* Load sample data 
*/ 
include 'data.php'; 

/* 
* Results array 
*/ 
$results = array(); 

/* 
* Autocomplete formatter 
*/ 
function autocomplete_format($results) { 
    foreach ($results as $result) { 
     echo $result[0] . '|' . $result[1] . "\n"; 
    } 
} 

/* 
* Search for term if it is given 
*/ 
if (isset($_GET['q'])) { 
    $q = strtolower($_GET['q']); 
    if ($q) { 
     foreach ($data as $key => $value) { 
      if (strpos(strtolower($key), $q) !== false) { 
       $results[] = array($key, $value); 
      } 
     } 
    } 
} 

/* 
* Output format 
*/ 
$output = 'autocomplete'; 
if (isset($_GET['output'])) { 
    $output = strtolower($_GET['output']); 
} 

/* 
* Output results 
*/ 
if ($output === 'json') { 
    echo json_encode($results); 
} else { 
    echo autocomplete_format($results); 
} 

data.php

<?php 
     $data = array(/* 
* image_name 
*/); 

HTML

<link rel="stylesheet" type="text/css" href="src/jquery.autocomplete.css"> 

    <script type="text/javascript" src="src/jquery.autocomplete.js"></script> 
<script type="text/javascript" src="src/jquery.min.js"></script> 
    <script> 
    $("#ac5").autocomplete('search.php', { 
      minChars: 1, 
      useDelimiter: true, 
      selectFirst: true, 
      autoFill: true, 
     }); 

    </script> 

    <form> 
      <input type="text" id="ac5"> 
     </form> 

我使用jquery.autocomplete.js和不工作...

+0

什麼?你想要什麼? – Rizier123

+1

你想把一個目錄中的所有文件名放入一個數組中嗎?如果是這樣,然後使用'scandir'例如'$ files = scandir(「directory」);' – jakecfc1992

+0

是否可以從文件夾創建數組[文件名]? – Foxpro

回答

0

嘗試這樣。

我已經拿到了數據庫文件夾,在這個文件夾中有4個文件在那裏,所以打印出來的文件名稱爲數組。

代碼: -

<?php 
$dir = '/work/database'; 
$files1 = scandir($dir); 
print_r($files1); 
?> 

輸出: -

Array ([0] => . [1] => .. [2] => logo.png [3] => logos.jpg [4] => nbc_logo.jpg [5] => sciit_logo.png) 
+0

我想要這樣做'<?php $ data = array( 「Great Bittern」=>「Botaurus stellaris」 ,); ?>'Ex - '<?php $ data = array( // image_name); ?>。如何?有可能嗎? – Foxpro

+0

以上代碼是否滿足您的要求?輸出如你所料。 – RaMeSh

+0

如果你想這樣只是刪除「回聲」數組類型:「。」

';" and "echo '
';「這兩條線。 – RaMeSh

0

根據你的問題我已經瞭解到,你想要在你的特定文件夾中的所有圖像的名稱最好的方式來獲取目錄中的所有圖像的列表是在php中使用​​3210函數。

$images = glob('/youfolderpath/*.{jpeg,gif,png}', GLOB_BRACE); 
//images has all your image names will jpeg, gif and png format, $images is an array that contains all your image names 

有用的鏈接:http://www.w3schools.com/php/func_filesystem_glob.asp

+0

它會工作,沒有數組我的意思是[替換數組?]'<?php $ data = array(// image_name); ?>'到'<?php $ data = glob('/ youfolderpath/*。{jpeg,gif,png}',GLOB_BRACE); ' – Foxpro

+0

是的,你只需要提供文件夾名稱,它將使用參數 –

+0

中指定的擴展名掃描該文件夾中的所有圖像,同時請記住,使用此函數不會在數組中獲得任何額外數據,恰好所需的數據只出現在陣列中 –

相關問題