exclude header from row number

My table row index starts at 2. Should start at 1.

Also the .php file should not be included in the list.

http://flamencopeko.net/lyrics/index.php

[php]

<?php // open directory $myDirectory = opendir("."); // get entries while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close directory closedir($myDirectory); // count elements in array $indexCount = count($dirArray); $indexReal = $indexCount-2; // show number of songs print("
\n"); Print ("Songs in table: $indexReal.
\n"); print("

\n"); // sort files sort($dirArray); // print table print("\n"); print("\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print(""); print(""); print(""); print(""); print("\n"); } } print("
# title type size
$index"); $content = file($dirArray[$index]); echo ("$content[0]"); print(""); print(filetype($dirArray[$index])); print(""); print(filesize($dirArray[$index])); print("
\n"); ?>

[/php]

<?php // open directory $myDirectory = opendir("."); // get entries while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close directory closedir($myDirectory); // count elements in array $indexCount = count($dirArray); $indexReal = $indexCount-2; // show number of songs print("
\n"); Print ("Songs in table: $indexReal.
\n"); print("

\n"); // sort files sort($dirArray); // print table print("\n"); print("\n"); [b]array_shift($dirArray); array_shift($dirArray); [/b] // loop through the array of files and print them all [b]foreach[/b]($dirArray as $index => $value){ [b]$ind = $index + 1; // This is to fix the correct list of indices...[/b] if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print(""); print(""); print(""); print(""); print("\n"); } } print("
# title type size
$ind"); $content = file($dirArray[$index]); echo ("$content[0]"); print(""); print(filetype($dirArray[$index])); print(""); print(filesize($dirArray[$index])); print("
\n"); ?>

Hello friend,

The problem is that into folder has directories “.” and “…”

To fix this problem, is very simple.

array_shift(); << this function will remove the first indice of array…

but do you have 2 indices, so… utilize two turn.

I hope that it can help you…
:smiley:

foreach($dirArray as $index => $value){
$ind = $index + 1; // This is to fix the correct list of indices…
if (substr("$dirArray[$index]", 0, 1) != “.”){ // don’t list hidden files
print("

$ind");
print("");
$content = file($dirArray[$index]);
echo ("<a href="$dirArray[$index]">$content[0]");
print("");
print("");
print(filetype($dirArray[$index]));
print("");
print("");
print(filesize($dirArray[$index]));
print("");
print("\n");
}
}

Thanks so much Thiago! That worked like a charm. And I even to some degree understood a bit of it!

I tried to shorten it to:

[php]

// remove first indice of array
array_shift($dirArray);

// loop through the array of files and print them all
foreach($dirArray as $index => $value){
$ind = $index; // fix the correct list of indices

[/php]

That works too. Any reason not to do this? It seems like you did minus 2, then plus 1.

My iq is on the lower side I’m affraid. I’ll read up on the shift and foreach stuff.

Lyrics page now integrated into site. I’m so happy. Thanks everyone for everything.

Anyone know how to hide that index.php file still hangin’ 'round in the table?

Ohh, i’m sorry I forgot… To remove .php, try…

foreach($dirArray as $index => $value){
$ind = $index + 1;

   // If this index has ".php", so let's to next, and doesn't print. 
[b]if(strrpos($dirArray[$index], ".php") !== false){
  continue;
}[/b]

if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
	print("<tr><td>$ind</td>");
	print("<td>");
	$content = file($dirArray[$index]);
	echo ("<a href=\"$dirArray[$index]\">$content[0]</a>"); 
	print("</td>");
	print("<td>");
	print(filetype($dirArray[$index]));
	print("</td>");
	print("<td>");
	print(filesize($dirArray[$index]));
	print("</td>");		
	print("</tr>\n");
}

}

Seems it’s still included in the array? So it’s number (6) is missing in the table.

ohh yeah, it’s wrong… but, no problem ;D!!

$ind = 1;
// loop through the array of files and print them all
foreach($dirArray as $index => $value){

[b]if(strrpos($dirArray[$index], ".php") !== false){
  continue;
}[/b]

if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
	print("<tr><td>[b]$ind[/b]</td>");
	print("<td>");
	$content = file($dirArray[$index]);
	echo ("<a href=\"$dirArray[$index]\">$content[0]</a>"); 
	print("</td>");
	print("<td>");
	print(filetype($dirArray[$index]));
	print("</td>");
	print("<td>");
	print(filesize($dirArray[$index]));
	print("</td>");		
	print("</tr>\n");
[b]$ind++;[/b]	
}

}

I didn’t understand that. Nothing changed. Still jumps from 5 to 7.

sure? post here the code…

[php]

<?php // open directory $myDirectory = opendir("."); // get entries while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close directory closedir($myDirectory); // count elements in array $indexCount = count($dirArray); $indexReal = $indexCount-2; // show number of songs print("
\n"); Print ("Songs in table: $indexReal.
\n"); print("

\n"); // sort files sort($dirArray); // print table print("\n"); print("\n"); // remove first indice of array array_shift($dirArray); array_shift($dirArray); $ind = 1; // loop through the array of files and print them all foreach($dirArray as $index => $value){ $ind = $index + 1; // fix the correct list of indices // If this index has ".php", so let's to next, and doesn't print. if(strrpos($dirArray[$index], ".php") !== false){ continue; } if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print(""); print(""); print(""); print(""); print("\n"); $ind++; } } print("
# title type size
$ind"); $content = file($dirArray[$index]); echo ("$content[0]"); print(""); print(filetype($dirArray[$index])); print(""); print(filesize($dirArray[$index])); print("
\n"); ?>

[/php]

Alternate solution could be to keep the index.php file outside the lyrics folder and name it lyrics.php. That would require quite some code change I assume.

foreach($dirArray as $index => $value){
$ind = $index + 1; // fix the correct list of indices

// If this index has “.php”, so let’s to next, and doesn’t print.
if(strrpos($dirArray[$index], “.php”) !== false){
continue;
}

Ah! Works! :slight_smile:

I substract none .txt files “manually” here:

[php]

// count elements in array
$indexCount = count($dirArray);
$indexReal = $indexCount-3;

[/php]

But that’s ok.

Superb!

good luck friend.

Thanks a lot. We got me started. Have so much stuff I want to add to the site. Will keep building. PHP it is.

Sponsor our Newsletter | Privacy Policy | Terms of Service