sigh.. it's Phrozt... I don't know why no one can spell it.. but anyway.
here's how I build my queries:
mysql_select_db($database_conn_seno, $conn_seno);
if (isset($_GET['brand_id'])) {
$colname_rs_brand = (get_magic_quotes_gpc()) ? $_GET['brand_id'] : addslashes($_GET['brand_id']);
$query_rs_tuxedos = sprintf("SELECT tuxStyles.style, brands.brand, tuxedos.id, tuxedos.name, tuxedos.style_id, tuxedos.brand_id, tuxedos.photoAlt, tuxedos.photoURL, tuxedos.visible FROM tuxedos LEFT JOIN tuxStyles ON tuxedos.style_id = tuxStyles.id LEFT JOIN brands ON tuxedos.brand_id = brands.id WHERE tuxedos.brand_id = %s AND tuxedos.visible = 1 ORDER BY tuxStyles.style", $colname_rs_brand);
} else if (isset($_GET['color_id'])){
$colname_rs_color = (get_magic_quotes_gpc()) ? $_GET['color_id'] : addslashes($_GET['color_id']);
$query_rs_tuxedos = sprintf("SELECT tuxStyles.style, brands.brand, tuxedos.id, tuxedos.name, tuxedos.style_id, tuxedos.color_id, tuxedos.photoAlt, tuxedos.photoURL, tuxedos.visible FROM tuxedos LEFT JOIN tuxStyles ON tuxedos.style_id = tuxStyles.id LEFT JOIN brands ON tuxedos.brand_id = brands.id WHERE tuxedos.color_id = %s AND tuxedos.visible = 1 ORDER BY brands.brand ASC, tuxStyles.style", $colname_rs_color);
} else {
$query_rs_tuxedos = "SELECT tuxStyles.style, brands.brand, tuxedos.id, tuxedos.name, tuxedos.style_id, tuxedos.photoAlt, tuxedos.photoURL, tuxedos.visible FROM tuxedos LEFT JOIN tuxStyles ON tuxedos.style_id = tuxStyles.id LEFT JOIN brands ON tuxedos.brand_id = brands.id WHERE tuxedos.visible = true ORDER BY brands.brand ASC, tuxStyles.style";
}
$rs_tuxedos = mysql_query($query_rs_tuxedos, $conn_seno) or die(mysql_error());
$row_rs_tuxedos = mysql_fetch_assoc($rs_tuxedos);
$numRows_rs_tuxedos = mysql_num_rows($rs_tuxedos);
The reason i do it as such is because there are three different ways to select tuxes; show them all, show tuxes of a certain brand, show tuxes of a certain color.
There can, of course, be multiple pages, which have to factor in the sort type as well... if you're interested, here's the code for that:
<?php
if ($numRows_rs_tuxedos > 9) {
$maxTuxes = 9;
$numTuxes = $numRows_rs_tuxedos; // Number of tuxes left to display
$pageNum = 1;
$catSet = isset($_GET['brand_id']) || isset($_GET['color_id']) ? true : false; //if a category is set or not
$uriQuery = strpos($_SERVER['QUERY_STRING'], "&") > 0 ? substr($_SERVER['QUERY_STRING'], 0, strpos($_SERVER['QUERY_STRING'], "&")) : $_SERVER['QUERY_STRING']; //grabs the set category
$pageURI = $catSet ? $_SERVER['PHP_SELF'] . "?" . $uriQuery : $_SERVER['PHP_SELF'];
if (isset($_GET['page_num']) && $_GET['page_num'] * 9 <= $numTuxes && $_GET['page_num'] != 1) {
$setPage = $_GET['page_num'];
$startTux = $setPage * $maxTuxes - $maxTuxes;
if ($catSet) {
echo "Page: <a href=\"" . $pageURI . "&page_num=1\">1</a>";
} else {
echo "Page: <a href=\"" . $pageURI . "?page_num=1\">1</a>";
}
$pageNum++;
do {
if ($pageNum == $setPage) {
echo " | " . $pageNum;
} else {
if ($catSet) {
echo " | <a href=\"" . $pageURI . "&page_num=" . $pageNum . "\">" . $pageNum . "</a>";
} else {
echo " | <a href=\"" . $pageURI . "?page_num=" . $pageNum . "\">" . $pageNum . "</a>";
}
}
$pageNum++;
$numTuxes -= $maxTuxes;
} while ($numTuxes - $maxTuxes > 9);
} else {
$startTux = 0;
echo "Page: " . $pageNum;
$pageNum++;
do {
if ($catSet) {
echo " | <a href=\"" . $pageURI . "&page_num=" . $pageNum . "\">" . $pageNum . "</a>";
} else {
echo " | <a href=\"" . $pageURI . "?page_num=" . $pageNum . "\">" . $pageNum . "</a>";
}
$pageNum++;
$numTuxes -= $maxTuxes;
} while ($numTuxes - $maxTuxes > 9);
}
}
?>