-2

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>

2 Answers2

2

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>';
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Even if i use that exact code it outputs like this in Chrome:'; foreach($CountryArtists as $value) { if($i == 1) echo ""; echo ''.$value.''; if($i == 5) { echo ""; $i=0; } $i++; } echo ''; ?>...........I must be doing something else wrong? – user3018771 Oct 18 '15 at 20:31
  • Yes, you are doing something wrong. Hold on, I will try and make it closer to what you need... – Rasclatt Oct 18 '15 at 20:34
  • Thank you for the help, i could post the full file, maybe its something to do with how my page is setup. – user3018771 Oct 18 '15 at 20:38
  • Take a look now at the edit. It should become more clear. – Rasclatt Oct 18 '15 at 20:51
  • It still does not work for me, im sure your code is right. can you please look at my edit and tell me if im doing anything wrong – user3018771 Oct 18 '15 at 21:14
  • You missing quotes in your array starting -> `RTravis", "STwain", TKeith", TMcgraw");` – Rasclatt Oct 18 '15 at 21:16
  • Not sure what this is -> `$CountryArtists = array("C:\Users\images");` If you are trying find files somehow using this, it will not work. You need to `scandir("C:\Users\images")` – Rasclatt Oct 18 '15 at 21:17
  • So the script you copied for your last edit, are you saying it's not working?? – Rasclatt Oct 18 '15 at 21:23
  • no its not working, am i missing some file required to use php or something? – user3018771 Oct 18 '15 at 21:26
  • It seems to load the html properly but the php code is just taken literally, it is a .PHP file – user3018771 Oct 18 '15 at 21:29
  • Oh, yeah, then the PHP parser is not being applied. Is the file name extension `.php` as opposed to `.html` or `.htm`? – Rasclatt Oct 18 '15 at 21:29
  • Chrome comes up with"'; for($i = 0; $i < $fin; $i++) { if($a == 1) echo "\t".PHP_EOL; $artist = (!empty($ArtistImages[$i]))? $ArtistImages[$i]: ""; echo "\t\t".''.$artist.''.PHP_EOL; if($a == $cols) { echo "\t".PHP_EOL; $a=0; } $a++; } echo ''; ?> – user3018771 Oct 18 '15 at 21:30
  • Yeah, php is not parsing the page. Is the file extension `.php`? – Rasclatt Oct 18 '15 at 21:31
  • Yes, i used it in caps .PHP, because if i used lower case it would still appear as a .txt and just display the exact same as notpad on the browser – user3018771 Oct 18 '15 at 21:32
  • Is this a localhost or hosted?? – Rasclatt Oct 18 '15 at 21:33
  • Not sure what you mean by localhost or hosted, not using a server or anything, just trying to access files from a folder on my pc – user3018771 Oct 18 '15 at 21:34
  • Well there you go. Two things, 1) You need to be running PHP as a server or virtual server on your PC or 2) You have to upload your files to a hosting company like godaddy, networksolutions, bluehost, etc.... that support PHP. Also, `.php` is case-sensetive so needs to be `.php` not `.PHP` – Rasclatt Oct 18 '15 at 21:37
  • WTF then how am i supposed to do this for homework, i was never told this or given any virtual php server application. Really appreciate your time, i will delete this post after so its not a bother. – user3018771 Oct 18 '15 at 21:40
  • I believe you can download and install one. Also, you can write PHP no problem, you just can not test it without a server environment. – Rasclatt Oct 18 '15 at 21:41
0

In the php file you need to echo the data you want. However if you in the php file you can close php like this. ? > And write html code. When you want to display php attributes you again need to open a php like this: and continue like this... Php fcan only return string or json data Let me know if it work for you....

Moshe Kamer
  • 27
  • 1
  • 7