2015-10-18 60 views
-2

我有一個包含20張國家藝術家圖片的文件,以及一個帶有他們網站的文本文件。我試圖使用PHP在4行5列表中顯示這些數據。在動態表格中顯示陣列中的數據PHP

我嘗試使用foreach循環,對於陣列 在每5個元素迭代(由一個加載每一行一個)

foreach(array_chunk($CountryArtists, 5, true) as $Value) { 
    <table> 
     <tr><td> $CountryArtists[$Value] $ArtistImages[$Value]</td> 
    <td> $CountryArtists[$Value] $ArtistImages[$Value]</td> 
    <td> $CountryArtists[$Value] $ArtistImages[$Value]</td> 
    <td> $CountryArtists[$Value] $ArtistImages[$Value]</td> 
    <td> $CountryArtists[$Value] $ArtistImages[$Value]</td></tr> 
</table> 

我一直試圖找出如何將圖像加載到數組,但沒有運氣。我開始想我必須把參考文件的位置放在數組中,但我不確定。

$colsToDisplay = 5; 
$CountryArtists = array("C:\Users\THEFOLDER\images"); 
$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", RTravis", "STwain", TKeith", TMcgraw"); 
$filename = "C:\Users\THEFOLDER\images"; 

我是比較新的PHP,真的只需要知道如何加載我的圖像,以及如何使這張表正確顯示。

編輯:

我添加回聲表線,但它只是顯示在瀏覽器中輸出的回聲:

" echo " $CountryArtists[$Value] $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo "" } ?> 

我的代碼現在看起來像這樣:

foreach(array_chunk($CountryArtists, 5, true) as $Value) { 
    echo "<table>" 
    echo "<tr><td> $CountryArtists[$Value] $ArtistImages[$Value]</td>" 
    echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>" 
    echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>" 
    echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>" 
    echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td></tr>" 
    echo "</table>" 
} 

我覺得我做錯了,會非常感激地向我指出。

完整的文件

<!DOCTYPE HTML> 
<html> 
<body> 

<?php 
$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", "RTravis", "STwain", "TKeith", "TMcgraw"); 
$count = count($ArtistImages); 
$cols = 6; 
$div = (int) $count/(int)$cols; 
$diff = ceil($div); 
echo 
$fin = $cols * $diff; 

$a = 1; 
echo '<table>'; 
for($i = 0; $i < $fin; $i++) { 
    if($a == 1) 
     echo "\t<tr>".PHP_EOL; 

    $artist = (!empty($ArtistImages[$i]))? $ArtistImages[$i]: ""; 
    echo "\t\t".'<td>'.$artist.'</td>'.PHP_EOL; 

    if($a == $cols) { 
      echo "\t</tr>".PHP_EOL; 
      $a=0; 
     } 

    $a++; 
} 
echo '</table>'; 
?> 

</body> 
</html> 

回答

1

我想你可能正在尋找類似於這種算法的東西。它每5個值添加一行。你會想要做一個除數,使它ceil(),使其添加任何所需的空單元格。如果你做<ul><li>並且使用CSS使它們像表格一樣顯示,你可以做到這一點。那麼你不需要計算額外的單元格。

$i = 1; 
echo '<table>'; 
foreach($array as $value) { 
    if($i == 1) 
     echo "<tr>"; 

    echo '<td>'.$value.'</td>'; 

    if($i == 5) { 
      echo "</tr>"; 
      $i=0; 
     } 

    $i++; 
} 
echo '</table>'; 

編輯:

這是基於你一個更實際的版本:

$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", "RTravis", "STwain", "TKeith", "TMcgraw"); 

// Count total artists in array 
$count = count($ArtistImages); 
// Choose how many to display per row 
$cols = 6; 
// Divide the total by the columns 
$div = (int) $count/(int)$cols; 
// Round up (incase the number will produce empty cells 
$diff = ceil($div); 
// Mulitply the final numbers 
$fin = $cols * $diff; 
// Create an autoincrementer to keep track of next rows 
$a = 1; 
echo '<table>'.PHP_EOL; 
for($i = 0; $i < $fin; $i++) { 
    if($a == 1) 
     echo "\t<tr>".PHP_EOL; 
    // You need to see if this artist is populated. If not, make empty 
    // If left without this it will have a warning saying not exists 
    $artist = (!empty($ArtistImages[$i]))? $ArtistImages[$i]: ""; 
    echo "\t\t".'<td>'.$artist.'</td>'.PHP_EOL; 
    if($a == $cols) { 
      echo "\t</tr>".PHP_EOL; 
      $a=0; 
     } 

    $a++; 
} 
echo '</table>'; 
+0

即使我使用精確的代碼,它在Chrome中輸出如下:'; foreach($ CountryArtists爲$ value){if($ i == 1)echo「」; echo''。$ value。''; if($ i == 5){echo「」; $ I = 0; } $ i ++; } echo''; ?> ...........我一定在做別的事嗎? – user3018771

+0

是的,你做錯了什麼。堅持下去,我會盡量讓它更接近你所需要的...... – Rasclatt

+0

謝謝你的幫助,我可以發佈完整的文件,也許它與我的網頁設置有關。 – user3018771

0

在PHP文件,你需要回應你想要的數據。 但是,如果你在php文件中,你可以像這樣關閉php。 ? > 並寫html代碼。 當你想顯示PHP屬性再次需要打開一個PHP這樣的: ,並繼續像這樣... 腓FCAN只返回字符串或JSON數據 讓我知道這是否爲你工作....

+0

看到這個一個http://stackoverflow.com/questions/900207/return-a- php-page-as-an-image –