Click here to Skip to main content
14,330,218 members
Rate this:
Please Sign up or sign in to vote.
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 18: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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100