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

Php 在动态表中显示数组中的数据,php,html,arrays,string,jpeg,Php,Html,Arrays,String,Jpeg,我有一个包含20张乡村艺术家照片的文件,还有一个包含他们网站的文本文件。我试图使用PHP在一个4行5列的表中显示这些数据 我尝试使用foreach循环,该循环对数组中的每5个元素进行迭代 (逐个加载每行) 我对PHP比较陌生,只需要知道如何加载图像以及如何正确显示此表 编辑: 我在表格行中添加了echo,但它只是在浏览器输出中显示echo: " echo " $CountryArtists[$Value] $ArtistImages[$Value]" echo " .$CountryArtist

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

我尝试使用foreach循环,该循环对数组中的每5个元素进行迭代 (逐个加载每行)

我对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>"
}
foreach(数组块($CountryArtists,5,true)为$Value){
回声“”
echo“$CountryArtists[$Value]$ArtistImages[$Value]”
回音“$CountryArtists[$Value]。$ArtistImages[$Value]”
回音“$CountryArtists[$Value]。$ArtistImages[$Value]”
回音“$CountryArtists[$Value]。$ArtistImages[$Value]”
回音“$CountryArtists[$Value]。$ArtistImages[$Value]”
回声“”
}
我觉得自己做错了,如果有人能指出这一点,我将不胜感激

完整文件

 <!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文件中,您需要回显所需的数据。 但是,如果您在php文件中输入,您可以像这样关闭php。 ? > 并编写html代码。 当您想要显示php属性时,您需要再次打开一个php,如下所示: 继续这样下去。。。 Php FCA只能返回字符串或json数据
让我知道它是否适合你

我想您可能正在寻找类似于此算法的东西。它每5个值添加一行。您需要使用除数使其
ceil()
来添加所需的空单元格。如果您执行
  • 并使用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>';
    
    $i=1;
    回声';
    foreach($array作为$value){
    如果($i==1)
    回声“;
    回显“.$value.”;
    如果($i==5){
    回声“;
    $i=0;
    }
    $i++;
    }
    回声';
    
    编辑:

    以下是一个基于您的更实用的版本:

    $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>';
    
    $ArtistImages=数组(“AJackson”、“BShelton”、“CUnderwood”、“DBentley”、“DLJones”、“DRucker”、“Jalden”、“JCash”、“JJohnson”、“JStrait”、“KChesney”、“Lantelbellum”、“LDavis”、“LRimes”、“MLambert”、“MMcBride”、“RTravis”、“STwain”、“TKeith”、“TMcgraw”);
    //计算阵列中的艺术家总数
    $count=计数($ArtistImages);
    //选择每行要显示的数量
    $cols=6;
    //将总数除以各列
    $div=(int)$count/(int)$cols;
    //四舍五入(如果数字将产生空单元格
    $diff=ceil($div);
    //把最后的数字加倍
    $fin=$cols*$diff;
    //创建自动递增器以跟踪下一行
    $a=1;
    回音“.PHP_EOL;
    对于($i=0;$i<$fin;$i++){
    如果($a==1)
    echo“\t”.PHP\u EOL;
    //您需要查看此艺术家是否已填充。如果未填充,请将其设为空
    //如果没有这个,它将有一个警告说不存在
    $artist=(!empty($ArtistImages[$i])?$ArtistImages[$i]:“”;
    echo“\t\t”。$artist.”PHP\u EOL;
    如果($a==$cols){
    echo“\t”.PHP\u EOL;
    $a=0;
    }
    $a++;
    }
    回声';
    
    即使我在Chrome中使用了它输出的代码,也可以看到这个:“;foreach($CountryArtists as$value){if($i==1)echo”“;echo”“.$value.”;if($i==5){echo”“;$i=0;}$i++;}”echo“”;?>………我一定是做错了什么?是的,你做错了什么。等一下,我会尽量让它更接近你所需要的…谢谢你的帮助,我可以发布完整的文件,也许这与我的页面设置有关。现在看看编辑。它应该会变得更清楚。它对我仍然不起作用,im确保你的代码是正确的。你能看看我的编辑并告诉我我是否做错了什么吗
    $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>';