Growing a list of links from a query

I have the following code which will extract the filename from the table and link to it. I want to do this, so I can later reference $ filesList and it will contain one block of html code with links to as many files as there are files.

I thought that adding to the previous variable would be the easiest way to do this, but it actually outputs pointless 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 don't need to have an array for what I want to do?

0


source to share


3 answers


You need to change this code to:

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

      



Does it help? You cannot team up with +

.

+1


source


One thing I can see right away is that you have $ filesList = $ filesList + ... Use period, not + -sign.

try it






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

      



+1


source


Have you tried something like this? (Unverified 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";    
}

      

-1


source







All Articles