Jump to content

pagination class not working


mdvignesh

Recommended Posts


Help to solve

 

i am getting Access denied for user 'SYSTEM'@'localhost' (using password: NO)

phpclasspagination

 

<html>

<head>

<link rel="stylesheet" href="style.css" type="text/css" />

</head>

<body>

<?php

 

//$dbHost = 'localhost';

//$dbUser = 'root';

//$dbPass = '';

//$dbName = 'pagination';

 

//$mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);

 

require("CSSPagination.class.php");

 

$sql1 = "SELECT * FROM records WHERE active='1' ORDER BY name";

 

$rowsperpage = 5;

 

 

$website = $_SERVER['PHP_SELF']."?id=1";

 

$pagination = new CSSPagination($sql1, $rowsperpage, $website);

$pagination->setPage($_GET


); // dont change it

echo $pagination->showPage();

 

 

$sql2="SELECT * FROM records ORDER BY name ASC LIMIT " . $pagination->getLimit() . ", " . $rowsperpage;

 

$dbcon=mysql_connect($dbHost, $dbUser, $dbPass);

 

mysql_select_db("pagination",$dbcon);

 

$result = @mysql_query($sql2, $mysqli) or die("failed");

 

//$result = $mysqli->query($sql2);

//$rows = $result->fetch_array(MYSQLI_ASSOC)

 

while($rows = mysql_fetch_array($result)) {

echo $rows["name"];

echo "<br>";

echo $rows["id"];

}

 

echo $pagination->showPage(); // Show the pagination index

//ob_end_flush();

?>

</body>

</html>

 

 

CSSPagination.class

 

 

<?php

 

class CSSPagination

{

private $totalrows;

private $rowsperpage;

private $website;

private $page;

private $sql;

 

public function __construct($sql, $rowsperpage, $website)

{

$this->sql = $sql;

$this->website = $website;

$this->rowsperpage = $rowsperpage;

}

 

public function setPage($page)

{

if (!$page) { $this->page=1; } else  { $this->page = $page; }

}

 

public function getLimit()

{

return ($this->page - 1) * $this->rowsperpage;

}

 

private function getTotalRows()

{

$result = @mysql_query($this->sql) or die ("mdv ". mysql_error());

$this->totalrows = mysql_num_rows($result);

}

 

private function getLastPage()

{

return ceil($this->totalrows / $this->rowsperpage);

}

 

public function showPage()

{

$this->getTotalRows();

 

$pagination = "";

$lpm1 = $this->getLastPage() - 1;

$page = $this->page;

$prev = $this->page - 1;

$next = $this->page + 1;

 

$pagination .= "<div class=\"pagination\"";

if($margin || $padding)

{

$pagination .= " style=\"";

if($margin)

$pagination .= "margin: $margin;";

if($padding)

$pagination .= "padding: $padding;";

$pagination .= "\"";

}

$pagination .= ">";

 

 

 

if ($this->getLastPage() > 1)

{

if ($page > 1)

$pagination .= "<a href=$this->website&page=$prev>« prev</a>";

else

$pagination .= "<span class=\"disabled\">« prev</span>";

 

 

if ($this->getLastPage() < 9)

{

for ($counter = 1; $counter <= $this->getLastPage(); $counter++)

{

if ($counter == $page)

$pagination .= "<span class=\"current\">".$counter."</span>";

else

$pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";

}

}

 

elseif($this->getLastPage() >= 9)

{

if($page < 4)

{

for ($counter = 1; $counter < 6; $counter++)

{

if ($counter == $page)

$pagination .= "<span class=\"current\">".$counter."</span>";

else

$pagination .= "<a href=$this->website&page=$counter/>".$counter."</a>";

}

$pagination .= "...";

$pagination .= "<a href=$this->website&page=$lpm1>".$lpm1."</a>";

$pagination .= "<a href=$this->website&page=".$this->getLastPage().">".$this->getLastPage()."</a>";

}

elseif($this->getLastPage() - 3 > $page && $page > 1)

{

$pagination .= "<a href=$this->website&page=1>1</a>";

$pagination .= "<a href=$this->website&page=2>2</a>";

$pagination .= "...";

for ($counter = $page - 1; $counter <= $page + 1; $counter++)

{

if ($counter == $page)

$pagination .= "<span class=\"current\">".$counter."</span>";

else

$pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";

}

$pagination .= "...";

$pagination .= "<a href=$this->website&page=$lpm1>$lpm1</a>";

$pagination .= "<a href=$this->website&page=".$this->getLastPage().">".$this->getLastPage()."</a>";

}

else

{

$pagination .= "<a href=$this->website&page=1>1</a>";

$pagination .= "<a href=$this->website&page=2>2</a>";

$pagination .= "...";

for ($counter = $this->getLastPage() - 4; $counter <= $this->getLastPage(); $counter++)

{

if ($counter == $page)

$pagination .= "<span class=\"current\">".$counter."</span>";

else

$pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";

}

}

}

if ($page < $counter - 1)

$pagination .= "<a href=$this->website&page=$next>next »</a>";

else

$pagination .= "<span class=\"disabled\">next »</span>";

$pagination .= "</div>\n";

}

 

return $pagination;

}

}

?>

 

Link to comment
Share on other sites

The error is occurring at the mysql_query statement, because you have not made a mysql connection before calling code that needs a mysql connection. You also need to get all the @ error suppressors out of your code. There's absolutely no reason to put @'s in any code (when developing and testing code, you want to see all the errors, not hide them and on a live server display_errors should be turned off, so having @'s in your code would just slow it down without serving any purpose.) The @'s you have now are hiding php error messages that would help you find the problems in your code.

 

You are also trying to put an instance of a mysqli database connection into the second parameter of a mysql_query() statement. That won't work. You cannot mix mysql (non-i ) and mysqli (with-i ) statements together.

 

Edit: Also, any WHERE clause you use on your page must be present in both query statements so that the queries will find the same set of data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.