【问题标题】:Displaying data from arrays in a dynamic table PHP在动态表 PHP 中显示来自数组的数据
【发布时间】:2016-01-17 02:25:10
【问题描述】:

我有一个包含 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 " $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>

【问题讨论】:

    标签: php html arrays string jpeg


    【解决方案1】:

    在 php 文件中你需要回显你想要的数据。 但是,如果您在 php 文件中,您可以像这样关闭 php。 ? > 并编写html代码。 当你想显示 php 属性时,你需要再次打开一个这样的 php: 并继续这样... php f只能返回字符串或json数据 让我知道它是否适合你....

    【讨论】:

    【解决方案2】:

    我认为您可能正在寻找与此算法类似的东西。它每 5 个值添加一行。您需要做一个除数,使其成为ceil(),以使其添加任何所需的空单元格。如果您执行&lt;ul&gt;&lt;li&gt; 并使用CSS 使它们像表格一样显示,则可以执行此foreach。那么你就不需要计算额外的单元格了。

    $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>';
    

    【讨论】:

    • 即使我使用确切的代码,它在 Chrome 中也会像这样输出:'; foreach($CountryArtists as $value) { if($i == 1) echo ""; echo ''.$value.''; if($i == 5) { 回声 ""; $i=0; } $i++; } 回声''; ?>...........我一定是做错了什么?
    • 是的,你做错了什么。等等,我会努力让它更接近你需要的......
    • 感谢您的帮助,我可以发布完整的文件,可能与我的页面设置方式有关。
    • 现在看看编辑。应该会更清楚。
    • 它仍然对我不起作用,我确定您的代码是正确的。你能看看我的编辑,如果我做错了什么,请告诉我
    猜你喜欢
    • 2018-05-12
    • 2021-06-28
    • 1970-01-01
    • 2020-10-02
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多