2015-10-18 62 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 –