PHP search with class and public function - php

How can i run search function? i cant display the any result with this code and i don't know how to handle this class and function.. i'm a newbie with this kind of code..please review my codes....thanks!!!
<?php
$txtsearch = $_POST['txtsearch'];
class BlogController{
public function search($txtsearch){
$mysqli = new mysqli("localhost","root","","sample_db");
$display_query = "SELECT * FROM `tb_blogs` WHERE id='$txtsearch' ";
$result = $mysqli->query($display_query);
}
}
if(isset($_POST["btnsearch"])){
echo BlogController::search();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
Create New Blog<br>
<form action="function.php" method="post">
<input text-align:right type="text" name="txtsearch" placeholder="search">
<input type="submit" name="btnsearch" value="Search">
<table border="1" width="60%" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th width="5%">ID</th>
<th width="20%">Title</th>
<th width="20%">Author</th>
<th width="40%">Content</th>
<th width="15%">Action</th>
</tr>
</thead>
<tbody>
<?php while($blog = $result->fetch_object()): ?>
<tr>
<td><?php echo $blog->id?></td>
<td><?php echo $blog->title?></td>
<td><?php echo $blog->author?></td>
<td><?php echo $blog->content?></td>
<td>
Edit
<a class ="btn_del" href="delete.php?blog_id=<?php echo $blog->id;?>">Delete</a>
</td>
</tr>
<?php endwhile?>
</tbody>
</table>
<script type="text/javascript">
$(function(){
$(".remove-btn").on('click',function(){
var is_confirm = confirm("Do you want to delete record?");
if(is_confirm){
window.location = $(this).attr('href');
}
return false;
});
});
</script>
</form>
</body>
</html>

Try this :
class BlogController{
public $txtsearch;
function __construct($search) {
$this->$txtsearch = $search;
}
public function search(){
$mysqli = new mysqli("localhost","root","","sample_db");
$display_query = "SELECT * FROM `tb_blogs` WHERE id='$this->$txtsearch' ";
$result = $mysqli->query($display_query);
return $result;
}
}
Create an instance and then call the function search
$myclass = new BlogController($_POST['txtsearch']); //pass value to the construct function
print_r($myclass->search()); // will return an array

Related

How to transfer this into codeigniter framework?

Everthing is working I've tested it, my only problem is that how to transfer into Codeigniter.. please someone help and explain if can.. I have to add this on my school project but in Codeigniter framework. I'm newbie on Codeigniter and I want to learn more.
This is my "print.php"
<?php
require('fpdf/fpdf.php');
if(isset($_POST["from_date"], $_POST["to_date"]))
{
$connect = mysqli_connect("localhost", "root", "", "datedate");
$output = '';
$query = "SELECT * FROM tbl_order WHERE order_date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."' ";
$result = mysqli_query($connect, $query);
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
$pdf->Cell(50,10,'Date:'.date('d-m-Y').'',0,"R");
$pdf->Ln(15);
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,10,'USERS',1,1,"C");
$pdf->SetFont('Arial','B',12);
$pdf->Cell(10,8,'No.',1);
$pdf->Cell(45,8,'First Name',1);
$pdf->Cell(45,8,'Middle Name',1);
$pdf->Cell(45,8,'Last Name',1);
$pdf->Cell(45,8,'Birth Date',1);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)){
$no=$no+1;
$pdf->Ln(8);
$pdf->SetFont('Arial','',12);
$pdf->Cell(10,8,$no,1);
$pdf->Cell(45,8,$row['order_customer_name'],1);
$pdf->Cell(45,8,$row['order_item'],1,0,"C");
$pdf->Cell(45,8,$row['order_value'],1);
$pdf->Cell(45,8,$row['order_date'],1);
}
}
}
$pdf->Output();
?>
this is my "index.php"
<?php
$connect = mysqli_connect("localhost", "root", "", "datedate");
$query = "SELECT * FROM tbl_order ORDER BY order_id asc";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Ajax PHP MySQL Date Range Search using jQuery DatePicker</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<br /><br />
<div class="container" style="width:900px;">
<h2 align="center">Ajax PHP MySQL Date Range Search using jQuery DatePicker</h2>
<h3 align="center">Order Data</h3><br />
<div class="col-md-3">
<input type="text" name="from_date" id="from_date" class="form-control" placeholder="From Date" />
</div>
<div class="col-md-3">
<input type="text" name="to_date" id="to_date" class="form-control" placeholder="To Date" />
</div>
<div class="col-md-5">
<input type="button" name="filter" id="filter" value="Filter" class="btn btn-info" />
</div>
<div style="clear:both"></div>
<br />
<div id="order_table">
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="30%">Customer Name</th>
<th width="43%">Item</th>
<th width="10%">Value</th>
<th width="12%">Order Date</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["order_id"]; ?></td>
<td><?php echo $row["order_customer_name"]; ?></td>
<td><?php echo $row["order_item"]; ?></td>
<td>$ <?php echo $row["order_value"]; ?></td>
<td><?php echo $row["order_date"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '')
{
$.ajax({
url:"print.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#order_table').html(data);
}
});
}
else
{
alert("Please Select Date");
}
});
});
</script>
I think your code is currently in pure PHP, and you want to code it in CodeIgniter, right?
If so, please take a look at it's document. Or sample here: This link
I suggest your code will like this:
In models/order.php
Class Order {
public function detail($id) {
// Get and return your data here
}
}
In controllers/index.php
Class IndexController {
public function index() {
// load model here
$data = ... // call to Order->detail
// Return view here
}
}
In views/index.php
// Render your view, form here
In controllers/print.php
Class Print {
public function index() {
// Do your code after submit here
}
}
Hope this can help you.

Navigate to the specific link is not working according to the database status value in php and sqlite

when i clicked the link it does not redirect to the page that specified.i have the sqlite database and there is a database column for status(added,updated like wise).i want to redirect to the page when i clicked the status link in my link.php.Now Object not found error is getting.
this is my link.php code below.
Please can you help me guys.
<?php
// Includs database connection
include "db_connect.php";
// Makes query with rowid
$query = "SELECT rowid, * FROM registration";
// Run the query and set query result in $result
// Here $db comes from "db_connection.php"
$result = $db->query($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Data List</title>
<script>
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no')
}
</script>
</head>
<body>
<br>
<br>
<!-- Add New-->
<table>
<tbody>
<tr>
<th style="">status</th>
<th style="">submitted</th>
<th style="">department</th>
<th style="">head</th>
<th style="">title</th>
<th style="">applicant</th>
<th style="">date</th>
</tr>
<?php while($row = $result->fetchArray()) {?>
<!--<tr class="table-row" data-href="update.php?id=<?php echo $row['rowid'];?>" data-target="_blank">-->
<tr>
<td>
</td>
<td>view
<?php
if ($row['rowid'] == "added"){
echo "<a href='updated.php" . $row['submitted'] . "updated'> </a>";
} else {
echo "<a href='next.php" . $row['submitted'] . "next'> </a>";
}
?>
</td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['submitted'];?></td>
<td><?php echo $row['department'];?></td>
<td><?php echo $row['head'];?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['applicant'];?></td>
<td><?php echo $row['date'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
var tableRows = document.getElementsByClassName('table-row');
for (var i = 0, ln = tableRows.length; i < ln; i++) {
tableRows[i].addEventListener('click', function() {
window.open(this.getAttribute('data-href'), this.getAttribute('data-target'));
});
}
</script>
<br>
<br>
</body>
</html>

Pagination without LIMIT

Few weaks ago I started learning PHP and MVC. So I started making simple project and got stuck at pagination problem.. I need help to find the best solution for pagination in my website. In the most pagination examples I have found, pagination is made with SQL SELECT LIMIT, but in my case it seems not very logical.
I have BooksController:
<?php
require 'Database/Connection.php';
require 'Database/QueryBuilder.php';
require 'Database/Book.php';
class BooksController
{
public function index()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$books = $books->selectAll('books_table');
require 'views/listofbooks.view.php';
}
public function about()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$bookId = $_REQUEST['id'];
$book = $books->selectByID('books_table', $bookId);
require 'views/aboutbook.view.php';
}
public function search()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$text = $_REQUEST['search'];
$books = $books->search('books_table', $text);
require 'views/listofbooks.view.php';
}
}
And ListOfBooks View:
<!DOCTYPE html>
<html>
<head>
<title>Books</title>
<link rel="stylesheet" type="text/css" href="styles/styles.css">
<script src="styles/sorttable.js"></script>
<script>sorttable.sort_alpha = function(a,b) { return a[0].localeCompare(b[0], 'lt'); }</script>
</head>
<body>
<form action="/search" name="search" method="GET">
<input type="text" id="textbox" size="30" name="search" placeholder="Text" required />
<input type="submit" id="button">
</form>
<table class="sortable">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Year</th>
<th scope="col">Author</th>
<th scope="col">Genre</th>
</tr>
</thead>
<tbody>
<?php foreach($books as $book) : ?>
<tr>
<td data-label="Title"><?php echo $book->Title; ?></td>
<td data-label="Year"><?php echo $book->Year; ?></td>
<td data-label="Author(s)"><?php echo $book->Author; ?></td>
<td data-label="Genre"><?php echo $book->Genre; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
Should I change my recreate my BooksController or there is any other solution?
As you haven't mentioned that it has to be done in PHP, in my opinion, the easiest solution to your problem is a simple DataTable plugin. It is javascript (jQuery to be exact, but that is a javascript framework) and it will ease the usage of your server (there will not be one query for each of pagination pages, but one query and javascript will take care of pagination), but increase the use of client's PC. Also, you have filtering, search etc. available for dataTables, but here is the easiest to start with: https://datatables.net/examples/basic_init/zero_configuration.html

HTML Table with inline PHP

I want to create a HTML Table, but the rows should be generated from the values from a mysql database. The problem is that I want to have a boolean box where the user can mark it, and then press a button to update the table in the database. How do I do such a thing ?
Code so far:
<?php
session_start();
require("connectToEvent_log.php");
$connectToEvent = connect2db();
$uid = '2'; // for the filnal version: #$_SESSION['uid'];
$view_event = "SELECT * FROM event_log WHERE uid = $uid";
$view_event_query = mysqli_query($connectToEvent, $view_event);
$row = mysqli_num_rows($view_event_query);
$print = mysqli_fetch_array($view_event_query);
?>
<html>
<head>
<title>Events</title>
</head>
<body>
<form action="viewEvents.php" method="POST">
<table border = '1'>
<tr>
<?php
while($row != 0){
echo "<td>".$print['hours']."</td>";
echo "<td>".$print['date']."</td>";
}
?>
</tr>
</table>
<form/>
</form>
</body>
</html>
You can easily iterate over the result from the mysqli_fetch_array function to create the table rows. Creating a checkbox markup is done easily, i assume that the table has a primary key id and the column, that stores the checkbox value (0 or 1) is called checkbox.
<?php
session_start();
require("connectToEvent_log.php");
$connectToEvent = connect2db();
$uid = '2'; // for the filnal version: #$_SESSION['uid'];
$view_event = "SELECT * FROM event_log WHERE uid = $uid";
$view_event_query = mysqli_query($connectToEvent, $view_event);
$num_rows = mysqli_num_rows($view_event_query);
$rows = mysqli_fetch_array($view_event_query);
?>
<html>
<head>
<title>Events</title>
</head>
<body>
<form action="viewEvents.php" method="POST">
<table border="1">
<thead>
<tr>
<td>Id</td>
<td>Date</td>
<td>Hours</td>
<td>Checkbox</td>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < count($num_rows); $i++) {
?>
<tr>
<td><?php print $rows[$i]["eid"]; ?></td>
<td><?php print $rows[$i]["date"]; ?></td>
<td><?php print $rows[$i]["hours"]; ?></td>
<td><input type="checkbox" name="row[<?php $rows[$i]["eid"]?>][checkbox]" value="1" <?php if ($rows[$i]["accepted"]) print ' checked="checked"'; ?>/></td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" />
</form>
</body>
</html>

Call to undefined function ProdTotal()

I am trying to call function ProdTotal to get the total quantity order and total amount ordered for a product and load it in the table in the Display Function. I am getting the error: Call to undefined function ProdTotal();
<?php
Class CarsClass {
private $user = 'php06';
private $pwd = 'php06';
private $dbConn;
function __construct($db='classicmodels') {
//Create connection to MySQL database requested, if blank just connect up.
$this->dbConn = new mysqli('localhost', $this->user, $this->pwd, $db);
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select count(*) as 'custcount' from customers";
$result = $this->dbConn->query($query);
$row = $result->fetch_assoc();
$custCount = $row['custcount'];
print "Connected to DB $db as user $this->user<br><br> Number of Rows $custCount<br><br>";
}
function __destruct(){
mysqli_close();
Print "DB closed by user <br><br>";
}
function header(){
echo "Midterm Exam Script 2 Header<br><br>";
}
function display(){
$totqty = 0;
$totamt = 0;
//get row from WB_Resident Table
$query = "select productCode,productName,productDescription,quantityInStock,buyprice,MSRP from products";
$result = $this->dbConn->query($query);
?>
<table id="midterm2">
<tr>
<th colspan="13">Product Database Table</th>
</tr>
<tr>
<th width="2%">productCode</th>
<th width="10%">productName</th>
<th width="10%">productDescription</th>
<th width="10%">quantity in stock</th>
<th width="10%">buyPrice</th>
<th width="2%">MSRP</th>
<th width="10%">Total Qty Ordered</th>
<th width="10%">Total $ Ordered</th>
</tr>
<?php
while($row = $result->fetch_assoc()):
$producta = $row["productCode"];
ProdTotal($producta);
?>
<tr>
<td>
<?php echo $row["productCode"]; ?>
</td>
<td>
<?php echo $row["productName"];?>
</td>
<td>
<?php echo $row["productDescription"]; ?>
</td>
<td>
<?php echo $row["Qty In Stock"]; ?>
</td>
<td>
<?php echo $row["Buy Price"]; ?>
</td>
<td>
<?php echo $row["MSRP"]; ?>
</td>
<td>
<?php echo $totqty; ?>
</td>
<td>
<?php echo $totamt; ?>
</td>
</tr>
<?php
endwhile;
?>
</table>
<?php
}
function footer(){
echo "Midterm Exam Script 3 Footer<br><br>";
}
function ProdTotal($product){
echo "product code entered = $product <br><br>";
$query = "select RTRIM(productCode) as productt, quantityOrdered, priceEach from orderdetails order by productt";
$result = $this->dbConn->query($query);
while($row = $result->fetch_assoc()){
if ($row["productt"] == $product){
echo $row;
$total = $row["quantityOrdered"] * $row["priceEach"];
$totqty = $totqty + $row["quantityOrdered"];
$totamt = $totamt + $total;
echo totqty;
echo totamt;
return array($totqty, $totamt);
}
}
}
}
?>
This script is executed to call class.
<html>
<head>
<title>Midterm2 Script 3</title>
<link rel="stylesheet" type="text/css" href="midterm2.css" />
</head>
<body>
<?php
require 'CarsClass3a.php';
$obj1= new CarsClass('classicmodels');
$obj1->header();
$obj1->display();
$obj1->footer();
?>
</body>
</html>
In display(), change the call to ProdTotal() to
list($totqty, $totamt) = $this->ProdTotal($producta);

Categories