When I put a term in search-box, my pagination is not working although it gives me a perfect result.
For an eg. If I type "Laptops" it gives me records having laptops,but my pagination is not going with the flow. I don't know where I have missed something.
Here is my code:
Search-Box:
<form action="" method="GET">
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
<input type="submit" value="Submit" />
</form>
To Display data:
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products WHERE status='Active' LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Image</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<!--<th>Category</th>
<th>Subcategory</th>-->
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or status LIKE '%" . $term . "' or quantity LIKE '%" . $term . "' or details LIKE '%" . $term . "' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
while ($row = mysql_fetch_array($r_query))
{
echo "<tr bgcolor=''>";
echo "<td width='5%'>" . $row['id'] . "</td>";?>
<td width="10%"><img style='border:#666 1px solid;' width='70' src="<?php echo $row["productimage"]; ?>" alt="" /></td>
<?php echo "<td width='20%'>" . $row['product_name'] . "</td>";
echo "<td width='5%'>" . $row['price'] . "</td>";
echo "<td width='5%'>" . $row['status'] . "</td>";
echo "<td width='5%'>" . $row['quantity'] . "</td>";
echo "<td width='19%'>" . $row['details'] . "</td>";
// echo "<td>" . $row['category'] . "</td>";
// echo "<td>" . $row['subcategory'] . "</td>";
echo "<td width='10%'>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";?>
<td><a href="#" class="delete" onclick="dialogbox(<?php echo $row['id']; ?>)" >Delete</a>
<?php
//echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "' onclick='return show_confirm();' >Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
Pagination Code:
<?php
//Pagination code starts here
$sql = "SELECT * FROM products";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='product_listing.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
You have to use r_query to find no of rows in pagination. Now you are finding the total number of rows in the table
<?php
//Pagination code starts here
$total_records = mysql_num_rows($r_query); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='product_listing.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
Related
If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>
I wrote a code to connect a html page to the database, but it does not search for all employees in the table. I'm not sure why the code seems to be correct. It does not give any error or warning.
Can anyone explain to me what is wrong?
<div>
<form id='searchform' action='index.php' method='get'>
<a href='index.php'>All employees</a> ---
Search by a last name:
<input id='search' name='search' type='text' size='20' value='<?php echo #$_GET['search']; ?>' />
<input id='submit' type='submit' value='Go!' />
</form>
</div>
<?php
// check if search view of list view
if (isset($_GET['search'])) {
$sql = "SELECT * FROM Employee WHERE LastName like '%" . #$_GET['search'] . "%'";
} else {
$sql = "SELECT * FROM Employee";
}
// execute sql statement
$stmt = oci_parse($conn, $sql);
oci_execute($stmt);
?>
<table style='border: 1px solid #DDDDDD'>
<thead>
<tr>
<th>Personal Number</th>
<th>Insurance Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Gender</th>
<th>Hire Date</th>
<th>Salary</th>
<th>Office ID</th>
</tr>
</thead>
<tbody>
<?php
// fetch rows of the executed sql query
while ($row = oci_fetch_assoc($stmt)) {
echo "<tr>";
echo "<td>" . $row['PersonalNumber'] . "</td>";
echo "<td>" . $row['InsuranceNumber'] . "</td>";
echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
echo "<td>born on " . $row['BirthDate'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>hired on " . $row['HireDate'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td>" . $row['OfficeID'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div>There are found <?php echo oci_num_rows($stmt); ?> employees!</div>
<?php oci_free_statement($stmt); ?>
</body>
I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
I am stuck in one of my application module. I have to set multiple delete options in my application.
I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:
<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
</head>
<body >
<?php
$con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
echo "<table border='1' style='
background-color: white;'>
<tr>
<th></th>
<th>id</th>
<th style='padding-left: 11px;'>Lead Generated Date </th>
<th>name</th>
<th>email</th>
<th>contacts</th>
<th>requirement</th>
<th style='display:none;'>Company name</th>
<th style='display:none;'>Address</th>
<th>Next Follow-Up date</th>
<th>Final_Details</th>
<th style='display:none;'>Status</th>
<th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
<th>EDIT</th>
<th>Follow-up History</th>
<th>Add Follow-up</th>
<th>Delete Record</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['contacts'] . "</td>";
echo "<td>" . $row['requirement'] . "</td>";
echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
echo "<td style='display:none;'>" . $row['address'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['final_details'] . "</td>";
echo "<td style='display:none;'>" . $row['status'] . "</td>";
echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";
$count=mysqli_num_rows($result);
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM Persons WHERE id='$del_id'";
echo $sql;
$result2 = mysqli_query($con,$sql);
}
// if successful redirect to delete_multiple.php
if($result2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
}
}
mysqli_close($con);
echo "</table>";
echo "</form>";
?>
<br><br>
[ Back To Home ]
</body>
</html>
I am stuck, the query is not working I think. I don't know what I am missing.
Try replacing
$sql = "DELETE FROM Persons WHERE id='$del_id'";
With
$sql = "DELETE FROM Persons WHERE id=$del_id";
I'm assuming the ID is a number so the single quotes aren't needed.
I am making a project on proposal management system and I am stuck on the pricing module. Here is my HTML DEMO. I have shown only required field in the output. INSERT queries below are correct.
I am trying to AUTOMATICALLY display the sum of the 'amount' in the 'total' each time the user presses 'save' button . The value of 'total' should increment when user presses 'save' either from 'add services' table or from 'add products' table. I am unsuccessful so far. HOW CAN I DO IT?
I have tried this: (Any other solution will be appreciated).
<td> <input type="text" name="amount" maxlength="15" id="amount" readonly="true" value=" <?php
if(isset($_POST['save'])|| isset($_POST['psave']))
{
$con= mysqli_connect("localhost","root","","my_db")or die("cannot connect");
$sql = "SELECT proposal_services.service_amount AMOUNT,proposal_products.product_amount AMOUNT1, FROM proposal_services INNER JOIN proposal_products ON proposal_products.proposal_id=proposal_services.proposal_id";
$result= mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
foreach ( $row as $records )
{
$sum = str_replace(",", "", $records['AMOUNT']) + str_replace(",", "", $records['AMOUNT1']);
}
echo number_format( $sum, 2 );
}
?> "/></td>
my php code where I insert and display results is:
insert_services.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['ser_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_services(proposal_id,user_id,service_name,service_description,service_price,per,service_quantity,service_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_services WHERE proposal_id='$prop_id' ";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th align='center'>Service Name</th>
<th align='center'>Description</th>
<th align='center'>Price</th>
<th align='center'>per</th>
<th align='center'>Quantity</th>
<th align='center'>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<div id='change'>";
echo "<tr>";
echo "<td align='center'>" . $row['service_name'] . "</td>";
echo "<td align='center'>" . $row['service_description']. "</td>";
echo "<td align='center'>" . $row['service_price']. "</td>";
echo "<td align='center'>" . $row['per']. "</td>";
echo "<td align='center'>" . $row['service_quantity']. "</td>";
echo "<td align='center'>" . $row['service_amount']. "</td>";
echo "</tr>";
echo "</div>";
}
echo "</table>";
?>
insert_products.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['pro_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_products(proposal_id,user_id,product_name,product_description,product_price,per,product_quantity,product_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_products WHERE proposal_id='$prop_id'";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>per</th>
<th>Quantity</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_description'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo "<td>" . $row['per'] . "</td>";
echo "<td>" . $row['product_quantity'] . "</td>";
echo "<td>" . $row['product_amount'] . "</td>";
echo "<td align='center'>" . "<input type='button' value= 'edit'>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>