-3

I am using this to create a mp3 list.

$mp3 = glob($directory . '*.mp3');
//print each file name
foreach($mp3 as $mp3)
{   
echo '
            <tr>
                <td class="one"><!--number goes here --></td>
                <td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
                <td class="two">'. $_GET['s'] .'</td>
                <td class="three">'. $_GET['a'] .'</td>
                <td class="two">'. $_GET['a'] .'</td>

            </tr>
';
}

I want to add number with every item of the list like, first one: 1, second one:2 etc. how can I do it?

Sparrow
  • 13
  • 6

2 Answers2

2
$listOfMp3 = glob($directory . '*.mp3');

foreach($listOfMp3 as $i => $mp3)
{
  echo $i; 
}
Linesofcode
  • 5,327
  • 13
  • 62
  • 116
0

This is not the best solution, but you can use a counter. Here is an example:

$mp3 = glob($directory . '*.mp3');
$c = 0;     //Set this value to 1 if you don't want to start with 0

foreach($mp3 as $mp3)
{   
echo '
        <tr>
            <td class="one">'.$c.'</td>
            <td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
            <td class="two">'. $_GET['s'] .'</td>
            <td class="three">'. $_GET['a'] .'</td>
            <td class="two">'. $_GET['a'] .'</td>
        </tr>';
        $c++;
}

So now $c will display the number of the file every time. Anyway, you can get the foreach key to do exactly the same.