I am trying to convert one of my older scripts into OOP with a class but I have a question: how do I reference a variable between methods within the same class? I tried using $this->variable with no luck.
Here's the code:
Code:<?php class DisplayCat { // Insures that the value of $cat is valid public function CheckCatValue($cat) { if (($cat == "") || ($cat == "0")) { echo "category was left blank"; } elseif (is_numeric($cat) == false) { echo "category must be a numeric value"; } else { $this->DisplayProducts($cat); } } // Loops through the category and displays produts public function DisplayProducts($cat) { $getProducts = mysql_query("SELECT * FROM products WHERE prod_cat = $cat LIMIT $this->offset,$this->rowsperpage") or die(mysql_error()); $num_rows = mysql_num_rows($getProducts); while($row = mysql_fetch_array( $getProducts )) { $prod_id = $row["prod_id"]; $prod_title = $row["prod_title"]; $prod_pprice = $row["prod_pprice"]; $prod_gprice = $row["prod_gprice"]; $prod_thumb = $row["prod_thumb"]; for ($n = 1; $n <=4; $n++) { } echo " <div class=\"catbody_block\"> <div class=\"catbody_img\"> <a href=\"product.php?product=$prod_id\"><img src=\"$prod_thumb\" border=\"0\" alt=\"$prod_id\" /></a> </div> <div class=\"catbody_data\"> $prod_id | $prod_pprice <br /> $prod_title <br /> <a href=\"product.php?product=$prod_id\"><img src=\"images/_info.gif\" border=\"0\" alt=\"info\"/></a><a href=\"addtocart.php?product=$prod_id\"><img src=\"images/_buy.gif\" border=\"0\" /></a> </div> </div>"; } } // Creates pagination values public function CreatePagination($cat,$sql,$conn) { $sql = "SELECT COUNT(*) FROM products WHERE prod_cat = $cat"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; if ($numrows == 0) { echo "Numrows = 0"; } else { $rowsperpage = 12; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; } } // Displays pagination footer public function DisplayPagination($cat) { echo "<div class=\"catbody_pagination\">More: "; if ($this->currentpage > 1) { echo "<a href='{$_SERVER['PHP_SELF']}?$this->currentpage=1&cat=$cat'><< </a> "; $prevpage = $this->currentpage - 1; } $range = 12; for ($x = ($this->currentpage - $range); $x < (($this->currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $this->totalpages)) { if ($x == $this->currentpage) { echo "$x"; } else { echo " <a href='{$_SERVER['PHP_SELF']}?$this->currentpage=$x&cat=$cat'>$x</a> "; } } } if ($this->currentpage != $this->totalpages) { $nextpage = $this->currentpage + 1; // echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&cat=$cat'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?$this->currentpage=$this->totalpages&cat=$cat'> >></a>"; } echo "     </div>"; } } ?>
Code:<?php include ("classes/displaycat.php"); $cat = mysql_real_escape_string($_GET['cat']); $DisplayCat = new DisplayCat; $CheckCatValue = $DisplayCat->CheckCatValue($cat); $CreatePagination = $DisplayCat->CreatePagination($cat); $DisplayPagination = $DisplayCat->DisplayPagination($cat); ?>
I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
and assume it has to do with not properly passing the values of LIMIT $offset,$rowsperpage - how do I do that?




Bookmarks