Click here to Skip to main content
14,766,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table called download_manager .It has two columns filename and downloads .What I want to do is to get file name and its download count and display them inside ul element.
I am able to get file names but I cant display the download count I'm getting undefined index error.This is my code

What I have tried:

<?php 
    $handle = opendir($directory) or die('Error'); 
    $files = array(); 
    while ($file = readdir($handle)) { 
        if ($file[0] == '.') { 
            continue; 
        } 
        $files[] = $file; 
    } 
    sort($files, SORT_STRING); 
    $query = 'select * from download_manager'; 
    $data = mysqli_query($link, $query); 
    $fileInfo[] = array(); 
    if (mysqli_num_rows($data)) { 
        while ($singleFile = mysqli_fetch_array($data)) { 
            $fileInfo[$singleFile['filename']] = $singleFile['downloads']; 
        } 
    } 
    ?> <!DOCTYPE html> <html> <head> </head> <body> <ul> <?php 
    foreach ($files as $key => $value) { 
         echo '<li><a href="download.php?file='.urlencode($value).'">'.$value.'</a> <span>'.$fileInfo[$value].'</span> '; 
    } 
    ?> </ul> </body> </html>
Posted
Updated 4-Oct-18 19:08pm
Comments
Patrice T 4-Oct-18 17:03pm
   
Show exact error message and line number.
Member 3722539 5-Oct-18 6:42am
   
thank you

1 solution

By looking at the posted code, Very likely the filename in the directory and download_manager table are mismatch.

Example:
let say this is download_manager table
Filename 	Downloads
abc	             10		
xyz.txt	         20		


let say these are the files in the directory
Directory
abc.txt
xyz.doc


based on the code

$fileInfo["abc.txt"] --> Undefined index error
$fileInfo["xyz.doc"] --> Undefined index error
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900