PHP PDO CRUD display data based on select box value - php

i was doing practice of PHP PDO CRUD example of codingcage ...link is http://www.codingcage.com/2015/04/php-pdo-crud-tutorial-using-oop-with.html
-- in index.php i have created a select box with dynamic value :
$conn = new mysqli('localhost', 'root', '', 'kk') or die ('Cannot connect to db');
$result = $conn->query("SELECT DISTINCT project FROM tbl_activitymaster where ProjectFlag='open' ");
echo "<html>";
echo "<body>";
echo "<select name='unqprj'>";
while ($row = $result->fetch_assoc()) {
unset($project, $project);
$project = $row['project'];
$name = $row['project'];
echo '<option value="'.$project.'">'.$project.'</option>';
$value="<?php echo $project;?>";
}
echo "</select>";
echo "</body>";
echo "</html>";
But the problem is that how to use this select box in the crud example to filter the records based on the value selected . I have done it in asp.net but in php how to do this. any help please or any link
// as mulder is suggesting but no idea how to implement with this eaample
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form2' >
<select name="value">
<option value="all">All</option>
<option value="Project1">Project1</option>
<option value="Project2">Project2</option>
</select>
<input type='submit' value = 'Show'>
</form>
class.crud.php..............
//show
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<td><?php print($row['id']); ?></td>
<td><?php print($row['Project']);?></td>
<td><?php print($row['ProjectUser']);?></td>
<td><?php print($row['Month']);?></td>
<td><?php print($row['Status']);?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
//paging
public function paging($query,$records_per_page)
{
$starting_position=0;
if(isset($_GET["page_no"]))
{
$starting_position=($_GET["page_no"]-1)*$records_per_page;
}
$query2=$query." limit $starting_position,$records_per_page";
return $query2;
}
index.php...............
<?php include_once '../header.php'; ?>
<?php include_once 'dbconfig.php'; ?>
<?php
$conn = new mysqli('localhost', 'root', '', 'kk')
or die ('Cannot connect to db');
$result = $conn->query("SELECT DISTINCT project FROM tbl_activitymaster where ProjectFlag='open' ");
echo "<html>";
echo "<body>";
echo "<select name='unqprj'>";
while ($row = $result->fetch_assoc()) {
unset($project, $project);
$project = $row['project'];
$name = $row['project'];
echo '<option value="'.$project.'">'.$project.'</option>';
$value="<?php echo $project;?>";
echo "</select>";
echo "</body>";
echo "</html>";
?>
<div class="container">
<table class='table table-bordered table-hover table-responsive'>
<thead style="background-color:#FFFBF0">
<tr>
<th>ID</th>
<th>Project</th>
<th>ProjectUser</th>
<th>Month</th>
<th>Status</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM tbl_activitymaster where ProjectUser='" . ($_SESSION['sess_username']) . "' ";
$records_per_page=10;
$newquery = $crud->paging($query,$records_per_page);
$crud->dataview($newquery);
?>
<tr>
<td colspan="7" align="center">
<div class="pagination-wrap">
<?php $crud->paginglink($query,$records_per_page); ?>
</div>
</td>
</tr>
</table>
</div>
<?php include_once '../footer.php'; ?>

Here the code of codeingcage firstly and you need to just inject select box over there like:
Index.php
<?php
include_once 'dbconfig.php';
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<div class="container">
<i class="glyphicon glyphicon-plus"></i> Add Records
</div>
<div class="clearfix"></div><br />
<div class="container">
<table class='table table-bordered table-responsive'>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>E - mail ID</th>
<th>Contact No</th>
<th colspan="2" align="center">Actions</th>
</tr>
<?php
$query = "SELECT * FROM tbl_users";
$records_per_page=3;
$newquery = $crud->paging($query,$records_per_page);
$crud->dataview($newquery);
?>
<tr>
<td colspan="7" align="center">
<div class="pagination-wrap">
<?php $crud->paginglink($query,$records_per_page); ?>
</div>
</td>
</tr>
</table>
</div>
<?php include_once 'footer.php'; ?>
dbconfig.php
<?php
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "dbpdo";
try
{
$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
include_once 'class.crud.php';
$crud = new crud($DB_con);
?>
class.crud.php
<?php
class crud
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function create($fname,$lname,$email,$contact)
{
try
{
$stmt = $this->db->prepare("INSERT INTO tbl_users(first_name,last_name,email_id,contact_no) VALUES(:fname, :lname, :email, :contact)");
$stmt->bindparam(":fname",$fname);
$stmt->bindparam(":lname",$lname);
$stmt->bindparam(":email",$email);
$stmt->bindparam(":contact",$contact);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
public function getID($id)
{
$stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
public function update($id,$fname,$lname,$email,$contact)
{
try
{
$stmt=$this->db->prepare("UPDATE tbl_users SET first_name=:fname,
last_name=:lname,
email_id=:email,
contact_no=:contact
WHERE id=:id ");
$stmt->bindparam(":fname",$fname);
$stmt->bindparam(":lname",$lname);
$stmt->bindparam(":email",$email);
$stmt->bindparam(":contact",$contact);
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
public function delete($id)
{
$stmt = $this->db->prepare("DELETE FROM tbl_users WHERE id=:id");
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
/* paging */
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php print($row['id']); ?></td>
<td><?php print($row['first_name']); ?></td>
<td><?php print($row['last_name']); ?></td>
<td><?php print($row['email_id']); ?></td>
<td><?php print($row['contact_no']); ?></td>
<td align="center">
<i class="glyphicon glyphicon-edit"></i>
</td>
<td align="center">
<i class="glyphicon glyphicon-remove-circle"></i>
</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
public function paging($query,$records_per_page)
{
$starting_position=0;
if(isset($_GET["page_no"]))
{
$starting_position=($_GET["page_no"]-1)*$records_per_page;
}
$query2=$query." limit $starting_position,$records_per_page";
return $query2;
}
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0)
{
?><ul class="pagination"><?php
$total_no_of_pages=ceil($total_no_of_records/$records_per_page);
$current_page=1;
if(isset($_GET["page_no"]))
{
$current_page=$_GET["page_no"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<li><a href='".$self."?page_no=1'>First</a></li>";
echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
}
else
{
echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
}
}
if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
}
?></ul><?php
}
}
/* paging */
}
add-data.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-save']))
{
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email_id'];
$contact = $_POST['contact_no'];
if($crud->create($fname,$lname,$email,$contact))
{
header("Location: add-data.php?inserted");
}
else
{
header("Location: add-data.php?failure");
}
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<?php
if(isset($_GET['inserted']))
{
?>
<div class="container">
<div class="alert alert-info">
<strong>WOW!</strong> Record was inserted successfully HOME!
</div>
</div>
<?php
}
else if(isset($_GET['failure']))
{
?>
<div class="container">
<div class="alert alert-warning">
<strong>SORRY!</strong> ERROR while inserting record !
</div>
</div>
<?php
}
?>
<div class="clearfix"></div><br />
<div class="container">
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='first_name' class='form-control' required></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='last_name' class='form-control' required></td>
</tr>
<tr>
<td>Your E-mail ID</td>
<td><input type='text' name='email_id' class='form-control' required></td>
</tr>
<tr>
<td>Contact No</td>
<td><input type='text' name='contact_no' class='form-control' required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save">
<span class="glyphicon glyphicon-plus"></span> Create New Record
</button>
<i class="glyphicon glyphicon-backward"></i> Back to index
</td>
</tr>
</table>
</form>
</div>
<?php include_once 'footer.php'; ?>
edit-data.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-update']))
{
$id = $_GET['edit_id'];
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email_id'];
$contact = $_POST['contact_no'];
if($crud->update($id,$fname,$lname,$email,$contact))
{
$msg = "<div class='alert alert-info'>
<strong>WOW!</strong> Record was updated successfully <a href='index.php'>HOME</a>!
</div>";
}
else
{
$msg = "<div class='alert alert-warning'>
<strong>SORRY!</strong> ERROR while updating record !
</div>";
}
}
if(isset($_GET['edit_id']))
{
$id = $_GET['edit_id'];
extract($crud->getID($id));
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<div class="container">
<?php
if(isset($msg))
{
echo $msg;
}
?>
</div>
<div class="clearfix"></div><br />
<div class="container">
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='first_name' class='form-control' value="<?php echo $first_name; ?>" required></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='last_name' class='form-control' value="<?php echo $last_name; ?>" required></td>
</tr>
<tr>
<td>Your E-mail ID</td>
<td><input type='text' name='email_id' class='form-control' value="<?php echo $email_id; ?>" required></td>
</tr>
<tr>
<td>Contact No</td>
<td><input type='text' name='contact_no' class='form-control' value="<?php echo $contact_no; ?>" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="glyphicon glyphicon-edit"></span> Update this Record
</button>
<i class="glyphicon glyphicon-backward"></i> CANCEL
</td>
</tr>
</table>
</form>
</div>
<?php include_once 'footer.php'; ?>
delete.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-del']))
{
$id = $_GET['delete_id'];
$crud->delete($id);
header("Location: delete.php?deleted");
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<div class="container">
<?php
if(isset($_GET['deleted']))
{
?>
<div class="alert alert-success">
<strong>Success!</strong> record was deleted...
</div>
<?php
}
else
{
?>
<div class="alert alert-danger">
<strong>Sure !</strong> to remove the following record ?
</div>
<?php
}
?>
</div>
<div class="clearfix"></div>
<div class="container">
<?php
if(isset($_GET['delete_id']))
{
?>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>E - mail ID</th>
<th>Gender</th>
</tr>
<?php
$stmt = $DB_con->prepare("SELECT * FROM tbl_users WHERE id=:id");
$stmt->execute(array(":id"=>$_GET['delete_id']));
while($row=$stmt->fetch(PDO::FETCH_BOTH))
{
?>
<tr>
<td><?php print($row['id']); ?></td>
<td><?php print($row['first_name']); ?></td>
<td><?php print($row['last_name']); ?></td>
<td><?php print($row['email_id']); ?></td>
<td><?php print($row['contact_no']); ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
</div>
<div class="container">
<p>
<?php
if(isset($_GET['delete_id']))
{
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<button class="btn btn-large btn-primary" type="submit" name="btn-del"><i class="glyphicon glyphicon-trash"></i> YES</button>
<i class="glyphicon glyphicon-backward"></i> NO
</form>
<?php
}
else
{
?>
<i class="glyphicon glyphicon-backward"></i> Back to index
<?php
}
?>
</p>
</div>
<?php include_once 'footer.php'; ?>
MY SQL :
CREATE TABLE IF NOT EXISTS `tbl_users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(25) NOT NULL, `last_name` varchar(25) NOT NULL, `email_id` varchar(50) NOT NULL, `contact_no` bigint(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21
Still adding your selectbox and will update this answer soon.

Related

When click on checkbox it show multiple value?

view:
<script>
$(document).ready(function(){
$(".uid").click(function(){
jid = $(".jid").attr("id");
uid = $(':checked').map(function() {
return this.id;
}).get().join(',');
$.ajax({
type:"POST",
data:{"jid":jid, "uid":uid},
url:"<?php echo base_url(); ?>shortlist",
success:function(data){
$(".stage_shortlist").html(data);
}
});
});
$(document).on("click",".uid_short",function(){
jid_short = $(".jid_short").attr("id");
uid_short = $(':checked').map(function() {
return this.id;
}).get().join(',');
alert(jid_short);
alert(uid_short);
});
});
</script>
<h2>Received</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody>
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$where = "jid='".$row['job_id']."' and share_with_emp='1'";
$this->db->where($where);
$sql = $this->db->get();
if($sql->num_rows() > 0)
{
$result = $sql->result_array();
foreach($result as $recieve)
{
?>
<tr>
<td><?php echo $recieve['fname']; ?></td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jid" class="jid" id="<?php echo $recieve['jid']; ?>"/>
<?php
if($recieve['shortlist']=='1')
{
echo '<input type="checkbox" name="uid" id="" class="uid" checked disabled><span>Shortlist</span>';
}
else
{
echo '<input type="checkbox" name="uid" id="'.$recieve["uid"].'" class="uid"><span>Shortlist</span>';
}
?>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<p>No resume Found</p>";
}
?>
</tbody>
</table>
</section>
<h2>Shortlisted</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody class="stage_shortlist">
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$where = "jid='".$row['job_id']."' and shortlist='1'";
$this->db->where($where);
$sql_short = $this->db->get();
$result_short = $sql_short->result_array();
foreach($result_short as $short)
{
?>
<tr>
<td><?php echo $short['fname']; ?></td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jid_short" class="jid_short" id="<?php echo $short['jid']; ?>"/>
<?php
if($short['interview']=='1')
{
echo '<input type="checkbox" name="uid_short" id="" class="uid_short" checked disabled><span>Shortlist</span>';
}
else
{
echo '<input type="checkbox" name="uid_short" id="'.$short["uid"].'" class="uid_short"><span>Shortlist</span>';
}
?>
</label>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</section>
controller:
<?php
public function shortlist()
{
$jid = $this->input->post('jid');
$uid = explode(",",$this->input->post('uid'));
$data = array('shortlist'=>'1');
foreach($uid as $user_id)
{
$where = "uid='".$user_id."' and jid='".$jid."'";
$this->db->where($where);
$sql = $this->db->update('upload_detail',$data);
}
if($sql==true)
{
$this->db->select('*');
$this->db->from('upload_detail');
$where = "jid='".$jid."' and shortlist='1'";
$this->db->where($where);
$sql_short = $this->db->get();
$result_short = $sql_short->result_array();
foreach($result_short as $short)
{
?>
<tr>
<td><?php echo $short['fname']; ?></td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jid_short" class="jid_short" id="<?php echo $short['jid']; ?>"/>
<?php
if($short['interview']=='1')
{
echo '<input type="checkbox" name="uid_short" id="" class="uid_short" checked disabled><span>Shortlist</span>';
}
else
{
echo '<input type="checkbox" name="uid_short" id="'.$short["uid"].'" class="uid_short"><span>Shortlist</span>';
}
?>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo '<p>Unable to proceed!</p>';
}
}
?>
In this code I am update shortlist value 1 onclick uid which is the part of recieved heading section and response call from shortlist controller which is work perfectly but problem is that when I click on class uid_short it alert multiple value inside the alert(uid_short); I don't know what where am I doing wrong? Please help me to solve this issue.
Thank You

Trying to update column values by submit button

I'm trying to update status on my leaves table by pressing the approve/reject button. Here's my table schema:
So far this is what i've done:
<?php
if(isset($_POST['approved']))
{
echo "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
echo "Rejected";
$status=$_POST['rejected'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
and here's my update.php
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE leaves SET status = :status WHERE id = :id";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave.php");
else
echo "Error";
?>
I'm getting the "error" when I click on approve/reject. I think the problem is with the update.php. I'm not sure what it is tho or where.
Try this : update.php
<?php
if(isset($_POST['approved']))
{
$msg = "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
$msg = "Rejected";
$status=$_POST['rejected'];
}
$id=$_POST['id'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE newusers SET username = '$status' WHERE id = '$id'";
if(mysqli_query($con, $sql))
header("refresh:1; url=index.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
and view-leave.php
<?php
if(isset($_GET['msg']))
{
echo $_GET['msg'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
You have to write database name here:
mysqli_select_db($con, 'dbname');
Did you try to declare a default value for the $msg=''; and $status = '';?

only can display 1 image. If the user have more that 1 image how to display it

in my programe, user login and can upload images. user may view all his images and delete it.
The problem is i only can display 1 image. If the user have more that 1 image how to display it.
display.php
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
$reg_user = new USER();
$stmt = $user_home->runQuery("SELECT * FROM bike_tbl WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$userID=$row['userID'];
echo $userID;
if(isset($_POST['btn-update']))
{
$status = $_POST['status'];
$remarks = $_POST['remarks'];
if($reg_user->updateBike($status,$remarks,$userID))
{
echo "good";
}
else {
echo "bad";
}
}
?>
<form action="" method="post">
<table class="table table-bordered">
<thead>
<tr>
<th>Image</th>
<th>File Name</th>
<th>Serial Number</th>
<th>Status</th>
<th>Remarks</th>
</tr>
</thead>
<?php while ($row[image])?>
<tbody>
<tr>
<td><img src="uploads/<?php echo $row['file'] ?>" height="100" width="100"></td>
<td><?php echo $row['file'] ?></td>
<th><?php echo $row['serialNumber'] ?></th>
<td><select name="status">
<option value="In use">In use</option>
<option value=Deleted>Deleted</option>
</select>
</td>
<td><input type="text" name="remarks" /></td>
</tr>
</tbody>
</table>
<button class="btn btn-large btn-primary" type="submit" name="btn-update">Update</button>
</form>
class.user.php
public function updateBike($status,$remarks,$userID)
{
try
{
// echo $status;
// echo$remarks;
// echo$userID;
// echo "UPDATE bike_tbl SET status=$status,remarks=$remarks WHERE userID=$userID:";
$stmt = $this->conn->prepare("UPDATE bike_tbl SET status=:status,remarks=:remarks WHERE userID=:userID");
$stmt->bindparam(":status",$status);
$stmt->bindparam(":remarks",$remarks);
$stmt->bindparam(":userID",$userID);
$stmt->execute();
return $stmt;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}

Call php function when click a button

I have a code :
<?php
getPriceListHeader();
function getPriceListDetail($PriceListCode)
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT * FROM pricelisdetail where pricelist_code='".$PriceListCode."'";
$results = $readConnection->fetchAll($query);
echo "<table id='tbdata'>
<thead>
<tr>
<th>Price List Code</th>
<th>Price List Name</th>
<th>Effective From</th>
<th>Effective To</th>
</tr>
</thead>
<tbody> ";
foreach ($results as $row)
{
echo "<tr>";
echo "<td> ".$row[entity_id];
echo "<td> ".$row[sku];
echo "<td> ".$row[sku];
echo "<td> ".$row[sku];
};
echo " </tbody>
</table> ";
}
function getPriceListHeader()
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM pricelistheader';
$results = $readConnection->fetchAll($query);
echo "
<h2>Price List</h2>
<div>
<h3>Please select Price List</h3>
<div>
<select class='element select large' id='pricelist' name='element_2'>";
foreach ($results as $row)
{
echo '<option value="' . $row[entity_id]. '">' . $row[sku] . '</option>';
}
echo "</select>
</div>
<input type='button' class='button' name='insert' value='Get Data' onclick='getPriceListDetail(pricelist.value)'/>
";
getPriceListDetail('');
}
?>
I have a dropdown list, a button, a table
When I select a value from dropdown list, then I click button , table will be filled data again. There are 2 method, getPriceListHeader(): load data header when load the page, getPriceListDetail : load data detail when click a button. I try to put event getPriceListDetail(value) to button, but when I click, nothing happens
Please help me how to do this.
Yes, you can call php via ajax request to server like this (very simple):
Note that the following code uses jQuery
jQuery.ajax({
type: "POST",
url: 'my_php_function.php',
dataType: 'name_the_data_type',
success: function (data) {
// here you will get the response your function
}
});
and my_php_function.php like this:
<?php
// here is your php code or function
?>
from the source How can I call PHP functions by JavaScript?
You can't attach PHP function to HTML. PHP is server-side language, so after it's displayed in users browser you can't refer to PHP again, unless you reload page.
There are 3 options I think you can do:
Reload page after changing select page and using GET prepare new data.
Send all data to user browser and than show only part of it related to selected option.
Use AJAX and ask server for new data in background.
Finally, I found out the way.It works for me. Now, I do not understand the code , I am trying to read and get it clearly.
Thanks all for your comments.
$to="";
$from="";
$show_order_statuses = 0;
$orserstatus = "";
$result_order = 0;
//var_dump($results);
//return;
if(!empty($_REQUEST['filter_type']))
{
$orders_row = array();
$filter_type = $_REQUEST['filter_type'];
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM pricelistitem where pricelist_code='".$filter_type."' ";
// $query = 'SELECT * FROM pricelistheader1';
$results = $readConnection->fetchAll($query);
foreach ($results as $rowid)
{
$result_order=10;
$orders_row[]=array($rowid['pricelist_code'],$rowid['product_code'],number_format( $rowid['unitprice'],2),$rowid['UOM']);
// $orders_row[]=array(1,1,1,1,1);
}
}
?>
<div id="anchor-content" class="middle">
<div id="page:main-container">
<div class="content-header">
<table cellspacing="0">
<tbody>
<tr>
<td style="width:50%;"><h3 class="icon-head head-report-sales-sales"><?php echo $this->__("Price List");?></h3></td>
<td class="form-buttons"><button style="" onclick="filterFormSubmit.submit()" class="scalable " type="button" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>"><span>Show Report</span></button></td>
</tr>
</tbody>
</table>
</div>
<div>
<div class="entry-edit">
<form method="get" action="<?php echo Mage::helper('core/url')->getCurrentUrl();?>" id="filter_form">
<?php /*?><input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /><?php */?>
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend">Filter</h4>
<div class="form-buttons"></div>
</div>
<div id="sales_report_base_fieldset" class="fieldset">
<div class="hor-scroll">
<table cellspacing="0" class="form-list">
<tbody>
<tr>
<td class="label"><label for="sales_report_filter_type">Filter By <span class="required">*</span></label></td>
<td class="value">
<select class="required-entry select" name="filter_type" id="sales_report_filter_type" onchange="myFunction();">
<?php
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM pricelistheader ";
$results = $readConnection->fetchAll($query);
$so=1;
foreach ($results as $row)
{
$selected='';
if ($filter_type==$row[pricelist_code])
$selected= ' selected=selected';
else
$selected='';
echo '<option value="' . $row[pricelist_code]. '" '.$selected.' >' . $row[pricelist_name].' '. $row[pricelist_code] . '</option>';
}
?>
</select>
</tr>
<tr>
<td class="label"><label for="effect_from">Effect From </label></td>
<td class="value">
<?php
foreach ($results as $row)
{
if ($filter_type==$row[pricelist_code])
echo $row[pricelist_fromdate];
}
?>
</td>
</tr>
<tr>
<td class="label"><label for="effect_from">Effect To </label></td>
<td class="value">
<?php
foreach ($results as $row)
{
if ($filter_type==$row[pricelist_code])
echo $row[pricelist_todate];
}
?>
</td>
</tr>
</tbody>
<script>
function myFunction() {
// document.getElementById("tbdata").deleteRow(1);
var rowCount = tbdata.rows.length;
for (var i = rowCount - 1; i > 0; i--) {
tbdata.deleteRow(i);
}
srt.value=pricelist.options[pricelist.selectedIndex].value;
";
//$('#tbdata').empty();
}
</script>
</table>
</div>
</div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
var filterFormSubmit = new varienForm('filter_form');
//]]>
</script>
<script type="text/javascript"> new FormElementDependenceController({"sales_report_order_statuses":{"sales_report_show_order_statuses":"1"}}); </script>
<style type="text/css">
.no-display{display:none;}
</style>
</div>
<div>
<?php if($result_order>0){?>
<table cellspacing="0" class="actions">
<tbody>
<tr>
<td class="pager"> </td>
<td class="export a-right">
<form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form_customer">
<input name="form_key_customer" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
</form>
<script type="text/javascript">
//<![CDATA[
var csvFormSubmitcustomer = new varienForm('csv_form_customer');
//]]>
</script>
</td>
<td class="filter-actions a-right">
<img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif"> Export to:
<select style="width:8em;" id="sales_order_grid_export_customer" name="sales_order_grid_export_customer">
<option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
</select>
<button onclick="csvFormSubmitcustomer.submit()" class="scalable task" type="button"><span>Export</span></button>
</td>
</tr>
</tbody>
</table>
<?php } ?>
<div id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr class="headings">
<th class=" no-link"><span class="nobr">Price List Code</span></th>
<th class=" no-link"><span class="nobr">Cust Code</span></th>
<th class=" no-link"><span class="nobr">Cust Name</span></th>
</tr>
</thead>
<tbody id="">
<?php
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM " . $resource->getTableName('catalog/product');;
$customercount=0;
$customerresults = $readConnection->fetchAll($query);
$customerresults = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('erp_pricelistcode_1', $filter_type)
// ->addFieldToSelect (array('created_at','customer_id','increment_id','updated_at','status','entity_id','state'))
;
// ->addAttributeToFilter('erp_pricelistcode_1','00')->load();
$so=1;
foreach ($customerresults as $row) {
$customercount++;
}
// var_dump(#$customerresults);
if($customercount>0){
foreach($customerresults as $singlerows){
$cot=0;
{
echo "<tr>";
{
$cot++;
?>
<td>
<?php
echo $singlerows->getData('erp_pricelistcode_1');
?>
</td>
<td>
<?php
echo $singlerows->getFirstname();
?>
</td>
<td>
<?php
echo $singlerows->getName();
?>
</td>
<?php
}
echo "</tr>";
}
}
}else{
?>
<tr class="even">
<td colspan="13" class="empty-text a-center">No records found.</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div>
<?php if($result_order>0){?>
<table cellspacing="0" class="actions">
<tbody>
<tr>
<td class="pager"> </td>
<td class="export a-right">
<form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form">
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
</form>
<script type="text/javascript">
//<![CDATA[
var csvFormSubmit = new varienForm('csv_form');
//]]>
</script>
</td>
<td class="filter-actions a-right">
<img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif"> Export to:
<select style="width:8em;" id="sales_order_grid_export" name="sales_order_grid_export">
<option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
</select>
<button onclick="csvFormSubmit.submit()" class="scalable task" type="button"><span>Export</span></button>
</td>
</tr>
</tbody>
</table>
<?php } ?>
<div id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr class="headings">
<th class=" no-link"><span class="nobr">Price List Code</span></th>
<th class=" no-link"><span class="nobr">Product Code</span></th>
<th class=" no-link"><span class="nobr">Unit Price</span></th>
<th class=" no-link"><span class="nobr">UOM</span></th>
</tr>
</thead>
<tbody id="">
<?php
$cot=0;
if(count($orders_row)>0){
foreach($orders_row as $singlerows){
$cot=0;
if(!empty($singlerows)){
echo "<tr>";
foreach($singlerows as $value){
$cot++;
?>
<td>
<?php
if ($cot==3 )
echo "<div style='float:right;width:30%;'>";
echo $value;
echo "</div>";
?>
</td>
<?php
}
echo "</tr>";
}
}
}else{
?>
<tr class="even">
<td colspan="13" class="empty-text a-center">No records found.</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

How to only shown one table after clicking search button?

I have a 2 tables like this when I search the member name.
link :
I wanted the search result to be shown only after search.
my code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
</table>
</center>
<?php
}
}
}
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table><center>
</div>
Please include the php file for me too!
To do it in easiest way just try to use this code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
<?php
}
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table>
</center>
<?php
}
}
?>
</div>
But I would recommend you to use SQL UNION Operator.
It will make your life easy for these type of problems

Categories