Jump to content
Larry Ullman's Book Forums

Chapter 19 - Example E-Commerce - view_print.php - Display the Image ( not displaying!)


Recommended Posts

Hi all,

I'm running the php files on my machine using the wampserver environment with mysql all scripts/pages are located in the 'www' folder, i.e root directory.

Inside the www directory I created a folder 'ch19' and its in this folder where all my files and folders for this chapter reside.

so to run the ecommerce example site,  the url is localhost/ch19/

ch19 directory contains the following php scripts: index.php, browse_prints.php, show_image.php and view_print.php

( I have not gone as far as the view_cart.php and add_cart.php just yet, will do once I've got this to work..)

ch19 has the following folders inside of it:

admin

includes

Both the mysqli_connect.php file and the ch19_uploads folder ( that contain the print images ) are under www but one level above ch19 folder.

Ive provided screenshots of what I'm seeing.  Notice that when I click on browse_prints.php and then click on a particular print,  the print image doesn't appear.  Instead, the default 'oops ' image loads in its place.  I'm noticing that this default image is taking the dimensions (wxh) of the image of the print thats meant to appear in its place.  Odd.

When I right click on this image and go to 'view image info' , sure enough the full image path to the print is shown.  I wonder why the print image(s) don't load.

Here is my code:

 

<?php # Script 19.6 - browse_prints.php
// This page displays the available prints (products).

// Set the page title and include the HTML header:
$page_title = 'Browse the Prints';
include ('includes/header.html');

require ('../mysqli_connect_ch19.php');
 
// Default query for this page:
$q = "SELECT artists.artist_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name, price, description, print_id FROM artists, prints WHERE artists.artist_id = prints.artist_id ORDER BY artists.last_name ASC, prints.print_name ASC";

// Are we looking at a particular artist?
if (isset($_GET['aid']) && filter_var($_GET['aid'], FILTER_VALIDATE_INT, array('min_range' => 1))  ) {
	// Overwrite the query:
	$q = "SELECT artists.artist_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name, price, description, print_id FROM artists, prints WHERE artists.artist_id=prints.artist_id AND prints.artist_id={$_GET['aid']} ORDER BY prints.print_name";
}

//	Create the table head
echo	'<section class="bp_tbl">
			<div class="bp_tbl_row bp_tbl_row_header">
				<div id="tc_artist_name" class="bp_tbl_cell cell_width_20">
					<h4>Artist</h4>
				</div>
				
				<div id="tc_print_name" class="bp_tbl_cell cell_width_20">
					<h4>Print Title</h4>
				</div>
				
				<div id="tc_description" class="bp_tbl_cell cell_width_50">
					<h4>Description</h4>
				</div>
				
				<div id="tc_price" class="bp_tbl_cell cell_width_10">
					</h4>Price</h4>
				</div>
			</div>';
			
//	Display every returned record:
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
	$i = 0;
	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	$i += 1;
	echo "\t<div id=\"tr_result_$i\" class=\"bp_tbl_row\">
				<div id=\"tc_artist_result_$i\" class=\"bp_tbl_cell cell_width_20\"><a href=\"browse_prints.php?aid={$row['artist_id']}\">{$row['artist']}</a></div>
				<div id =\"tc_print_name_result_$i\" class=\"bp_tbl_cell cell_width_20\"><a href=\"view_print.php?pid={$row['print_id']}\">{$row['print_name']}</a></div>
				<div id=\"tc_desc_result_$i\" class=\"bp_tbl_cell cell_width_50\">{$row['description']}</div>
				<div id=\"tc_price_result_$i\" class=\"bp_tbl_cell cell_width_10\">&pound{$row['price']}</div>
			</div>\n";
	}	//	End of while loop.
}	else {	//	no rows returned in result
	echo '<h4 class="error">Error! database query yielded no results!</h4>';
}
echo '</section>';
mysqli_close($dbc);
include('includes/footer.html');
?>


<?php # Script 19.7 - view_print.php
//	This page displays the details for a particular print

$row = FALSE;

//	Validate that a print ID has been passed to this page:
if (isset($_GET['pid']) && filter_var($_GET['pid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
	$pid = $_GET['pid'];
	
	require('../mysqli_connect_ch19.php');
	
	$q = "SELECT CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name, price, description, size, image_name FROM artists, prints WHERE artists.artist_id=prints.artist_id AND prints.print_id=$pid";
	
	$r = mysqli_query($dbc, $q);
	
	//	If a record was returned, retrieve the information, set the page title and include the HTML header:
	if (mysqli_num_rows($r) == 1) {
		$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
		$page_title = $row['print_name'];
		include('includes/header.html');
		
		//	Begin displaying the print information:
		echo "<div align=\"center\"><b>{$row['print_name']}</b> by {$row['artist']}<br />";
		echo (is_null($row['size'])) ? '(No size information available)' : $row['size'];
		echo "<br />&pound{$row['price']}<a href=\"add_cart.php?pid=$pid\">Add to Cart</a></div><br />";
		
		//	Display the image:
		if ($image = @getimagesize ("../ch19_uploads/$pid")) {
			echo "<div align=\"center\"><img src=\"show_image.php?image=$pid&name=" . urlencode($row['image_name']) . "\" $image[3] alt=\"{$row['print_name']}\" /></div>\n";
		}	else {
			echo "<div align=\"center\">No image available.</div>\n";
		}
		
		//	Display the description:
		echo '<p align="center">' . ((is_null($row['description'])) ? '(No description available)' : $row['description']) . '</p>';
	}	//	End of the mysqli_num_rows() IF.
	
}	//	End of $_GET['pid'] IF.

//	If a problem occured, display an error message:

if (!$row) {
	$page_title = 'Error';
	include('includes/header.html');
	echo '<div align="center">This page has been accessed in error!</div>';
}

include ('includes/footer.html');
?>			

 

 

sc4.jpg

Link to comment
Share on other sites

Sorry folks, I had to reply to my own post because I wanted to also share my 'show_image.php' script code with you all:

<?php # Script 19.8 - show_image.php
//	This page retrieves and shows an image.

//	Flag variables:
$image = FALSE;
$name = (!empty($_GET['name'])) ? $_GET['name'] : 'print image';

//	Check for an image value in the URL:
if (isset($_GET['image']) && filter_var($_GET['image'], FILTER_VALIDATE_INT, array('min-range' => 1 )) ) {
	$image = '../ch19_uploads' . $_GET['image'];
	//	Check that the image exists as a file on the server
	if (!file_exists($image) || (!is_file($image)) ) {
		$image = false;
	}
}	//	End of $_GET['image'] IF.

if (!$image) {
	$image = 'images/unavailable1.png';
	$name = 'unavailable.png';
}

//	Retrieve the image information
$info = getimagesize($image);
$fs = filesize($image);

//	Send the file

//	Prepare the browser to recieve the file based on its MIME type
header("Content-Type:{$info['mime']}\n");

//	The second line sets the name of the file being sent
header("Content-Disposition:inline; filename=\"$name\"\n");

//	This last header function indicates how much data is to be expected.
header("Content-Length: $fs\n");

//	The file data itself is sent using this function below:
readfile($image);

 

sc5.jpg

Link to comment
Share on other sites

Hi Larry, yes the complete path would be:

C:\wamp64\www\ch19_uploads

all uploaded images of prints end up in that folder.

As for the complete path from where my scripts are being run, that would be:  C:\wamp64\www\ch19, from here the scripts are being run.

so to 'get at' the images, in the scripts the path is usually defined as '..\ch19_uploads'

and obviously if i'm in a level 2 folder within ch19,  i.e admin, then the path to get at ch19_uploads or the mysqli_connect_ch19.php script would be ..\..\ch19_uploads.

 

Edited by reborn4tomorrow2509
Link to comment
Share on other sites

I did try and look at the html source code using 'view source' and noticed that the ampersand '&' in the url itself was highlighted in red, not sure if thats normal or not.

when viewing source code, the line which shows the url image source reads:

 

<img src="show_image.php?image=1&name=daydreaming_by_the_river_image_copy_low_res.jpg" width="900" height="747" alt="Day dreaming by the River" />

 

Going into the folder where I manually saved the images from  the website art2arts,  ( its just a folder containing the images i select for uploading into ch19_uploads) i cross checked this image file,  and sure enough its 900px (width) x 747px (height) and its called 'daydreaming_by_the_river_image_copy_low_res'.

the image we are seeing appearing on the view_print.php script instead, is the oops image, interestingly its taking on the same dimensions of the image its MEANT to be showing! 😕

 

Link to comment
Share on other sites

UPDATE: the reason that default image was appearing instead of the prints was because the original script in the book did not hard code any file extension for the file path and image.

The default 'oops' image appears if an image does not have a .jpg file extension set. I tried to set them manually by modifying the line that renames the function like so:

rename($temp, "../../ch19_uploads/$id".".jpg");	

Now what im seeing is no default image appears, just the message 'no image available' is being displayed on the screen for each print.

I think i'll take a break, have a coffee and return back to this.  I'll re-read the entire chapter if need be, may even read the earlier chapter on header files in case I've missed something.

 

Link to comment
Share on other sites

UPDATE #2: Really trying to understand whats going on here, as I'm struggling to get the image to display for the view_print.php page.

I even tried moving the ch19_uploads folder one level even deeper ( outside of the www directory itself and straight int the wamp64 directory, like I did with the folder 'uploads' in chapter 11 ) and though I can add images to this folder via add_print.php,  I can't for some reason get view_print.php to display anything!

The problem it seems to me, is that in the show_image.php script, where it checks that the image exists and is a file, $image is FALSE.  (  the file doesnt exist or its not a file ).

thats all i can think of as to why the image wont show up.  How to remedy this is a brain fryer for me heh

 

 

Link to comment
Share on other sites

Ah, okay. First, thanks for all the details. Second, I would advise against trying random things as you debug. It's a natural thing to do but tends to lead to more confusion. Here, for example, the upload script purposefully doesn't append the file extension, but that's totally okay because the show_image.php script doesn't use the file extension either. So that's actually correct and you definitely don't want to add the .jpg extension to the uploaded file (for starters, what if the upload was a PNG?). 

Normally if you're getting this result it's because your path to the file is incorrect. That your upload script uses ../../ch19_uploads/$id but show_image.php uses ../ch19_uploads makes me think that's the problem (assuming show_image.php and the upload script are in the same folder). Again, I definitely wouldn't move the folder, I'd just make sure you get the file path correct in the code. You can also try using an absolute path to the folder in your code. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...