Ignore first loop result - php

I have a a FOR loop that lists all files within a certain directory. One of the results is also INDEX.PHP, a result which I don't need. It's the first result/entry of the loop...
Is there any way to skip this first result and start looping from the second one?
Please help.
<?php
$myDirectory = opendir('.');
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray);
echo '<h5>There are ' . $indexCount . ' files in the Media Center</h5>';
sort($dirArray);
echo '<table width="100%">';
echo '<tr>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 10px 0 0 0">Download</th>';
echo '<th width="33%" align="center" class="admin_th">Filetype</th>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 0 10px 0 0">Filesize (in bytes)</th>';
echo '</tr>';
for($index = 0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != ".")
{
echo '<tr><td width="33%" align="center" class="admin_td-even">' . $dirArray[$index] . '</td>';
echo '<td width="33%" align="center" class="admin_td-odd">';
echo strtoupper(substr($dirArray[$index], -3));
echo '</td>';
echo '<td width="33%" align="center" class="admin_td-even">';
echo filesize($dirArray[$index]);
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
?>

As soon as you see it what you don't want, place a continue statement which backs the loop to for condition.

I see two ways of doing it:
You can start one index later:
Instead of for($index = 0; $index < $indexCount; $index++) { ...},
do
for($index = 1; $index < $indexCount; $index++) { ...}
You can also add a condition:
for example:
for ($index = 0; $index < $indexCount; $index++) {
if ($dirArray[$index] == 'INDEX.PHP') continue;
// rest of the loop
}
But they are a few ways you can improve your code. Instead of using opendir() and readdir(), you can just use scandir like this:
foreach (scandir('.') as $file) {
}
But it looks like you want to grab some media file and display them. So an even better solution would be to use the glob() function, like this:
foreach (glob("*{.mp3,.mp4,.jpg,.png}", GLOB_BRACE) as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}

Does the loop return the files as objects? then you could just add an if statement to the filename parameter

for($index = 0; $index < $indexCount; $index++)
{
//skip first entry
if($index == 0) continue;
if (substr("$dirArray[$index]", 0, 1) != ".")
{
echo '<tr><td width="33%" align="center" class="admin_td-even">' . $dirArray[$index] . '</td>';
echo '<td width="33%" align="center" class="admin_td-odd">';
echo strtoupper(substr($dirArray[$index], -3));
echo '</td>';
echo '<td width="33%" align="center" class="admin_td-even">';
echo filesize($dirArray[$index]);
echo '</td>';
echo '</tr>';
}
}

Just ignore the desired file by comparing name of the current file with desired. For example, you can do it in list retrieving:
$myDirectory = opendir('.');
while( false!==($entryName = readdir($myDirectory)) )
{
if( strtolower($entryName)=='index.php' )continue;
$dirArray[] = $entryName;
}
closedir($myDirectory);
Do not rely on the index (number of file), because your index.php may be not first in list.

for($index = 0; $index < $indexCount; $index++) {
if(index == 0) continue;
// other stuff
}

Related

Getting every 7th element from array while echoing a table

I'm getting the data I need from fgetcsv and store them into $data. It contains a table with a header row and lot's of info. Every 7th column is the path to where the file is stored.
I've already searched for what my problem is but I can't seem the find the solution.
My Code yet:
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
I want to add a hyperlink via <a href=?> on every 7th column but I don't know how. How can I do it and is that the right way?
You check every column is a 7th column or divisible by 7 you can just check if the variable is divided by 7 like this.
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
if( $x && !($x % 7) ){
echo '<a href=?>'
}
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
Check if the remainder by 7 of your counter is 0. If it is, then it is a multiple of seven and you can echo your desired string.
if($start % 7 === 0){
echo '<a href=?>'
}

1111. appearing after print sequence - php

Morning,
I have some code that takes a string of shoe sizes, explodes it and then puts it in a table. The code works perfectly and the info is displayed correctly on the page but after the table there is always a few 1's after the table. I'm really confused about this.
So the table looks like this (in columns of three on the webpage)
Available in the following sizes:
3 4 5
6 9 10
111
Here's the Codes, for the purpose of the example $x = 3|4|6|10|9
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort($splits);
$x = print "<table class='greentable'>";
$x .= print "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";
foreach ($splits as $split) {
$entry = print "<td>".$split."</td>";
if($counter == 2){
$entry .= print "</tr><tr>";
$counter =0;
} else {
$counter++;
}
}
if($counter == 1){
$entry .= print "<td></td><td></td>";
}elseif ($counter == 2) {
$entry .= print "<td></td>";
}
$x.= $entry;
$x.= print "</tr></tbody></table>";
return $x;
}
I'd really appreciate any help.
Thanks
Chris
I don't know why do you concatenate print with some variable, but you should know that function print returns 1 (always) as a result of execution, so doing
$x .= print 'some value';
is the same as
print 'some value';
$x .= 1;
try this:do not use print you are already appending it to $x just echo $x;
<?php
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort($splits);
$x = "<table class='greentable'>";
$x .= "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";
foreach ($splits as $split) {
$entry .= "<td>".$split."</td>";//use . to concate here
if($counter == 2){
$entry .= "</tr><tr>";
$counter =0;
} else {
$counter++;
}
}
if($counter == 1){
$entry .="<td></td><td></td>";
}elseif ($counter == 2) {
$entry .= "<td></td>";
}
$x.= $entry;
$x.="</tr></tbody></table>";
echo $x;
}
get_sizes_pipe("1|2|3|4");
Rather than using string concatenation ( which becomes inefficient with large strings ) add html strings to an array and return the flattened html from your function.
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort( $splits );
$html=array();
$html[]="
<table class='greentable'>
<thead>
<tr>
<th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th>
</tr>
</thead>
<tbody>
<tr>";
foreach( $splits as $split ) {
$html[]="<td>".$split."</td>";
if( $counter == 2 ){
$html[]="</tr><tr>";
$counter=0;
} else {
$counter++;
}
}
$html[]=( $counter == 1 ) ? "<td></td><td></td>" : "<td></td>";
$html[]="
</tr>
</tbody>
</table>";
return implode( PHP_EOL, $html );
}
/* call your function with whatever value of `$x` you need */
echo get_sizes_pipe( $what_is_x );

Add tr after 4 uploaded images

I want a gallery of uploaded images, showing 4 images each tr.
There needs to be a loop somewhere but I can't get it to work.
It needs to add a tr automatically when there are 4 images in a tr.
<?php
$folder = 'uploads/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
$sortedArray = array();
for ($i = 0; $i < $count; $i++) {
$sortedArray[date ('YmdHis', filemtime($files[$i]))] = $files[$i];
}
krsort($sortedArray);
echo '<table>';
foreach ($sortedArray as &$filename) {
echo '<td align="center">';
echo '<a name="'.$filename.'" href="#'.$filename.'"><img src="'.$filename.'" /></a>';
echo 'Bestand naam: ' . substr($filename,strlen($folder),strpos($filename, '.')-strlen($folder));
echo '</td>';
}
echo '</table>';
?>
Let a counter, say $i run alongside your foreach loop that ticks up by one every time the loop ran. Check for "every fourth element" with if ($i % 4 ==0)
Use a counter in your loop. It should look like this :
echo '<table>';
$ctr = 0;
foreach ($sortedArray as &$filename) {
echo ($ctr % 4 == 0) ? "<tr>" : "";
echo '<td align="center">';
echo '<a name="'.$filename.'" href="#'.$filename.'"><img src="'.$filename.'" /></a>';
echo 'Bestand naam: ' . substr($filename,strlen($folder),strpos($filename, '.')-strlen($folder));
echo '</td>';
$ctr++;
echo ($ctr % 4 == 0) ? "</tr>" : "";
}
echo '</table>';
<?php
$folder = 'uploads/';
$filetype = '*.*';
$files = glob($folder . $filetype);
$count = count($files);
$sortedArray = array();
$i = 0;
krsort($sortedArray);
echo '<table><tr>';
foreach($sortedArray as & $filename)
{
echo '<td align="center">';
echo '<a name="' . $filename . '" href="#' . $filename . '"><img src="' . $filename . '"/> </a>';
echo 'Bestand naam: ' . substr($filename, strlen($folder) , strpos($filename, '.') - strlen($folder));
echo '</td>';
if ($i % 4 == 0)
{
echo '</tr><tr>';
}
$i++;
}
echo '</tr></table>';
?>

PHP Sorting files in a table

Currently I have a table order by
A B C D
E F G H
but I would like to order the table by
A E
B F
C G
D H
Code:
for($i=0;$i<=count($aFiles);$i++)
{
if($i%5==0)
{
echo "</tr><tr>";
}
echo '<td><a>'.$aFiles[$i].'</a></td>';
}
echo '</tr>';
echo '</table>';
Files are already sorted a-z in $aFiles.
$aFiles = array('a','b','c','d', 'e', 'f', 'g');
$iColumnNumber = 2;
$iRowMaxNumber = ceil(count($aFiles) / $iColumnNumber);
$iTableRow = 0;
$iTableColumns = 1;
$aTableRows = array();
// make array with table cells
foreach ($aFiles as $sFile)
{
if ($iTableRow == $iRowMaxNumber)
{
$iTableRow = 0;
$iTableColumns++;
}
if (!isset($aTableRows[$iTableRow]))
{
$aTableRows[$iTableRow] = array();
}
$aTableRows[$iTableRow][] = '<td>' . $sFile . '</td>';
$iTableRow++;
}
// if there is odd number of elements
// we should add empty td elements
for ($iTableRow; $iTableRow < $iRowMaxNumber; $iTableRow++)
{
if (count($aTableRows[$iTableRow]) < $iTableColumns)
{
$aTableRows[$iTableRow][] ='<td>empty</td>';
}
}
// display table
echo '<table>';
foreach ($aTableRows as $aTableRow)
{
echo '<tr>';
echo implode('', $aTableRow);
echo '</tr>';
}
echo '</table>';
Simple Approach using HTML trick:
echo '<div class="tbl_group"><table>';
for($i=0;$i<=count($aFiles / 2);$i++)
{
echo '<tr>';
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo '</tr>';
}
echo '</tr>';
echo '</table></div>';
echo '<div class="tbl_group"><table>';
for($i=$aFiles / 2;$i<=count($aFiles);$i++)
{
echo '<tr>';
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo '</tr>';
}
echo '</tr>';
echo '</table></div>';
For the CSS:
.tbl_group {
float: left;
}
With this approach, 2 tables are built side-by-side. float: left CSS will auto adjust the position of the tables.
Assuming you know the number of rows you want, you don't need to resort anything, just add some logic in your loop that generates the <td>s :
$size = count($aFiles);
echo '<table><tr>';
for($i=0;$i<=ceil($size/2);$i++) {
if($i%2==0) {
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo "</tr><tr>";
} else {
if(isset($aFiles[$i+floor($size/2)])) {
echo '<td><a>'.$aFiles[$i+floor($size/2)].'</a></td>';
}
}
}
echo '</tr></table>';
Example bellow also works with associative array + arrays with odd elements.
Code for associative AND indexed array :
$aFiles = ['test'=>"A","B",'c'=>"C","D","E","F",'g'=>"G","H",'iii'=>"I"];
$aKeys = array_keys($aFiles);
$aRows = ceil(count($aFiles) / 2);
echo '<table>';
for($i=0; $i < $aRows; $i++) {
echo
'<tr>' .
' <td>' . $aFiles[$aKeys[$i]] . '</td>' .
' <td>' . (isset($aKeys[$i+$aRows]) ? $aFiles[$aKeys[$i+$aRows]] : 'empty') . '</td>' .
'</tr>';
}
echo '</table>';
Code for ONLY indexed array :
$aFiles = ["A","B","C","D","E","F","G","H","I"];
$aRows = ceil(count($aFiles) / 2);
echo "<table>";
for($i=0; $i < $aRows; $i++) {
echo
"<tr>" .
" <td>" . $aFiles[$i] . "</td>" .
" <td>" . (isset($aFiles[$i+$aRows]) ? $aFiles[$i+$aRows] : "empty") . "</td>" .
"</tr>";
}
echo "</table>";
Output for both examples is identical :
<table>
<tr>
<td>A</td>
<td>F</td>
</tr>
<tr>
<td>B</td>
<td>G</td>
</tr>
<tr>
<td>C</td>
<td>H</td>
</tr>
<tr>
<td>D</td>
<td>I</td>
</tr>
<tr>
<td>E</td>
<td>empty</td>
</tr>
</table>
Not optimized, but this should more or less work:
$aFiles = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N",);
$rows = 4;
$columns = floor((count($aFiles)/$rows))+1;
echo '<table>';
for ($i=0;$i<$rows;$i++) {
echo '<tr>';
for ($j=0;$j<$columns;$j++) {
echo '<td>';
if (isset($aFiles[$i+$j*$rows]))
echo $aFiles[$i+$j*$rows];
else
echo " ";
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
You can change the number of rows if you want.
Try this one. Assume that your $aFiles contains a-z alphabetically.
for ($i = 0; $i < count($aFiles/2); $i++){
echo '<tr><td><a>'.$aFiles[$i].'</a><td><td><a>'.$aFiles[$i+13].'</a><td></tr>'
}

4x? random image table php

so im trying to create an image gallery that displays a random image in each cell in a 4 column table and automaticly extends as more images are added to the folder and trying to set it so that every time it load it randomizes the images. right it just reads the images in order and on each row it starts over instead of continuing on though the images.
my code:
$file = null;
$fileList = glob("./upload/*.*");
//create table tag
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
//create tr tag
echo '<tr>';
# Print each file
echo "Files found:"; foreach($fileList as $file) {
echo " - ". $file;
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>'; }
echo '</tr>';
echo '</table>';
that was my first try and it just create a single row
my second attempt:
//create table
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
echo '<tr>';
$x = 1;
$y = 1;
// Display the results
do {
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
$x = $x +1;
$y = $y +1;
}
while ($x <= 3);
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
echo '</tr>';
echo '<tr>';
$x = $x - 4;
$y = $y +1;
}
while ($x = 5);
}
while ($y <= 20);
echo '</tr>';
echo '</table>';
this time it just starts over on every row and create way to many rows
Your foreach loop starts over each time you call it. You should abandon the do/while loops and use for loops instead. One for the rows and one for the columns:
$fileList = glob("./upload/*.*");
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>'
// Determine max rows:
$max_rows = ceil(count($fileList) / 4);
// Keep an index
$index = 0;
// First loop for rows
for ($row = 0; $row < $max_rows; $row++) {
// Start a new table row
echo '<tr>';
// Second loop for columns
for ($col = 0; $col < 4; $col++) {
if ($index < count($fileList)) {
echo '<td width="25%"><img src="' . $fileList[$index++] . '" width="100%" /></td>';
}
else {
echo '<td></td>';
}
}
echo '</tr>';
}
echo '</table>';

Categories