www.webdeveloper.com
Results 1 to 12 of 12

Thread: Problem displaying my images & info

  1. #1
    Join Date
    Dec 2006
    Posts
    143

    Problem displaying my images & info

    Hi,

    i've built an application whereby i would upload an image onto my server allong with its info stored on my db which works fine. I am now trying to display my images and info together in my homepage.html.php file but when try to do this nothing is displayed.

    What i don't understand is in the book, 'Build Your Own Database Driven Website using PHP & MYSQL 4th ed' there are many examples in this book e.g chapter 7p223/4 which uses a while loop to get the results from the db and and uses the include 'jokes.html.php' code to display their results, it seems to work there but when i do it myself nothing appears, why? What am i missing?

    PHP Code:
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/shanghai_db.inc.php';
           
    $result mysqli_query($link"SELECT * FROM image_detail");
           
     
            
       if(!
    $result )
        {
            
    $error 'Error fetching image data from database';
            include 
    'error.html.php';
            exit();    
        }
              
        while (
    $row mysqli_fetch_array($result))
        {
            
    $images[] = array('Filename' => $row['Filename'], 'Description' => $row['Description'], 'Category' => $row['Category']);
        }
     
        include 
    'homepage.html.php';
        exit(); 
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title> Image Upload Success</title>
            <meta http-equiv="content-type"
                    content="text/html; charset=utf-8"/>
        </head>
        <body>
            <p>
                <?php if( isset($images) ): ?>
                <table>
                <?php foreach ($images as $image): ?>
                
                <tr valign="top">
                    <td><? php echo '<img src="./images/'.$image['photo'].'" >';  ?> </td>
                <?php echo "<b>Description:</b> ".$image['Description'] . " <br>"; ?>
                </tr>
                
                <?php endforeach; ?>
                </table>
                <?php endif ;?
            </p>
        </body>
    </html>
    
    plese help if u can. thankz

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    I believe this...
    PHP Code:
    <td><? php echo '<img src="./images/'.$image['photo'].'" >';  ?> </td>
    ...should be this...
    PHP Code:
    <td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td>
    ?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2006
    Posts
    143
    I changed it to Filename but again, no change, still blank page

  4. #4
    Join Date
    Dec 2006
    Posts
    143
    although i dont understand why you would change it to Filename???

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    Quote Originally Posted by nvidia View Post
    although i dont understand why you would change it to Filename???
    Because that's what you called it here:
    PHP Code:
        while ($row mysqli_fetch_array($result))
        {
            
    $images[] = array('Filename' => $row['Filename'], 'Description' => $row['Description'], 'Category' => $row['Category']);
        } 
    (Assuming I'm correctly following the program flow.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    It might help to do some debugging to find out what is actually in the array after you populate it:
    PHP Code:
    <pre><?php print_r($images); ?></pre>
    Based on the data (or lack of data) displayed, it may point you toward where to look for the problem.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Feb 2008
    Posts
    70
    what nogdog said is correct it should be $image[Filename], assuming you have stored the image filename correctly in the database then the only other issue that I can see is that the path to the image is incorrect.
    To test this add an image in your code with a known filename hardcoded to see if the image actually appears.

  8. #8
    Join Date
    Jun 2005
    Location
    Fullerton, CA USA
    Posts
    61
    It would probably help to show us what the browser received when you executed that page (i.e., "View Source" or "View Page Source").

    I you're getting nothing in the body by the paragraph tags, then $images isn't getting set. If you're getting table tags but no valid url, then you need to fix the stored data.

    Also, you're echoing the "description" outside of any table cell. You closed the cell after the image and didn't open a new one for the description.

  9. #9
    Join Date
    Dec 2006
    Posts
    143
    NogDog, i added your code line to my .html.php file and it outputted
    Code:
    Notice:  Undefined variable: images in C:\wamp\www\Shanghai2010\homepage.html.php on line 11
    The filename is safely stored in my db which i can easily see, i harded coded one of my images in my 'images' folder,


    Code:
    <td><img src="./images/IMG_0214.JPG" alt="Monkey" /> </td>  
    ....
    but nothing was shown up, i added the dot '.' before the slash because i read that it tells html to look in the current directory but sadly no images were shown .

    prouton, this is what i received when viewing the source page between my paragraph tags:

    Code:
    <pre><br />
    <b>Notice</b>:  Undefined variable: images in <b>C:\wamp\www\Shanghai2010\homepage.html.php</b> on line <b>11</b><br />
    </pre>

  10. #10
    Join Date
    Dec 2006
    Posts
    143
    UPDATE: I believe the way i was trying to display my images was slightly incorrect. After i enter the information for each image file and upload an image and submit, i would then go to my browser and manually type the following address: http://localhost/shanghai2010/homepage.html.php which would show nothing. BUT when i submit the form with the information and the file and add the hard coded img tag it would actaully display that image.


    Now that it would display that image, i immediately changed the code to:
    Code:
    	<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td>
    Then i started again by adding an image and it's relavant information, hitting the submit button but this time it is now showing the description:

    what is shown on browser:
    Code:
    Array ( [name] => IMG_0242.JPG [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\php1C86.tmp [error] => 0 [size] => 4463338 ) 
    The file and information has been stored successfully 
    
    ?> 
    Array ( [photo] => Array ( [name] => IMG_0242.JPG [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\php1C86.tmp [error] => 0 [size] => 4463338 ) ) '; ?>  Description: Dragon head statue 
     
    '; ?>  Description: Pond 
     
    '; ?>  Description: Crowded square 
     
    '; ?>  Description: Water fountain 
     
    '; ?>  Description: Market area 
     
    '; ?>  Description: Get me out of here! 
     
    '; ?>  Description: Snake 
     
    '; ?>  Description: pea****
    When viewing the source:

    Code:
    <p>
    			Array
    (
        [photo] => Array
            (
                [name] => IMG_0242.JPG
                [type] => image/pjpeg
                [tmp_name] => C:\wamp\tmp\php1C86.tmp
                [error] => 0
                [size] => 4463338
            )
    
    )
    			
    						<table>
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Dragon head statue <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Pond <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Crowded square <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Water fountain <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Market area <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Get me out of here! <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> Snake <br> </td>
    			</tr>
    			
    						
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <b>Description:</b> pea**** <br> </td>
    			</tr>
    			
    						</table>
    I added print_r($_FILES); to see what was in my fils array.

    Updatd html code:
    Code:
    <p>
    			<?php print_r($_FILES); ?>
    			
    			<?php if( isset($images) ): ?>
    			<table>
    			<?php foreach ($images as $image): ?>
    			
    			<tr valign="top">
    				<td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td> 
    				 
    			<td> <?php echo "<b>Description:</b> ".$image['Description'] . " <br>"; ?> </td>
    			</tr>
    			
    			<?php endforeach; ?>
    			</table>
    			<?php endif ;?>
    		</p>
    my php code is unchanged. Since when i add the
    Code:
     <td><? php echo '<img src="./images/'.$image['Filename'].'" >';  ?> </td>
    code i'm guessing there is something wrong with the way it's written perhaps?? because if hard code every file image it would probably display all them but doing it this way it does not which i'm not sure why??

    Sorry for any confusion if i have caused.

  11. #11
    Join Date
    Jun 2005
    Location
    Fullerton, CA USA
    Posts
    61
    <td><? php echo '<img src="./images/'.$image['Filename'].'" >'; ?> </td>
    Get rid of the space between the "?" and ""php".

  12. #12
    Join Date
    Dec 2006
    Posts
    143
    aah dang, the images and description show up which i'm pleased about including the description, thanks guys, i really appreciate u'r help with my thread. thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles