Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counter Issue 1

Status
Not open for further replies.

venetianjigsaw

Programmer
Mar 25, 2005
29
0
0
US
Hi,

On my same e-commerce site, I have a catalog that contains 22 products.

Essentially, I also have three categories which comprise the catalog:

Carseats (cst) 6 products
Furniture (frn) 10 products
Strollers (str) 6 products

My problem is that when I display each category, the numOfProducts returned is also 22. Can/will someone please take a look at my code and tell me what I have done wrong?

Thanks

VJ

<?php
session_start();
error_reporting(E_ALL);
$cellContent="&nbsp;";
$sesid=session_id();
echo "-";
//foreach($_GET as $key => $value)
//{
//print "Value of " . $key . " = " . $value . "<br />";
//}
//die;
//echo "-";

$numPerPage=3;
$start=0;

if (isset($_GET["start"]))
{
$start=$_GET["start"];
}

function writePageNumbers($numOfProducts,$numPerPage,$start,$category)
{
$numOfPages=$numOfProducts/$numPerPage;
//echo "numOfProd=".$numOfProducts;
//echo "numPerPg=".$numPerPage;
//echo "numberOfPages=".$numOfPages;
$numOfPages=ceil($numOfPages);
echo "<table bgcolor='#FFFACD'>";
echo "<tr style='background-color:#FFFACD'>";

$currentPage=($start/$numPerPage)+1;

for($i=1;$i<=$numOfPages;$i++)
{
if($i==$currentPage)
{
echo "\n<td>";
if ($category!="")
{
echo"<span style='font-weight:bold;font-size:12pt;font-family:arial;color:#999999'>$category</span>";
}
echo "<span style='color:#999999'>";
echo "[";
echo $i;
echo "]";
echo "</span>";
echo "</td>\n";
}
else
{
$start=($i-1) * $numPerPage;
echo "\n<td>";
echo "[";
echo "<a href='products.php?start=$start";
//if (isset($_GET["category"]))
//{
// echo "&category=$category";
//}
// if (isset($_GET["type"]))
// {
// $searchText=$_GET["searchText"];
// $searchWhat=$_GET["searchWhat"];
// echo "&type=search";
// echo "&searchText=$searchText";
// echo "&searchWhat=$searchWhat";
// }
echo "'>";
echo $i;
echo "</a>";
echo "]";
echo "</td>\n";
}
}
echo "</tr>";
echo "</table>";
}

include("../include/dbconn.inc");

$oursql="select * from tblproducts";
//echo $oursql;
//die;

//Execute SQL stmt
$myresult = mysql_query($oursql) or die (mysql_error());
$numOfProducts=mysql_num_rows($myresult);
//echo $numOfProducts;

// create sql stmt for entire catalog
$oursql="select * from tblproducts limit $start, $numPerPage";
//echo $oursql;
//die;

$myresult = mysql_query($oursql) or die (mysql_error());
//echo $myresult;
//echo die;

//create sql stmt for category only
//If category is set and mfgname is not
$category="";
if (isset($_GET["category"]) && !isset($_GET["mfgname"]))
{
if ($_GET["category"]=="Entire Site")
{
$oursql="select * from tblproducts";
}
else
{
$category=$_GET["category"];
$oursql="select * from tblproducts where category='$category'";
}
}
//echo $oursql;
//die;

if (isset($_GET["category"]) && !isset($_GET["mfgname"]))
{
if ($_GET["category"]=="Entire Site")
{
$oursql="select * from tblproducts limit $start, $numPerPage";
}
else
{
$category=$_GET["category"];
$oursql="select * from tblproducts where category='$category' limit $start, $numPerPage";
}
}
//echo $oursql;
//die;
$myresult = mysql_query($oursql) or die (mysql_error());
//echo $myresult;
//echo die;

//If category is not set and mfgname is

if (!isset($_GET["category"]) && isset($_GET["mfgname"]))
{
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where mfgname='$mfgname'";
}
//echo $oursql;
//die;

if (!isset($_GET["category"]) && isset($_GET["mfgname"]))
{
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where mfgname='$mfgname' limit $start, $numPerPage";
}
//echo $oursql;
//die;
$myresult = mysql_query($oursql) or die (mysql_error());
//echo $myresult;
//echo die;

//If category and mfgname is set
if (isset($_GET["category"]) && isset($_GET["mfgname"]))
{
if ($_GET["category"]=="Entire Site")
{
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where mfgname='$mfgname'";
}
else
{
$category=$_GET["category"];
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where category='$category' and mfgname='$mfgname'";
}
}
//echo $oursql;
//die;
if (isset($_GET["category"]) && isset($_GET["mfgname"]))
{
if ($_GET["category"]=="Entire Site")
{
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where mfgname='$mfgname' limit $start, $numPerPage";
}
else
{
$category=$_GET["category"];
$mfgname=$_GET["mfgname"];
$oursql="select * from tblproducts where category='$category' and mfgname='$mfgname' limit $start, $numPerPage";
}
}
//echo $oursql;
//die;
//Execute the sql stmt
$myresult = mysql_query($oursql) or die (mysql_error());
//echo $myresult;
//echo die;

?>

<table>
<tr>
<td width='25%'><?php writePageNumbers($numOfProducts,$numPerPage,$start,$category); ?></td>
</tr>
</table>
 
You have this sql statement:
Code:
$oursql="select * from tblproducts";
Which I assume gathers all products, no matter the category.
Following that you set $numOfProducts by these lines:
Code:
$myresult = mysql_query($oursql) or die (mysql_error());
$numOfProducts=mysql_num_rows($myresult);
Which will give you 22...
 
Yes...the catalog should have [8] links that display, with three items/page: $numPerPage=3; $start=0;

For the carseats page, I have six items and there should be [2] links that display: three/page

For the furniture page, I have 10 items and there should be [4] links that display: three/page

etc.

Does that make more sense? =)


 
If you just want the number of products in a certain category then when this segment of code is executed:
Code:
else
{
    $category=$_GET["category"];
    $oursql="select * from tblproducts where category='$category'";
}
You will need to call:
Code:
$myresult = mysql_query($oursql) or die (mysql_error());
$numOfProducts=mysql_num_rows($myresult);
Instead of at the top of the script.

Also I would be remiss not to mention that code like this:
Code:
$category=$_GET["category"];
$oursql="select * from tblproducts where category='$category'";
has some security flaws, which should be fixed before using the code live on the internet. (sorry if you already know)

Hope this makes sense...
 
Itshim;

Okay...what you suggested works! However, my catalog page is displaying all the products instead of 3 per page. Any clues?

Thanks

VJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top