在动态表PHP中显示数组中的数据

时间:2015-10-18 19:58:33

标签: php html arrays string jpeg

我有一个包含20张乡村艺术家照片的文件,以及一个包含他们网站的文本文件。我正在尝试使用PHP在4行5列表中显示这些数据。

我尝试使用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,但它只是在浏览器输出中显示了echo:

" 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>

2 个答案:

答案 0 :(得分: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>';

答案 1 :(得分:0)

在php文件中,您需要回显所需的数据。 但是如果你在php文件中你可以像这样关闭php。 ? &GT; 并编写HTML代码。 当你想显示php属性时,你再次需要打开这样的php: 并继续这样...... Php fcan只返回字符串或json数据 如果它适合你,请告诉我....