Judul : CODE BY OM SIR FOR with Search and Pagination
link : CODE BY OM SIR FOR with Search and Pagination
CODE BY OM SIR FOR with Search and Pagination
First create table toy:
SQL>
CREATE TABLE toy (
id int(8) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
code varchar(20) NOT NULL,
category varchar(255) NOT NULL,
price double NOT NULL,
stock_count bigint(16) NOT NULL,
PRIMARY KEY (id)
)
(1)write code for dbcontroller.php file:
<?php
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "ajax";
function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
mysql_select_db($this->database,$conn);
}
function runQuery($query)
{
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = mysql_query($query);
$rowcount = mysql_num_rows($result);
return $rowcount;
}
}
?>
(2)write code for perpage.php file:
<?php
function perpage($count, $per_page = '10',$href) {
$output = '';
$paging_id = "link_perpage_box";
if(!isset($_POST["page"])) $_POST["page"] = 1;
if($per_page != 0)
$pages = ceil($count/$per_page);
if($pages>1) {
if(($_POST["page"]-3)>0) {
if($_POST["page"] == 1)
$output = $output . '<span id=1 class="current-page">1</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="1" />';
}
if(($_POST["page"]-3)>1) {
$output = $output . '...';
}
for($i=($_POST["page"]-2); $i<=($_POST["page"]+2); $i++) {
if($i<1) continue;
if($i>$pages) break;
if($_POST["page"] == $i)
$output = $output . '<span id='.$i.' class="current-page" >'.$i.'</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $i . '" />';
}
if(($pages-($_POST["page"]+2))>1) {
$output = $output . '...';
}
if(($pages-($_POST["page"]+2))>0) {
if($_POST["page"] == $pages)
$output = $output . '<span id=' . ($pages) .' class="current-page">' . ($pages) .'</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $pages . '" />';
}
}
return $output;
}
function showperpage($sql, $per_page = 10, $href) {
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$perpage = perpage($count, $per_page,$href);
return $perpage;
}
?>
(3)write code for index.php file : your main file:
<?php
require_once("perpage.php");
require_once("dbcontroller.php");
$db_handle = new DBController();
$name = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("name","code");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "name":
$name = $v;
$queryCondition .= "name LIKE '" . $v . "%'";
break;
}
}
}
}
$orderby = " ORDER BY id desc";
$sql = "SELECT * FROM toy " . $queryCondition;
$href = 'index.php';
$perPage = 2;
$page = 1;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . $orderby . " limit " . $start . "," . $perPage;
$result = $db_handle->runQuery($query);
if(!empty($result)) {
$result["perpage"] = showperpage($sql, $perPage, $href);
}
?>
<html>
<head>
<title>CODE BY OM SIR FOR with Search and Pagination</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2>CODE BY OM SIR FOR with Search and Pagination</h2>
<div style="text-align:right;margin:20px 0px 10px;">
<a id="btnAddAction" href="add.php">Add New</a>
</div>
<div id="toys-grid">
<form name="frmSearch" method="post" action="index.php">
<div class="search-box">
<p><input type="text" placeholder="Name" name="search[name]" class="demoInputBox" value="<?php echo $name; ?>" />
<input type="submit" name="go" class="btnSearch" value="Search"><input type="reset" class="btnSearch" value="Reset" onclick="window.location='index.php'"></p>
</div>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Category</strong></th>
<th><strong>Price</strong></th>
<th><strong>Stock Count</strong></th>
<th><strong>Action</strong></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $k=>$v) {
if(is_numeric($k))
{
?>
<tr>
<td><?php echo $result[$k]["name"]; ?></td>
<td><?php echo $result[$k]["code"]; ?></td>
<td><?php echo $result[$k]["category"]; ?></td>
<td><?php echo $result[$k]["price"]; ?></td>
<td><?php echo $result[$k]["stock_count"]; ?></td>
<td>
<a class="btnEditAction" href="edit.php?id=<?php echo $result[$k]["id"]; ?>">Edit</a> <a class="btnDeleteAction" href="delete.php?action=delete&id=<?php echo $result[$k]["id"]; ?>">Delete</a>
</td>
</tr>
<?php
}
}
if(isset($result["perpage"])) {
?>
<tr>
<td colspan="6" align=right> <?php echo $result["perpage"]; ?></td>
</tr>
<?php } ?>
<tbody>
</table>
</form>
</div>
</body>
</html>
Output:
Demikianlah Artikel CODE BY OM SIR FOR with Search and Pagination
Sekianlah artikel CODE BY OM SIR FOR with Search and Pagination kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel CODE BY OM SIR FOR with Search and Pagination dengan alamat link https://othereffect.blogspot.com/2017/01/code-by-om-sir-for-with-search-and.html
0 Response to "CODE BY OM SIR FOR with Search and Pagination"
Post a Comment