i'm working on a website an i have a page showing all the data from my database with pagination all are working fine except for search.when i search like tom it only shows the result from the first page..i have a name of tom in different pages but it only show result where i'm in the current page.
please help me with this..
class.user.php
<?php
class paginate
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
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 echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['province']; ?></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)
{
?><tr><td colspan="4" align="center"><?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 "<a href='".$self."?page_no=1'>First</a> ";
echo "<a href='".$self."?page_no=".$previous."'>Previous</a> ";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong> ";
}
else
{
echo "<a href='".$self."?page_no=".$i."'>".$i." </a> ";
}
}
if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<a href='".$self."?page_no=".$next."'>Next</a> ";
echo "<a href='".$self."?page_no=".$total_no_of_pages."'>Last</a> ";
}
?></td></tr><?php
}
}
}
jquery/ajax
<script>
$(document).ready(function(){
$("#searchme").keyup(function(){
if( $(this).val() != "")
{
$("#data tbody>tr").hide();
$("#data td:contains-ci('" + $(this).val() + "')").parent("tr").show();
}
else
{
$("#data tbody>tr").show();
}
});
});
</script>
index.php
<body>
<div id="table-wrapper" class="table-responsive">
<input type="text" name="search" id="searchme" placeholder="Search...">
<table id="data" class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM login";
$records_per_page=20;
$newquery = $paginate->paging($query,$records_per_page);
$paginate->dataview($newquery);
$paginate->paginglink($query,$records_per_page);
?>
</tbody>
</table>
</div>
</body>
Related
<table class="table table-striped table-bordered" >
<thead>
<tr bgcolor='#89ff8'>
<td>First Name</td>
<td>First Name</td>
<td>Trade</td>
<td>registration Date</td>
<td>View Info</td>
</tr>
</thead>
<tbody>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['LastName']."</td>";
echo "<td>".numToTrade($res['Trade'])."</td>";
echo "<td>".$res['DateRegistered']."</td>";
echo "<td><a class='btn btn-info btn-xs' href=\"artisaninfo.php?id=$res[ArtisanId]\">view info</a></td>";
}
?>
</tr>
</tbody>
</table>
<?php
function numToTrade($num) {
$trade = null;
if ($num == 1) {
$trade= 'Plumber';
}
elseif($num==2) {
$trade = 'Electrician';
} elseif ($num==3) {
$trade = 'Carpenter';
} elseif ($num==4) {
$trade = 'Mechanic';
} elseif ($num==5) {
$trade = 'Ac-Technician';
} elseif ($num==6) {
$trade = 'Painter';
}
return $trade;
}
?>
function to return a string based on integer value statues php only appears onced. for all table rows the trade column is not rendering integer as it should in php
please help me.
function to return a string based on integer value statues php only appears onced. for all table rows the trade column is not rendering integer as it should in php
function numToTrade($num) {
if ($num == 1) {
return 'Plumber';
}
elseif($num==2) {
return 'Electrician';
} elseif ($num==3) {
return 'Carpenter';
} elseif ($num==4) {
return 'Mechanic';
} elseif ($num==5) {
return 'Ac-Technician';
} elseif ($num==6) {
return 'Painter';
}
}
?>
A faster version of the code worked for me
I've created a database with two tables. I connected this database with a PHP page for a small CRUD app.
To execute the CRUD operations, I've created a crud.class.php that hold all operations.
Operations on the first table (membri) work but into the second (articoli) they fail, in the update articoli page.
Simply, I've create the same PHP function for the first table for the second, but it send me back this error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
Here are my crud.class.php and edi_articoli.php page:
Class.crud.php:
class crud
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
/***************************************** Crea Articoli******************************/
public function crea_articolo($data,$autore,$titolo)
{
try
{
$stmt = $this->db->prepare("INSERT INTO articoli(data,autore,titolo) VALUES(:data, :autore, :titolo)");
$stmt->bindparam(":data",$data);
$stmt->bindparam(":autore",$autore);
$stmt->bindparam(":titolo",$titolo);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
/***************************************** Update Articolo******************************/
public function getID_articoli($id)
{
$stmt = $this->db->prepare("SELECT * FROM articoli WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
public function update_articoli($id,$data,$autore,$titolo)
{
try
{
$stmt=$this->db->prepare("UPDATE articoli SET data=:data,
autore=:autore,
titolo:titolo,
WHERE id=:id ");
$stmt->bindparam(":data",$data);
$stmt->bindparam(":autore",$autore);
$stmt->bindparam(":titolo",$titolo);
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
/***************************************** End Crea e aggiungi Articoli******************************/
/***************************************** Articoli******************************/
public function articoliview($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['data']); ?></td>
<td><?php print($row['autore']); ?></td>
<td class="setWidth concat"><div><?php print($row['titolo']); ?></div></td>
<td align="center">
<i class="glyphicon glyphicon-eye-open"></i>
</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>Non c'è nulla qui...</td>
</tr>
<?php
}
}
/***************************************** End Articoli******************************/
/***************************************** CREAZIONE UTENTI******************************/
/****************************************************************************************/
public function create($nome,$cognome,$brev_descr,$descrizione)
{
try
{
$stmt = $this->db->prepare("INSERT INTO membri(nome,cognome,brev_descr,descrizione) VALUES(:nome, :cognome, :brev_descr, :descrizione)");
$stmt->bindparam(":nome",$nome);
$stmt->bindparam(":cognome",$cognome);
$stmt->bindparam(":brev_descr",$brev_descr);
$stmt->bindparam(":descrizione",$descrizione);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
public function getID($id)
{
$stmt = $this->db->prepare("SELECT * FROM membri WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
/***************************************** END CREAZIONE UTENTI******************************/
/***************************************** UPDATE UTENTI ******************************/
public function update($id,$nome,$cognome,$brev_descr,$descrizione)
{
try
{
$stmt=$this->db->prepare("UPDATE membri SET nome=:nome,
cognome=:cognome,
brev_descr=:brev_descr,
descrizione=:descrizione
WHERE id=:id ");
$stmt->bindparam(":nome",$nome);
$stmt->bindparam(":cognome",$cognome);
$stmt->bindparam(":brev_descr",$brev_descr);
$stmt->bindparam(":descrizione",$descrizione);
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
/***************************************** END UPDATE UTENTI ******************************/
/***************************************** DELETE UTENTI******************************/
public function delete($id)
{
$stmt = $this->db->prepare("DELETE FROM membri WHERE id=:id");
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
/***************************************** END DELETE UTENTI******************************/
/***************************************** VIEW UTENTI ******************************/
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['nome']); ?></td>
<td><?php print($row['cognome']); ?></td>
<td class="setWidth concat"><div><?php print($row['brev_descr']); ?></div></td>
<td align="center">
<i class="glyphicon glyphicon-eye-open"></i>
</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>Non c'è nulla qui...</td>
</tr>
<?php
}
}
/***************************************** END VIEW UTENTI ******************************/
/***************************************** 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;
}
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'>Primo</a></li>";
echo "<li><a href='".$self."?page_no=".$previous."'>Precedente</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."'>Prossimo</a></li>";
echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Ultimo</a></li>";
}
?></ul><?php
}
}
/* paging */
}
Here my "update_articoli.php" page:
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-update']))
{
$id = $_GET['edit_articolo_id'];
$data = $_POST['data'];
$autore = $_POST['autore'];
$titolo = $_POST['titolo'];
if($crud->update_articoli($id,$data,$autore,$titolo))
{
$msg = "<br/><div class='alert alert-success'>
<strong>L'articolo è stato aggiornato correttamente!</strong>
</div>";
}
else
{
$msg = "<div class='alert alert-warning'>
<strong> C'è stato un errore durante l' aggiornamento dell' articolo !</strong>
</div>";
}
}
if(isset($_GET['edit_articolo_id']))
{
$id = $_GET['edit_articolo_id'];
extract($crud->getID_articoli($id));
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<div class="container">
<?php
if(isset($msg))
{
echo $msg;
}
?>
</div>
<div class="clearfix"></div>
<div class="container">
<h1>Modifica articolo</h1><hr>
<i class="glyphicon glyphicon-backward"></i> Torna alla lista articoli
<br/><br/>
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>Data</td>
<td><input type='text' name='data' class='form-control' value="<?php echo $data; ?>" required></td>
</tr>
<tr>
<td>Autore</td>
<td><input type='text' name='autore' class='form-control' value="<?php echo $autore; ?>" required></td>
</tr>
<tr>
<td>Titolo</td>
<td><textarea name='titolo' rows="5" maxlength="100" class='form-control' required><?php echo $titolo; ?></textarea></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="glyphicon glyphicon-edit"></span> Aggiorna articolo
</button>
<i class="glyphicon glyphicon-backward"></i> CANCEL
</td>
</tr>
</table>
</form>
</div>
<?php include_once 'footer.php'; ?>
Thanks for support.
The title may be a bit misleading as I didn't know exactly how to phrase it. I have a PHP page which shows a listing of records with search filters. The code is given below. The page gets displayed correctly but in one of the columns I get following database error for MS SQL
Error Number: 22018
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'Marie Stopes International' to data type int.
SELECT * FROM ctbl_mas_funder WHERE flag = 0 AND id = 'Marie Stopes International'
Filename: C:\inetpub\wwwroot\FMS\system\database\DB_driver.php
Line Number: 330
I think its because instead of pulling an ID for query it is pulling the data from some other column, but cannot quite put my finger on what is causing this.
The code for the page is as below.
<link href="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.css" rel="stylesheet" />
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/uniform/jquery.uniform.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.columnFilter.js"></script>
<script src="<?php echo base_url(); ?>assets/js/app.js"></script><!-- Core js functions -->
<script src="<?php echo base_url(); ?>assets/js/pages/data-tables.js"></script><!-- Init plugins only for page -->
<div class="wrapper">
<div class="crumb">
<ul class="breadcrumb">
<li><i class="icon16 i-home-4"></i>Home</li>
<li>Manage Funding</li>
<li class="active">
<?=$title ?>
</li>
</ul>
</div>
<div class="container-fluid">
<div id="heading" class="page-header">
<h1><i class="icon20 i-table-2"></i>
<?=$title ?>
</h1>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="icon"><i class="icon20 i-table"></i></div>
<h4>
<?=$title ?>
</h4>
<div class="flexigrid" style="width: 100%;">
<div class="tDiv">
<div class="tDiv2">
<?php
$f_manange_funding=explode(',', $this->session->userdata('f_manange_funding'));
if (in_array('6', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Word" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_word'"><div><span class="word" style="padding-left: 20px;">Word</span></div></div>
<div class="btnseparator"></div>-->
<?php }
if (in_array('5', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Excel" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_excel'"><div><span class="excel" style="padding-left: 20px;">Excel</span></div></div> <div class="btnseparator"></div> -->
<?php }
if (in_array('7', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Download Pdf" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_pdf'"><div><span class="pdf" style="padding-left: 20px;">Pdf</span></div></div>
<div class="btnseparator"></div>-->
<?php }
if (in_array('8', $m_projects) or $this->session->userdata('user_type')=='admin')
{?>
<!--<div class="fbutton tip" title="Print" onclick="javascript:window.open('<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/print_page', 'mywindow', 'location=1,status=1,scrollbars=1, width=600,height=600')"><div><span class="print" style="padding-left: 20px;">Print</span></div></div>
<div class="btnseparator"></div>-->
<?php } ?>
<div class="fbutton tip" title="Help" >
<div><span class="help_ic" style="padding-left: 20px;">Help</span></div>
</div>
</div>
<div style="clear:both"></div>
</div>
</div>
</div>
<!-- End .panel-heading -->
<div class="panel-body">
<?php
//flash messages
if($this->session->flashdata('feedback'))
{
echo '<div class="alert alert-success">';
echo '<a class="close" data-dismiss="alert">×</a>';
echo $this->session->flashdata('feedback');
echo '</div>';
} ?>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-hover" id="dataTable">
<thead>
<tr>
<th > Funding Hierarchy</th>
<th >Project Code </th>
<th >Country of implementation</th>
<th >Source of funding </th>
<th > Primary Donor</th>
<th > Contracting donor</th>
<th > Contracting recipient </th>
<th >Short Title</th>
<th >Funding status</th>
<th width="8%">Action</th>
</tr>
</thead>
<tbody>
<?php
$str="";
if(($funding_mechanism !="") or ($primary_donor !="") or ($contracting_donor_funder_code !="" ) or ($funder_code_primary !="" ) or ($funding_manager !="" ) ){
if($fundingstatus !="")
{
$str=$str."ctbl_contract_funding_info.fundingstatus ='$fundingstatus'";
}
if($project_code !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.project_code='$project_code'";
}
if($fundingHierarchy !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.fundingHierarchy='$fundingHierarchy'";
}
if($funding_mechanism !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_fin.funding_mechanism='$funding_mechanism'";
}
if($subfunding_title !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.subfunding_title='$subfunding_title'";
}
if($primary_donor !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.primary_donor='$primary_donor'";
}
if($source_funding !="")
{
if($str!="")
{
$str=$str." and ";
}
// $str=$str."ctbl_contract_funding_info.source_funding='$source_funding'";
$str=$str."ctbl_contract_funding_info.source_funding REGEXP '.*;s:[0-9]+:\"$source_funding\".*'";
}
if($contracting_donor !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_donor='$contracting_donor'";
}
if($contracting_recipient !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_recipient='$contracting_recipient'";
}
if($funder_code_source !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.funder_code_source='$funder_code_source'";
}
if($contracting_donor_funder_code !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.contracting_donor_funder_code='$contracting_donor_funder_code'";
}
if($funder_code_primary !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_info.funder_code_primary='$funder_code_primary'";
}
if($country_implemention !="")
{
if($str!="")
{
$str=$str." and ";
}
// $str=$str."ctbl_contract_funding_info.country_implemention='$country_implemention'";
$str=$str."ctbl_contract_funding_info.country_implemention REGEXP '.*;s:[0-9]+:\"$country_implemention\".*'";
}
if($funding_manager !="")
{
if($str!="")
{
$str=$str." and ";
}
$str=$str."ctbl_contract_funding_mgmt.funding_manager='$funding_manager'";
}
$query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id ,ctbl_contract_workflow.parent_type
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_funding_info.contract_id
LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id
WHERE '.$str.'
ORDER BY ctbl_contract_workflow.id asc');
$total=$query_search->num_rows() ;
foreach($query_search->result_array() as $row_search)
{
echo '<tr>';
echo '<td>'.$row_search['fundingHierarchy'].'</td>';
if($row_search['project_code']!='')
{
echo '<td>E'.$row_search['project_code'].'</td>';
}
else
{
echo '<td></td>';
}
if($row_search['country_implemention']=='')
{
$data['list1'] = $this->common_model->get_by_id('id',$country_implemention,'ctbl_mas_country');
echo '<td>'.$data['list1'][0]['name'].'</td>';
}
else
{
?>
<td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['country_implemention'])!='')
{
$implementing_agency= unserialize($row_search['country_implemention']);
}
else
{
$implementing_agency=array();
}
if (in_array($row['id'], $implementing_agency))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
}
if($source_funding!='' || $source_funding!='Array')
{
$data['list'] = $this->common_model->get_by_id('id',$source_funding,'ctbl_mas_funder');
echo '<td>'.$data['list'][0]['name'].'</td>';
}
else
{
?>
<td><?php
$query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['source_funding'])!='')
{
$source_funding= unserialize($row_search['source_funding']);
}
else
{
$source_funding=array();
}
if (in_array($row['id']."-".$row['code'], $source_funding))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
}
$data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
echo '<td>'.$data['list1'][0]['name'].'</td>';
?>
<td><?php
$c=explode("-",$row_search['contracting_donor']);
if(is_array($c) && $c[1]!='')
{
$data['list2'] = $this->common_model->get_by_id('id',$c[0],'ctbl_mas_funder');
echo $data['list2'][0]['name'];
}
else
{
echo $row_search['contracting_donor'];
}
?></td>
<td><?php
if($row_search['contracting_recipient']!='Select')
{
if($row_search['type_recipient']!='Select')
{
if($row_search['type_recipient']=='MSI partnership')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_entities');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='External partner')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream - project donor')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
}}?></td>
<?php
echo '<td>'.$row_search['subfunding_title'].'</td>';
echo '<td>'.$row_search['fundingstatus'].'</td>';
echo '<td class="crud-actions">';
if($row_search['main_id']==$row_search['id1'])
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
else
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
//echo '<i class="icon16 i-pencil"></i> ';
//echo '<a class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'" title="Remove"><i class="icon16 i-remove"></i></a>';
echo ' </td></tr>';
}
/// to show no records
/// to show no records
}else{
//// searching results query ends here
//// searching results query echo ends here
$query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id,ctbl_contract_workflow.parent_type
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id
ORDER BY ctbl_contract_workflow.id asc');
$total=$query_search->num_rows() ;
foreach($query_search->result_array() as $row_search)
{
echo '<tr>';
echo '<td>'.$row_search['fundingHierarchy'].'</td>';
if($row_search['project_code']!='')
{
echo '<td>E'.$row_search['project_code'].'</td>';
}
else
{
echo '<td>'.$row_search['project_code'].'</td>';
}
?>
<td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['country_implemention'])!='')
{
$implementing_agency= unserialize($row_search['country_implemention']);
}
else
{
$implementing_agency=array();
}
if (in_array($row['id'], $implementing_agency))
{
echo $row['name']."<br>";
}
}
}
?></td>
<td><?php
$query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
$total=$query->num_rows() ;
if($total>0)
{
foreach ($query->result_array() as $row)
{
if(unserialize($row_search['source_funding'])!='')
{
$source_funding= unserialize($row_search['source_funding']);
}
else
{
$source_funding=array();
}
if (in_array($row['id']."-".$row['code'], $source_funding))
{
echo $row['name']."<br>";
}
}
}
?></td>
<?php
$data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
echo '<td>'.$data['list1'][0]['name'].'</td>';
?>
<td>
<?php
//echo $row['contracting_donor'];
$code_id =explode("-",$row_search['contracting_donor']);
if(trim($row_search['fundingHierarchy']) == 'Donor Funding'){
$data['donor_name'] = $this->common_model->get_by_id('id',$code_id[0],'ctbl_mas_funder');
echo $data['donor_name'][0]['name'];
}
else{
echo $row_search['contracting_donor'];
}
?>
</td>
<td>
<?php
if($row_search['contracting_recipient']!='Select')
{
if($row_search['type_recipient']!='Select')
{
if($row_search['type_recipient']=='MSI partnership')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_partnership');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='External partner')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
else if($row_search['type_recipient']=='MSI funding stream - project donor')
{
if($row_search['contracting_recipient']!='Select')
{
$data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
$d=$data['list_cd'][0]['name'] ;
}
else
{
$d='';
}
?>
<?php echo $d ; ?>
<?php
}
}
}
?>
</td>
<?php
echo '<td>'.$row_search['subfunding_title'].'</td>';
echo '<td>'.$row_search['fundingstatus'].'</td>';
echo '<td class="crud-actions">';
if($row_search['main_id']==$row_search['id1'])
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
else
{
echo '<i class="icon16 i-zoom-in"></i> ';
}
//echo '<i class="icon16 i-pencil"></i> ';
//echo '<a class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'" title="Remove"><i class="icon16 i-remove"></i></a>';
echo ' </td></tr>';
}
}
?>
</tbody>
</table>
<?php require_once('help.php');?>
<div class="form-group">
<div class="col-lg-offset-4">
<div class="pad-left15"> <i class="i-cancel"></i>Cancel/Back </div>
</div>
</div>
</div>
<!-- End .panel-body -->
</div>
<!-- End .widget -->
</div>
<!-- End .col-lg-12 -->
</div>
<!-- End .row-fluid -->
</div>
<!-- End .container-fluid -->
</div>
I have to add delete button into each section of the table.
<?php
$link = mysqli_connect("localhost", "root", "", "demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$contact = mysqli_real_escape_string($link, $_POST['contact']);
$sql = "INSERT INTO persons (name, email, contact) VALUES ('$name', '$email', '$contact')";
if(mysqli_query($link, $sql)){
echo "<h2>Records added successfully.The new updated records are...</h2><br>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$selectData = "select * from persons";
$res = mysqli_query($link, $selectData);
?>
<table border="2">
<tr>
<th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
</tr>
</table>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<?php
}
}
?>
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td><a onClick="delTopicContentRow('<?php echo $row['id'] ?>')"><img src="../admin_assets/images/delete.png"></a></td>
<?php
}
}
The delete javascript function
function delTopicContentRow(topic_content_id)
{
var confirm_delete = confirm("Do you want to delete topic content ?");
if (confirm_delete === true)
{
$(".deleteall"+topic_content_id).addClass("deletealloncomplete");
$.ajax({
type:'POST',
url:'delRow.php',
data:{topic_content_id:topic_content_id},
success:function(data)
{
if(data == 1)
{
$(".deletealloncomplete").fadeOut("slow");
setTimeout(function(){
window.location.reload(1);
}, 2000);
}
else
{
alert("Try again.!");
}
}
});
}
}
You should start with HTML basic http://www.w3schools.com/html/html_basic.asp
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td>Delete item
</td>
<?php
}
}
//This last statement passes the id of the function to delete
function delete(){
if(isset($_GET['delete'])){
$id=$_GET['delete'];
$sql="DELETE FROM `trial` WHERE `id` ='$id'";
$stmt=$this->conn->prepare($sql);
$stmt->execute();
}
}
?>
I have a project that I'm doing now and my project consist of CRUD functions and it works fine. My project also has its pagination and search function.
The pagination is working but the search function does not seem to work. What I want is, a search where I can search anything within my table like I want to search all the fields or maybe a live search. Can somebody please help me.
Dbconfig.php
<?php
$db_host = 'localhost'
$db_username = 'root';
$db_password = '';
$db_name = 'survey';
try{
$db_con = new PDO("mysql:host={$db_host};dbname={$dbname}",db_username,$db_password);
}
catch(PDOException $exception{
echo $exception->getMessage();
}
include_once 'class.paginate.php'
$paginate = new paginate($DB_con);
?>
Class.paging.php
<?php
class paginate{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
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 echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['province']; ?></td>
<!-- <td>visit</td> -->
</tr>
<?php
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
public function search($query)
{
$stmt=$this->db->prepare($query);
$query = '%' . $query . '%';
$stmt->bindparam('query', $query, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
if(empty($result) or $result == false)
return array();
else
return $result;
}
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)
{
?><tr><td colspan="4" align="center"><?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 "<a href='".$self."?page_no=1'>First</a> ";
echo "<a href='".$self."?page_no=".$previous."'>Previous</a> ";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a> </strong> ";
}
else
{
echo "<a href='".$self."?page_no=".$i."'>".$i."</a> ";
}
} if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<a href='".$self."?page_no=".$next."'>Next</a> ";
echo "<a href='".$self."?page_no=".$total_no_of_pages."'>Last</a> ";
}
?></td></tr><?php
}
}
}
Index.php
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="Bootstrap/bootstrap.css">
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
<body>
<div id="search-wrapper">
<form name="search" method="GET" action="">
<table id="search" class="table-responsive">
<tr>
<input name="var1" type="text" id="search-box">
<button id="submit" name="submit" type="submit">Search</button>
</tr>
</table>
<?php
$query = "SELECT * FROM `login` WHERE (`username` like :query or `password` like :query) ";
$paginate->search($query)
?>
</form>
</div>
<div id="table-wrapper" class="table-responsive">
<table id="data" class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Actions</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM login";
$records_per_page=20;
$newquery = $paginate->paging($query,$records_per_page);
$paginate->dataview($newquery);
$paginate->paginglink($query,$records_per_page);
?>
</table>
</div>
<div id="footer">
cleartuts.blogspot.com
</div>
</body>
</html>