Jump to content
Larry Ullman's Book Forums

Indicating Availability


Recommended Posts

I created my site with PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide and then used the effortless e-commerce book to add extras shown in the book. One of these is to indicate availability. But.. It only displays the last statement from the product_functions.inc.php

 

Below is my code that links to this:

<?php
include ('includes/product_functions.inc.php');
$row = FALSE;
if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) {
$pid = (int) $_GET['pid'];
require_once ('../mysqli_connect.php');
$q="SELECT product_name, price, print_id, product_category, description, image_name, product_id FROM product WHERE product_id= $pid";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0) {
 $row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
) {
  echo '<table width="90%" border="0" cellspacing="2" cellpadding="2">'."\r\n";
//Here is the link to the stock status as created in the product_functions. Mine is in quantity_stock in database as opposed to stock in the book.
 echo '<h3>'.get_stock_status($row['quantity_stock']).' </h3>';
echo '</table>';
 }
 }
if (!$row) {
$page_title = 'Error';
echo '<div align="center">This page has been accessed in error!</div>';
}
mysqli_close($dbc);
?>

 

 

 

- PHP version 5.3.5

- MYSQLI 5.0.7

Link to comment
Share on other sites

Have you confirmed what the value of $row['quantity_stock'] is? And, if so, what is it?

 

And for those that don't have the book, or don't have it in front of them, could you confirm what the "last statement" is that's always being executed?

Link to comment
Share on other sites

The product_functions.inc.php where the last statement is:

 

<?php
function get_stock_status($quantity_stock) {

if ($quantity_stock > 5) { // Plenty!
 return 'In Stock';
} elseif ($quantity_stock > 0) { // Low!
 return 'Low Stock';
} else { // Out!
 return 'Currently Out of Stock';
}

} // End of get_stock_status() function.

 

The value of $row['quantity_stock'] is 14 so should show plenty!

Link to comment
Share on other sites

Might possibly be a String-related problem. Try to typecast it to int.

 

(int) $quantity_stock

 

Don't really know if this is your error, but if it returns 14, I agree with you that is weird.

 

Wish PHP can make optional types... Would be great to be able to use Strings, Integers, Doubles, DateTimes, MySQLI or other classes as types! Crossing fingers.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...