tags:

views:

57

answers:

3

I have the following code, which will retrieve a filename from a table and make a link to it. What I want to do, is have it so I can refer to $filesList later on, and it will contain a single block of html code with links to as many files as there are files.

I thought adding to the previous variable would be the easiest way to do this, but it actually outputs nonsense code: 0test.sh">test.sh

if ($getFiles = $con->prepare($filesQuery)) {
    $getFiles->bind_param("s", $pk);
    $getFiles->execute();
    $getFiles->bind_result($FILENAME);
    $files = array();

    while ($getFiles->fetch()) {
         $filename = array(
         'FILENAME' => $FILENAME,
          );
           $files[] = $filename;
        }
}
$filesList = '';
foreach ($files as $filenames)
    {

    $filesList = $filesList + '<p><a href="./files/'. $filenames['FILENAME'] .'">'. $filenames['FILENAME'] .'</a>' . "\n"; 
  };

Sureley I do not need to have an array for what i want to do?

+2  A: 

You need to change that code to:

$filesList = '';
foreach ($files as $filenames)
{
    $filesList .= '<p><a href="./files/'. $filenames['FILENAME'] .'">'. $filenames['FILENAME'] ."</a></p>\n"; 
};

Does that help? You cannot concatenate with +.

Petrunov
+2  A: 

One thing that I immediately spot is that you have $filesList = $filesList + ... Use a dot and not a + -sign.

Try this

$filesList = $filesList . "<p><a href=\"./files/{$filenames['FILENAME']}\">{$filenames['FILENAME']}</a>";

Kim L
A: 

Have you tried something like this? (Untested code, as I am not at home)

if ($getFiles = $con->prepare($filesQuery)) {
$getFiles->bind_param("s", $pk);
$getFiles->execute();
$getFiles->bind_result($FILENAME);
$files = array();

while ($getFiles->fetch()) {
   $filesList = $filesList + '<p><a href="./files/'. $FILENAME .'">'.   $FILENAME .'</a>' . "\n";    
}
Patrick Cornelissen