1 we'll query alloc when we get to that record. the theory is that most items will have quantity = 1 so we'll make the least number of trips. */ $query = "SELECT i.itemid, description, price, source, c.category, url, image_filename, " . "ub.fullname AS bfullname, ub.userid AS boughtid, " . "ur.fullname AS rfullname, ur.userid AS reservedid, " . "rendered, i.comment, i.quantity " . "FROM {$OPT["table_prefix"]}items i " . "LEFT OUTER JOIN {$OPT["table_prefix"]}categories c ON c.categoryid = i.category " . "LEFT OUTER JOIN {$OPT["table_prefix"]}ranks r ON r.ranking = i.ranking " . "LEFT OUTER JOIN {$OPT["table_prefix"]}allocs a ON a.itemid = i.itemid AND i.quantity = 1 " . // only join allocs for single-quantity items. "LEFT OUTER JOIN {$OPT["table_prefix"]}users ub ON ub.userid = a.userid AND a.bought = 1 " . "LEFT OUTER JOIN {$OPT["table_prefix"]}users ur ON ur.userid = a.userid AND a.bought = 0 " . "WHERE i.userid = $shopfor " . "ORDER BY $sortby"; $rs = mysql_query($query) or die("Could not query: " . mysql_error()); $shoprows = array(); while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) { $row['price'] = formatPrice($row['price']); if ($row['quantity'] > 1) { // check the allocs table to see what has been allocated. $avail = $row['quantity']; $query = "SELECT a.quantity, a.bought, a.userid, " . "ub.fullname AS bfullname, ub.userid AS boughtid, " . "ur.fullname AS rfullname, ur.userid AS reservedid " . "FROM {$OPT["table_prefix"]}allocs a " . "LEFT OUTER JOIN {$OPT["table_prefix"]}users ub ON ub.userid = a.userid AND a.bought = 1 " . "LEFT OUTER JOIN {$OPT["table_prefix"]}users ur ON ur.userid = a.userid AND a.bought = 0 " . "WHERE a.itemid = " . $row['itemid'] . " " . "ORDER BY a.bought, a.quantity"; $allocs = mysql_query($query) or die("Could not query: " . mysql_error()); $ibought = 0; $ireserved = 0; $itemallocs = array(); while ($allocrow = mysql_fetch_array($allocs, MYSQL_ASSOC)) { if ($allocrow['bfullname'] != '') { if ($allocrow['boughtid'] == $userid) { $ibought += $allocrow['quantity']; $itemallocs[] = ($allocrow['quantity'] . " bought by you."); } else { if (!$OPT["anonymous_purchasing"]) { $itemallocs[] = ($allocrow['quantity'] . " bought by " . $allocrow['bfullname'] . "."); } else { $itemallocs[] = ($allocrow['quantity'] . " bought."); } } } else { if ($allocrow['reservedid'] == $userid) { $ireserved += $allocrow['quantity']; $itemallocs[] = ($allocrow['quantity'] . " reserved by you."); } else { if (!$OPT["anonymous_purchasing"]) { $itemallocs[] = ($allocrow['quantity'] . " reserved by " . $allocrow['rfullname'] . "."); } else { $itemallocs[] = ($allocrow['quanitity'] . " reserved."); } } } $avail -= $allocrow['quantity']; } mysql_free_result($allocs); $row['allocs'] = $itemallocs; $row['avail'] = $avail; $row['ibought'] = $ibought; $row['ireserved'] = $ireserved; } $shoprows[] = $row; } mysql_free_result($rs); /* okay, I *would* retrieve the shoppee's fullname from the items recordset, except that I wouldn't get it if he had no items, so I *could* LEFT OUTER JOIN, but then it would complicate the iteration logic, so let's just hit the DB again. */ $query = "SELECT fullname FROM {$OPT["table_prefix"]}users WHERE userid = $shopfor"; $urs = mysql_query($query) or die("Could not query: " . mysql_error()); $ufullname = mysql_fetch_array($urs, MYSQL_ASSOC); $ufullname = $ufullname["fullname"]; mysql_free_result($urs); define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-3.1.12/libs/'); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); $smarty->assign('ufullname', $ufullname); $smarty->assign('shopfor', $shopfor); $smarty->assign('shoprows', $shoprows); $smarty->assign('userid', $userid); $smarty->assign('opt', $OPT); $smarty->display('shop.tpl'); ?>