Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    44
    Thanks
    9
    Thanked 0 Times in 0 Posts

    undefine index error in php

    Member Avatar digrev 1 Hour Ago
    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


    PHP Code:



     <?php
        $handle 
    opendir($directory) or die('Error');
        
    $files = array();
        while (
    $file readdir($handle)) {
            if (
    $file[0] == '.') {
                continue;
            }
            
    $files[] = $file;
        }
        
    sort($filesSORT_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>

  2. #2
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,133
    Thanks
    206
    Thanked 142 Times in 142 Posts
    just an idea

    I would print_r or var_dump the fileInfo array if that's the one you mean. Does it contain what you expect?
    0000

  3. #3
    New Coder
    Join Date
    Sep 2010
    Posts
    44
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thank you for the hint

  4. #4
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,436
    Thanks
    4
    Thanked 489 Times in 477 Posts
    My question would be why are you wasting processing time and memory making an extra copy of a perfectly good result set for NOTHING?

    Also seems a bit of a wonk that ... well, are you SURE those files exist in the database? Usually I'd think you'd be checking db to dir not dir to db... and since you're grabbing them all to an array why not glob?

    I'd do a rewrite, but your logic is so convoluted -- and I'm unaware of your db structure of filesystem contents -- that I'm unable to deciper what this is even supposed to do. GUESSING WILDLY I suspect what you're trying to do would be more efficient if coded along these lines.

    Code:
    <!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
    </head><body>
    
    <?php 
    	if ($files = glob($directory)) {
    		$stmt = $link->prepare('
    			SELECT filename
    			FROM download_manager
    			WHERE downloads = ?
    		'); 
    		$stmt->bind_param('s', $fileName);
    		echo '
    			<ul>';
    		foreach ($files as $fileName) {
    			$stmt->execute();
    			if ($data = $stmt->fetch()) echo '
    				<li>
    					<a href="download.php?file=', urlencode($data['downloads']), '">
    						', htmlspecialchars($data['downloads']), '
    					</a>
    					<span>', htmlspecialchars($fileName), '</span>
    				</li>';
    			else echo '
    				<li>Error, no match in DB for ', $fileName, '</li>';
    		}
    		echo '
    			</ul>';
    	}
    ?>
    
    </body></html>
    Note in my own code I usually don't open/close PHP willy nilly like that, but for simplicity sake I did it here. Also untested code, may be typo's I'm more of a PDO guy than mysqli.

    Leverages the POEM technique so you're not grabbing an entire data set you might not even actually be using. AGAIN though this is a case where doing this in PDO would be superior since, well... fetchColumn.

    Code:
    	if ($files = glob($directory)) {
    		$stmt = $db->prepare('
    			SELECT filename
    			FROM download_manager
    			WHERE downloads = ?
    		'); 
    		$stmt->bindParam(1, $fileName);
    		echo '
    			<ul>';
    		foreach ($files as $fileName) {
    			$stmt->execute();
    			if ($downloads = $stmt->fetchColumn()) echo '
    				<li>
    					<a href="download.php?file=', urlencode($downloads), '">
    						', htmlspecialchars($downloads), '
    					</a>
    					<span>', htmlspecialchars($fileName), '</span>
    				</li>';
    			else echo '
    				<li>Error, no match in DB for ', $fileName, '</li>';
    		}
    		echo '
    			</ul>';
    	}
    I just find PDO so much cleaner and simpler. Really wonder why anyone would use mysqli by choice in the first place.

    Hard to say if this is more or less efficient, it's less code, and doesn't chew on memory as hard, but DOES make more database requests. The size of the data sets would likely be a determining factor in how well this works. As a rule of thumb I tend to avoid large data set results even if it means more talking to the DB for the simple fact that with LOTS of user requests you're more likely to choke out RAM than you are connection limits on MOST web hosts.
    Last edited by deathshadow; Oct 6th, 2018 at 07:36 AM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. #5
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,133
    Thanks
    206
    Thanked 142 Times in 142 Posts
    Hi DS

    Couple of questions

    do you need

    sort($files, SORT_STRING);

    since glob pathnames are sorted alphabetically unless flag is set. Isn't SORT_STRING doing the same?

    and also if using glob does he need

    if ($fileName[0] == '.') continue;

    because glob default is to ignore hidden files. Also is $fileName even an array???

    Never really used glob so learning here.

    LT
    Last edited by low tech; Oct 6th, 2018 at 04:02 AM.
    0000

  6. #6
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,436
    Thanks
    4
    Thanked 489 Times in 477 Posts
    Point 1 about the glob sort? good point. So axe that. Hmm, I have it in my own codebases, no... I don't use sort, I have natcasesort -- because I don't want upper case sorted before lower. I swear the stuff I'm forgetting now.

    Point 2 regarding it ignoring hidden files, are . and .. actually treated as hidden? Never really thought about that. Hmm... Windows, yup. Linux... Yup... haiku... no. Hmm. Well, it's probably safe to omit that too, who the hell is going to host a PHP server on Haiku / BeOS?

    Why would the current and parent directory aliases be 'hidden'?!? That seems strange... eh, sometimes it's better not to ask these questions.

    Especially if it means less code, so yeah, kill off the sort and the check for a leading period. Good catch.

    Edited my post to reflect the suggested changes.
    Last edited by deathshadow; Oct 6th, 2018 at 07:36 AM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •