从显示的数据阵列在动态表PHP [英] Displaying data from arrays in a dynamic table PHP

查看:132
本文介绍了从显示的数据阵列在动态表PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国画家的20张图片的文件,并与他们网站的文本文件。我想显示4排5列的表使用PHP此数据。

我尝试使用foreach循环迭代数组中每5个元素
(一个装载每行一个)

 的foreach(array_chunk($ CountryArtists,5,真)为$值){
    <表>
       &所述; TR>&下; TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>
    &所述; TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>
    &所述; TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>
    &所述; TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>
    &所述; TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>< / TR>
< /表>

我一直试图弄清楚如何将图像加载到数组,但有没有运气。即时通讯开始认为我必须把引用数组中的文件的位置,但我不知道。

  $ colsToDisplay = 5;
$ CountryArtists =阵列(C:\\用户\\ THEFOLDER \\图像);
$ ArtistImages =阵列(AJackson,BShelton,CUnderwood,DBentley,DLJones,德鲁克,JAldean,JCash,JJohnson,JStrait,KChesney, LAntebellum,LDavis,LRimes,MLambert,MMcBride,RTravis,STwain,TKeith,TMcgraw);
$文件名=C:\\用户\\ THEFOLDER \\图像;

我是比较新的PHP和真的只需要知道如何加载我的图片,以及如何使该表显示正常。

编辑:

我添加回声表线,但它只是显示在浏览器中输出的回声:

 回声$ CountryArtists [$值] $ ArtistImages [$值]回声。$ CountryArtists [$价值。 $ ArtistImages [$值]回声。$ CountryArtists [$价值。 $ ArtistImages [$值]回声。$ CountryArtists [$价值。 $ ArtistImages [$值]回声。$ CountryArtists [$价值。 $ ArtistImages [$值]回声}&GT?;

我的code现在看起来是这样的:

 的foreach(array_chunk($ CountryArtists,5,真)为$值){
    回声<表>
    回声< TR>< TD> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>中
    回声< TD方式> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>中
    回声< TD方式> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>中
    回声< TD方式> $ CountryArtists [$值] $ ArtistImages [$值]< / TD>中
    回声&所述; TD方式> $ CountryArtists [$值] $ ArtistImages [$值]&下; / TD>&下; / TR>中
    回声< /表>
}

我觉得我做的不对强,会如此感激有它指给我看。

完整的文件

 <!DOCTYPE HTML>
< HTML和GT;
<身体GT;< PHP
$ ArtistImages =阵列(AJackson,BShelton,CUnderwood,DBentley,DLJones,德鲁克,JAldean,JCash,JJohnson,JStrait,KChesney, LAntebellum,LDavis,LRimes,MLambert,MMcBride,RTravis,STwain,TKeith,TMcgraw);
$数= COUNT($ ArtistImages);
$ COLS = 6;
$ DIV =(int)的$数/(INT)$ COLS;
$差异= CEIL($ DIV);
回声
$鳍= $ COLS * $差异;$ A = 1;
呼应'<表>';
为($ I = 0; $ I< $鳍; $ I ++){
    如果($ A == 1)
        回声\\ T< TR>中。PHP_EOL;    $艺术家=(!空($ ArtistImages [$ i]))? $ ArtistImages [$ i]:;
    回声\\ t \\ t的。'< TD>'。$艺术家。'< / TD>'PHP_EOL。    如果($ A == $ COLS){
            回声\\ T< / TR>中。PHP_EOL;
            $ A = 0;
        }    $ A ++;
}
呼应'< /表>';
?>< /身体GT;
< / HTML>


解决方案

我想你可能会寻找类似这种算法的东西。它增加了一个排,每5值。你会希望做一个除数使其 CEIL(),使其添加任何需要的空单元格。如果你这样做,你可以做到这一点的foreach < UL> <李> 和使用CSS来让他们显示象表。然后,你不需要额外的计算单元。

  $ I = 1;
呼应'<表>';
的foreach($数组作为$值){
    如果($ I == 1)
        回声< TR>中;    呼应'< TD>'$价值。'< / TD>';    如果($ I == 5){
            回声< / TR>中;
            $ I = 0;
        }    $ I ++;
}
呼应'< /表>';

编辑:

下面是一个基于你一个更实际的版本:

  $ ArtistImages =阵列(AJackson,BShelton,CUnderwood,DBentley,DLJones,德鲁克,JAldean,JCash, JJohnson,JStrait,KChesney,LAntebellum,LDavis,LRimes,MLambert,MMcBride,RTravis,STwain,TKeith,TMcgraw);//计数数组总艺术家
$数= COUNT($ ArtistImages);
//选择多少每行显示
$ COLS = 6;
//除以列的总
$ DIV =(int)的$数/(INT)$ COLS;
//四舍五入(柜面数量将产生空白单元格
$差异= CEIL($ DIV);
// Mulitply最终的数字
$鳍= $ COLS * $差异;
//创建一个autoincrementer跟踪下一行
$ A = 1;
呼应'<表>'PHP_EOL。
为($ I = 0; $ I< $鳍; $ I ++){
    如果($ A == 1)
        回声\\ T< TR>中。PHP_EOL;
    //你需要看是否填充这个艺术家。如果没有,请空
    //如果没有这个离开它会有一个警告,不存在
    $艺术家=(!空($ ArtistImages [$ i]))? $ ArtistImages [$ i]:;
    回声\\ t \\ t的。'< TD>'。$艺术家。'< / TD>'PHP_EOL。
    如果($ A == $ COLS){
            回声\\ T< / TR>中。PHP_EOL;
            $ A = 0;
        }    $ A ++;
}
呼应'< /表>';

I have a file with 20 pictures of country artists, and a text file with their websites. I'm trying to display this data in a 4 row 5 column table using PHP.

I try to use a foreach loop that iterates for every 5 elements in the array (to load each row one by one)

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>

Ive been trying to figure out how to load the images into the array, but having no luck. Im starting to think i must put the reference to the file location in the array,but i am not sure.

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

I'm relatively new to PHP and really just need to know how to load my images and how to make this table show up correctly.

EDIT:

I added echo to the table lines but it just shows echo in the browser output:

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

My code now looks like this:

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

I feel like I'm doing strong wrong, would be so grateful to have it pointed out to me.

FULL FILE

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

解决方案

I think you may be looking for something similar to this algorithm. It adds a row every 5 values. You will want to do a divisor make it ceil() to make it add any required empty cells. You can do this foreach if you do <ul> and <li> and use CSS to make them display like a table. Then you don't need to calculate extra cells.

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

EDIT:

Here is a more practical version based on yours:

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

这篇关于从显示的数据阵列在动态表PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆