Making webpages using PHP/MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

snappydesigns
Forum Commoner
Posts: 28
Joined: Tue Feb 13, 2007 11:37 am

Post by snappydesigns »

Hi Everah-

I think we were posting at roughly the same time, so you may have missed my previous post about figuring out the problem with my thumbnail links. Now I'm back to the problem of displaying the image on the product page.

Again here's the code I'm using:

Code: Select all

<?php 
include('header.html'); 

if (isset($_GET['pid'])) 
{ 
    $id = $_GET['pid']; 
    include_once 'connect_stflats.php'; 
    $query = 'SELECT item_id, product_name, product_id, image_name FROM donovandesigs WHERE item_id = ' . $id; 

    $result = mysql_query ($query) or die('Could not execute the query: ' . mysql_error()); 
    $row = mysql_fetch_array ($result, MYSQL_ASSOC); 

    if ($image = getimagesize($row['image_name'])) 
    { 
        echo '<div align="center"><img src="' . $row['image_name'] . '" />'; 
    } 
    else 
    { 
        echo '<div align="center">No Image Available.</div>'; 
    } 
} 

include('footer.html'); 
?>
I'm using the code you posted earlier now, but it's still not working. I know we defined the $id as item_id, but how does the code know that the web address will reference the item_id? Or this what we're doing at the beginnig of the code with: if (isset($_GET['pid']))...?

My head is swirling with all this code, but I have no idea where to go from here.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here are some comments to help you along...

Code: Select all

<?php
// Include the header.html file - This should really be include_once
include_once 'header.html';

// Check to see of the query string var pid is set
if (isset($_GET['pid']))
{
    // it is, so assign the $id var the value of the query string var 'pid'
    $id = $_GET['pid'];
    
    // Include a utility fiel
    include_once 'connect_stflats.php';
    
    // Make a SQL query instruction that pulls columns item_id, 
    // product_name, product_id and image_name from the 
    // donovadesigs table where the item_id value is the same 
    // as the query string pi value
    $query = 'SELECT item_id, product_name, product_id, image_name FROM donovandesigs WHERE item_id = ' . $id;

    // Execute the query instruction, halt process on query failure
    $result = mysql_query($query) or die('Could not execute the query: ' . mysql_error());
    
    // Assign a one-dimensional, associateive array of the query 
    // result data to $row
    $row = mysql_fetch_array($result);

	// if the getimagesize() function returns non-false...
    if ($image = getimagesize($row['image_name']))
    {
        // Echo out the image path in a div (remembering to close the div tag 
        echo '<div align="center"><img src="' . $row['image_name'] . '" /></div>';
    }
    else
    {
        // Otherwise just say no image is available
        echo '<div align="center">No Image Available.</div>';
    }
}

// Include the footer.html file, which should also be an include_once
include_once 'footer.html';
?>
snappydesigns
Forum Commoner
Posts: 28
Joined: Tue Feb 13, 2007 11:37 am

Post by snappydesigns »

Thank you for that, Everah. Things are making more sense to me now. The code still isn't working though. I'm getting a bit frustrated because I have no other solutions.

My basic goal is to use the item_id to make the url (for ex. http://twinpapers.com/view_donovanflat2.php?id=2) where the item_id is just the primary key automatically incremented. The webpage should then display the image (called up from the image path that is listed in the MySQL table called image_name) that corresponds with the item_id. I have almost 200 records in this particular table, so I want to have a webpage for each record that will display its image.

Obviously, the code I have is not doing this for me. Maybe it would if I modified it somehow. To be honest, I have no idea where to go from here. Any help here would be very much appreciated :D.

Jen
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you PM me your code for both the master and the detail. Let me see what is happening.
snappydesigns
Forum Commoner
Posts: 28
Joined: Tue Feb 13, 2007 11:37 am

Post by snappydesigns »

Gladly...

thanks!
snappydesigns
Forum Commoner
Posts: 28
Joined: Tue Feb 13, 2007 11:37 am

tried PM'ing you, but message won't send

Post by snappydesigns »

Hi Everah-

Ok, I did as you said with the switch in code. I also found an error (on my part where I had a column name that didn't exist...removed it). Now when I click on a thumbnail, I get the header/footer with no space in between with this above the header:
------->>
Warning: getimagesize(/images/donovan/stationery/flats/103.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/.mavin/mirka/twinpapers.com/test_view.php on line 31

No Image Available.
------->>

Here's line 31:

Code: Select all

if ($image = getimagesize($row['image_name']))

Now, I don't actually have the image uploaded to my directory just the file name...for example, /images/donovan/stationery/flats/103.jpg which does exist in the directory. Do I need to make a column with the image size or something because it look like it's checking the fileaname for the image size? Can we just get rid of that line all together or is it necessary when uploading a picture from the server? I'm stumped.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think you may be able to get away with file_exists() instead of getimagesize(). All it appears the call to getimagesize() is doing is checking if the image is there anyway.
Post Reply