How to create search function in PHP/MySQL - php

I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
I want to search in my table, this works fine so far but when I search on A for example and I click my pagination page 1 it rebuilds the page with all te records from the database.
I tried to add a parameter to the url upon button click but upon creating this button with link the searchValue is still null.
This question is not about my sql query. It's about searching with pagination
Could anyone help me fix this?

Related

Trying to insert data into another table on button click

I have a form that takes data from the db and displays it onto a table using a foreach loop. After reviewing the data, I want to insert that data into another table that is already in the DB via a button click. I have tried multiple ways, but can't seem to get it to work. Any help would be great. Thanks!
my index.php page
<div class='container-fluid'>
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Division</th>
<th>Sponsor Name</th>
<th>Project Start</th>
<th>Project End</th>
<th>Funding Type</th>
<th>Yes/No</th>
<th>Proper Type If No</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<!-- //Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<form action="process.php" method="POST">
<td>Yes<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes" id="rdYes" onclick="disable('<?php echo $rdDrop;?>')" checked/><br />
No<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/></td>
<input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
<td>
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $rdDrop;?>" data-toggle="dropdown" disabled>Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="flex-container" id="saveBtn">
<div class="dSave"><button type="submit" class="btn btn-success" name="submit" id="Save">Save</button></div>
<div><button type="submit" class="btn btn-success" name="Complete" id="Complete">Complete and Save</button></div>
</div>
</form>
</div>
</div>
</div>
</div>
My process.php page
if(isset($_POST['submit'])) {
$count = count($_POST['fpID']);
for($i=0; $i < count; $i++) {
$mssql = "INSERT into some_table (FP_id, Short_Title, PI, Department, Division, Sponsor, Date_Project_Start, Date_Project_End, Funding_Type, Yes_No, Proper_Type_If_No, Comment, Complete_Flag)
VALUES ('"
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
$mssql .= $_POST['Short Title'][$i] . "','";
$mssql .= $_POST['PI'][$i] . "','";
$mssql .= $_POST['Department'][$i] . "','";
$mssql .= $_POST['Division'][$i] . "','";
$mssql .= $_POST['Sponsor Name'][$i] . "','";
$mssql .= $_POST['Date_Project_Start'][$i] . "','";
$mssql .= $_POST['Date_Project_End'][$i] . "','";
$mssql .= $_POST['Funding Type'][$i] . "','";
$mssql .= $_POST['Yes_No'][$i] . "','";
$mssql .= $_POST['Proper_Type_If_No'][$i] . "','";
$mssql .= $_POST['Comment][$i] . "','";
$mssql .= 'No' "')";
}
header("Location: index.php");
}
?>
This:
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
should be
$mssql .= $_POST[$i]['Funding Proposal ID'] . "','";
Hope it helps
Looks like you only build the SQL query into the $mssql variable, but you don't do anything with it. You should send the variable content to the database object, like
$result = db::$conn->query($mssql);
This way the database should get filled. Put the header statement outside the for loop (at the bottom of the script).

Codeigniter Pagination directs me to 'about:blank#blocked'

Codeigniter Pagination doesnt work, the URL changes to 'about:blank#blocked'.
I'm fairly new at php frameworks and codeigniter. I tried to read the documentation but I don't seem to see the problem.
Controller:
class Listing extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
//Get Total Records Count
$this->db->select("*");
$this->db->from("cities");
if (!empty($_GET['cityFilter'])) {
$this->db->like('city_name', $_GET['cityFilter']);
}
$cityRecordsCount = $this->db->get();
$totalRecords = $cityRecordsCount->num_rows();
$limit = 10;
if (!empty($_GET['cityFilter'])) {
$config["base_url"] = base_url('Listing/index?cityFilter=' . $_GET['cityFilter']);
} else {
$config["base_url"] = base_url('Listing/index?cityFilter=');
}
$config["total_rows"] = $totalRecords;
$config["per_page"] = $limit;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['num_links'] = 2;
$config['cur_tag_open'] = ' <li class="active"><a>';
$config['cur_tag_close'] = '</a></li>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$str_links = $this->pagination->create_links();
$links = explode(' ', $str_links);
$offset = 0;
if (!empty($_GET['per_page'])) {
$pageNo = $_GET['per_page'];
$offset = ($pageNo - 1) * $limit;
}
//Get actual result from all records with pagination
$this->db->select("*");
$this->db->from("cities");
if (!empty($_GET['cityFilter'])) {
$this->db->like('city_name', $_GET['cityFilter']);
}
$this->db->limit($limit, $offset);
$cityRecords = $this->db->get();
$this->load->view('listCities', array(
'totalResult' => $totalRecords,
'results' => $cityRecords->result(),
'links' => $links
));
}
}
HTML
<div id="container">
<div class="row">
<div class="col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2 col-sm-12">
<h1>Pagination With Search.</h1>
<p>A demo for Codeigniter 2.X framework</p>
<br>
<form action="" method="GET">
<div class="input-group pull-right">
<input type="text" class="form-control" placeholder="Search For City"
name="cityFilter" value="<?php
if (!empty($_GET['cityFilter'])) {
echo $_GET['cityFilter'];
}
?>">
<span class="input-group-btn">
<button type="submit" class="btn btn-success"><i class="fa fa-search"></i> Search</button>
</span>
</div>
</form>
<table class="table table-bordered table-hover" border="1">
<thead>
<tr>
<th>#</th>
<th>City Name</th>
<th>City State</th>
</tr>
</thead>
<tbody>
<?php
foreach ($results as $o) {
?>
<tr>
<td><?php echo $o->city_id; ?></td>
<td><?php echo $o->city_name; ?></td>
<td><?php echo $o->city_state; ?></td>
</tr>
<?php }
?>
</tbody>
<tfoot>
</tfoot>
</table>
<ul class="pagination pull-right">
<!-- Show pagination links -->
<?php
foreach ($links as $link) {
echo "<li>" . $link . "</li>";
}
?>
</ul>
</div>
</div>
</div>
It shows a blank page and the URL changes to 'about:blank#blocked'. What am I doing wrong?

How to search with PHP/MySQL using Pagination

I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
The getDataAsArray function is like this:
public function getDataAsArray($myQuery){
$this->connection = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, 'portal');
$query = mysqli_query($this->connection, $myQuery);
$results = array();
while($line = mysqli_fetch_array($query)){
$results[] = $line;
}
return $results;
}
I know I should use :[name] or something to set a parameter in my query,the question is not about that and ofcourse I should use a prepared statment. Will do that later.
The question:
I wrote the code to search in the db records. I works fine but when I search it creates a new pagination with all the new records and when I click on the second page. The page builds the pagination with all the records from the database so I won't use it's searchValue anymore.
I think this issue will be solved with a $_GET parameter like search in the URL or Ajax but I don't know how.
Could anyone help me out?

why multiple data doesn't insert in this code

I have a tbl_employee table and tbl_time table, I want to insert multiple data insert for attendance but when I click submit button it's insert only single data . but where is the problem ,help me to find out this..this is insert code
require './db_connect.php';
class Time extends Db_connect {
protected $link;
public function __construct() {
$this->link = $this->database_connection();
}
public function attendance_insert($data) {
extract($data);
$cur_date = date('Y-m-d');
foreach ($time_attendance as $attn_key => $attn_value) {
if ($attn_value == 'P') {
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','P')";
$atten_date=mysqli_query($this->link, $SQL);
} else if ($attn_value == 'A') {
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','A')";
$atten_date=mysqli_query($this->link, $SQL);
}
if ($atten_date) {
$massage = "<div class='alert alert-success text-center'><h5>Attendance insert successfully</h5></div>";
return $massage;
} else {
die('Attendance insert query problem' . mysqli_error($this->link));
}
}
}
}
this is html code
<?php
require_once './time.php';
$obj_time = new Time()
$massage = '';
if (isset($_POST['btn'])) {
$massage = $obj_time->attendance_insert($_POST);
}
$employee_view = $obj_employee->employee_all_view();
?>
<div class="container-fluid">
<div class="row">
<center>
<span style="font-size:1.8em;">Attendance form</span>
</center>
</div>
</div>
<hr/>
<?php echo $massage; ?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-body panel-default">
<div class="well text-center" style="font-size:15px;">
<strong>Date :</strong>
<?php $current_date = date('Y-M-d');
echo $current_date; ?>
</div>
<form class="form-horizontal" method="post">
<table class="table table-striped table-responsive text-center">
<tr>
<td><b>Serial</b></td>
<td><b>Name</b></td>
<td><b>ID</b></td>
<td><b>Attendance</b></td>
</tr>
<?php
$i = 0;
while ($employee_info = mysqli_fetch_assoc($employee_view)) {
extract($employee_info);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $employee_first_name . ' ' . $employee_last_name; ?></td>
<td><?php echo $employee_id; ?></td>
<td>
<input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="P">P
<input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="A">A
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4">
<input type="submit" class="btn btn-primary btn-block" name="btn" value="submit"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
This :
foreach($time_attendance as $attn_key => $attn_value){
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)
VALUES('$attn_key','$cur_date','A')";
$atten_date = mysqli_query($this->link, $SQL);
if ($atten_date) {
$massage = "<div class=''><h5>Attendance insert successfully</h5></div>";
return $massage;
}
}
Literally ask to break the foreach with return $massage; if you query succeed... So yes, you'll only have one record.
Put your return outside the foreach.

Dynamic links for different pages

I want to generate a list of items by using a for loop. The items list will appear under the heading element in a drop down menu.
Here is my code:
<div class="container div_scroll">
<?php if($data->results()){
?>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading ">
<h4 class="panel"> <?php echo $state_decode . ' : ' . count($data -> results()); ?>
</h4>
</div>
<table class="table table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="col-md-1">S.N.</th>
<th class="col-md-1">ENQ No</th>
<th class="col-md-1">ENQ Date</th>
<th class="col-md-3">ENQ Descr</th>
<th class="col-md-1">Tender Ref</th>
<th class="col-md-2">Cust Name</th>
<th class="col-md-2">Project Name </th>
<th class="col-md-1">Action</th>
</tr>
</thead>
<tbody>
<?php
switch($state) {
case "draft" :
$heading = 'Edit';
$url = array('new_eis_form1.php','http://google.com');
$menu = array('Modify','Attach Files','Add Equipment');
$param = '';
$btn = array('btn btn-info','btn btn-info','btn btn-info');
$id_name = 'enq';
break;
case "submit" :
$url = 'http://google.com';
$action = 'View';
$param = '';
break;
break;
case "recvd" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "not_recvd" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "hold" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "wo1_pend" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
case "cancel" :
$url = 'new_eis_form1.php';
$action = 'Edit';
$param = '';
break;
}
foreach ($data -> results() as $test) {
$param = $test -> ENQ_NO;
echo "<tr>
<td class=\"col-md-1\">" . ++$slno . "</td>
<td class=\"col-md-1\">" . $test -> ENQ_NO . "</td>
<td class=\"col-md-1\">" . $test -> ENQ_DATE . "</td>
<td class=\"col-md-3\">" . $test -> ENQ_DESR . "</td>
<td class=\"col-md-1\">" . $test -> TENDER_REF_NO . "</td>
<td class=\"col-md-2\">" . $test -> CUST_NAME . "</td>
<td class=\"col-md-2\">" . $test -> PROJ_NAME . "</td>
<td class=\"col-md-1\"><div class=\"dropdown\">
<button class=\"btn btn-primary dropdown-toggle\" type=\"button\" data-toggle=\"dropdown\">".$heading."
<span class=\"glyphicon glyphicon-th-list\"></span>
<span class=\"caret\"></span></button>
<ul class=\"dropdown-menu pull-right\">".
for($i=0;$i<count($menu);$i++){echo "<li>".oper($menu[$i],$btn[$i])."</li>";
}
"</ul>
</div></td>
<td class=\"col-md-2\"></td>
</tr>";
/* oper() function is in coomon.php; uses two inputs one is btn display label and oter is btn style class */
}
// }
?>
</tbody>
</table>
</div>
</div>
<?php }else{ ?>
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading ">
<h4 class="panel">Alert</h4>
</div>
<div class="panel panel-body">
<h4 class="body panel">There is no enquiry; If you want to add enquiry details please <span class="glyphicon glyphicon-link "><u>click here</u></span> .</h4>
</div>
</div>
</div>
<?php } ?>
</div>
But, the problem is that:
I am unable to concatenate the for-loop here with the string in PHP.
I am using HTML inside PHP and not PHP inside HTML because it destroys bootstrap elements functionality.
<td class=\"col-md-1\"><div class=\"dropdown\"> <button class=\"btn btn-primary dropdown-toggle\" type=\"button\" data-toggle=\"dropdown\">".$heading." <span class=\"glyphicon glyphicon-th-list\"></span> <span class=\"caret\"></span></button> <ul class=\"dropdown-menu pull-right\">". for($i=0;$i<count($menu);$i++){echo "<li>".oper($menu[$i],$btn[$i])."</li>"; } "</ul> </div></td>
Something like that ?
$str = '';
for ($i = 0; $i < count($menu); $i++) {
$str .= '<li>
'. oper($menu[$i],$btn[$i]).'
</li>';
}
$html = '<td class="col-md-1">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle"
type="button" data-toggle="dropdown">'.$heading.'<span class="glyphicon glyphicon-th-list"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu pull-right">
' . $str . '
</ul>
</div>
</td>';

Categories