i have a code for my table pagination.
but now i have a problem. the pagination is showing EVERY page. but i got over 900 pages.
ALSO: i need to use PDO
i want the pagination to work like this:
Image
i dont know how to make this in my already excisting code:
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $oConn->prepare($sql);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $oConn->prepare($query);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
I dont see your table and your query in your question so I will give 2 complete examples tested on my demo site, you need to change it to your own variables.
Solution 1.
Here is the example pagination for items belong to a category, for simple pagination :
$perpage = "3";
//This limit of the page to show on each page
$n_stmt = $pdo->prepare("SELECT * FROM categories");
$n_stmt->execute();
$total_posts = $n_stmt->rowCount();
$total_pages = ceil($total_posts/$perpage);
$page = !empty($_GET['page']) && $_GET['page'] ? (int) $_GET['page'] : 1;
if($page < 1) $page = 1;
if($page > $total_pages) $page = $total_pages;
$limit = ($page - 1) * $perpage;
$pag_limit = 10;
$stmt = $pdo->prepare("SELECT * FROM categories ORDER BY created DESC LIMIT :limit, :perpage");
$stmt->bindValue(":limit",$limit, PDO::PARAM_INT);
$stmt->bindValue(":perpage",$perpage, PDO::PARAM_INT);
$stmt->execute();
while($news = $stmt->fetch(PDO::FETCH_ASSOC)){
// Do what ever you want here
}
And pagination:
<div class="pagination">
<?php if($page >1){?>
First
Preview
<?php } for($i = $page - $pag_limit; $i < $page + $pag_limit + 1; $i++){
if($i > 0 and $i <= $total_pages){
if($i == $page){?>
<?php echo $i;?>
<?php }else{?>
<?php echo $i;?>
<?php
}
}
?>
<?php } ?>
<?php if($page != $total_pages){?>
next
Last
<?php } ?>
</div>
Solution 2.
Here is pagination for search result with your codes, as I said this code works on my demo site you need to change your own variables and its in pdo:
define("ROW_PER_PAGE",10);
//this goes on top of your page
require_once("db.php");
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = htmlspecialchars(strip_tags($_POST["search"]["keyword"]), ENT_QUOTES);
}
$sql = 'SELECT * FROM posts WHERE title LIKE :keyword OR descriptions LIKE :keyword OR subject LIKE :keyword ORDER BY id DESC ';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $pdo->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div class=\"pagination\">";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= "<input type=\"submit\" name=\"page\" value=" . $i . " class=\"btn-page current\" />";
} else {
$per_page_html .= "<input type=\"submit\" name=\"page\" value=" . $i . " class=\"btn-page\"/>";
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $pdo->prepare($query);
$pdo_statement->bindValue(":keyword", "%" . $search_keyword . "%", PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
Your html part with pagination at the bottom and your result in form same as you can see
<form name="frmSearch" action="search/" method="post">
<div class="searchf">
<input type="text" name="search[keyword]" class="field" value="<?php echo $search_keyword; ?>" id="keyword" maxlength="25">
<input type="submit" name="submit" class="searchf-btn" value="Ara">
</div>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<div class="news_box">
<a href="<?php echo htmlspecialchars($row["news_url"]);?>/" title="<?php echo htmlspecialchars($row["title"]);?>">
<div class="title"><h2><?php echo htmlspecialchars($row["title"]);?></h2></div>
<div class="image">
<img src="images/posts/<?php echo htmlspecialchars($row["img"]);?>" alt="<?php echo htmlspecialchars($row["title"]);?>"/></div>
<div class="spot"><?php echo htmlspecialchars($row["subject"]);?></div>
</a>
</div>
<?php
}
}
?>
<div class="cl"> </div>
//Here is pagination
<?php echo $per_page_html; ?>
</form>
I am using seo urls in demo, you need to set htaccess for links, or change pagination links like so : your_page.php?page=$i.
Both tested on my demo site and working, I used some filtering functions you can remove them.
Hello everyone I'm completely new to mysqli and PDO. I tried to convert my msqli code to PDO but I keep getting errors. Can someone please help me? the error is like when i click on post, nothing happens and I searched everywhere how to convert from msqli to pdo and I converted some of them but it still doesn't work
<?php
$databaseHost = 'localhost';
$databaseName = 'test';
$databaseUsername = 'test';
$databasePassword = 'pass';
try {
$dbConn = new PDO("mysql:host={$databaseHost};dbname={$databaseName}", $databaseUsername, $databasePassword);
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
if (isset($_POST['save'])) {
$name = $_POST['name'];
$comment = $_POST['comment'];
$sql = "INSERT INTO comments (name, comment) VALUES ('{$name}', '{$comment}')";
if ($result = $dbConn->query($sql)) {
$id = $link->insert_id;
$saved_comment = '<div class="comment_box">
<span class="delete" data-id="' . $id . '" >delete</span>
<span class="edit" data-id="' . $id . '">edit</span>
<div class="display_name">'. $name .'</div>
<div class="comment_text">'. $comment .'</div>
</div>';
echo $saved_comment;
}else {
echo "Error: ".($dbConn);
}
exit();
}
// delete comment fromd database
if (isset($_GET['delete'])) {
$id = $_GET['id'];
$sql = "DELETE FROM comments WHERE id=" . $id;
($dbConn->prepare($sql));
exit();
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$sql = "UPDATE comments SET name='{$name}', comment='{$comment}' WHERE id=".$id;
if ($dbConn->prepare($sql)) {
$id =($dbConn);
$saved_comment = '<div class="comment_box">
<span class="delete" data-id="' . $id . '" >delete</span>
<span class="edit" data-id="' . $id . '">edit</span>
<div class="display_name">'. $name .'</div>
<div class="comment_text">'. $comment .'</div>
</div>';
echo $saved_comment;
}else {
echo "Error: ". ($dbConn);
}
exit();
}
// Retrieve comments from database
$sql = "SELECT * FROM comments";
$result = $dbConn->prepare($sql);
$comments = '<div id="display_area">';
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$comments .= '<div class="comment_box">
<span class="delete" data-id="' . $row['id'] . '" >delete</span>
<span class="edit" data-id="' . $row['id'] . '">edit</span>
<div class="display_name">'. $row['name'] .'</div>
<div class="comment_text">'. $row['comment'] .'</div>
</div>';
}
$comments .= '</div>';
?>
You used ->prepare(), but did not use ->execute().
Documentation:
http://php.net/manual/en/pdostatement.execute.php
I've added clients in clients.php sown on the following screenshot:
UPDATE: If I add new client from manange_sale page, The List Shows up soon after!
When I try to access them in another page through this function:
function get_client_info($client_id, $term) {
global $db;
$query = "SELECT * from clients WHERE client_id='".$client_id."'";
$result = $db->query($query) or die($db->error);
$row = $result->fetch_array();
return $row[$term];
}
It just does not show any clients list on that page:
Please help me resolve this issue.
Note: Same script is working perfectly on Online host but causing issues in localhost.
Rest of the client class:
<?php
//Notes Class
class Client {
public $full_name;
public $business_title;
public $mobile;
public $phone;
public $address;
public $city;
public $state;
public $zipcode;
public $country;
public $email;
public $price_level;
public $notes;
function get_client_info($client_id, $term) {
global $db;
$query = "SELECT * from clients WHERE client_id='".$client_id."'";
$result = $db->query($query) or die($db->error);
$row = $result->fetch_array();
return $row[$term];
}//get user email ends here.
function add_client($full_name, $business_title, $mobile, $phone, $address, $city, $state, $zipcode, $country, $email, $price_level, $notes) {
global $db;
$query = "SELECT * from clients WHERE full_name='".$full_name."' AND store_id='".$_SESSION['store_id']."'";
$result = $db->query($query) or die($db->error);
$num_rows = $result->num_rows;
if($num_rows > 0) {
return 'A client with same name already exists.';
} else {
$query = "INSERT into clients(client_id, full_name, business_title, mobile, phone, address, city, state, zipcode, country, email, price_level, notes, store_id)
VALUES(NULL, '".$full_name."', '".$business_title."', '".$mobile."', '".$phone."', '".$address."', '".$city."', '".$state."', '".$zipcode."', '".$country."', '".$email."', '".$price_level."', '".$notes."', '".$_SESSION['store_id']."')
";
$result = $db->query($query) or die($db->error);
$_SESSION['cn_id'] = $db->insert_id;
return 'Client added successfuly.';
}
}//add warehouse ends here.
function set_client($client_id) {
global $db;
$query = 'SELECT * from clients WHERE client_id="'.$client_id.'" AND store_id="'.$_SESSION['store_id'].'"';
$result = $db->query($query) or die($db->error);
$row = $result->fetch_array();
extract($row);
$this->full_name = $full_name;
$this->business_title = $business_title;
$this->mobile = $mobile;
$this->phone = $phone;
$this->address = $address;
$this->city = $city;
$this->state = $state;
$this->zipcode = $zipcode;
$this->country = $country;
$this->email = $email;
$this->price_level = $price_level;
$this->notes = $notes;
}//Set Warehouse ends here..
function update_client($client_id, $full_name, $business_title, $mobile, $phone, $address, $city, $state, $zipcode, $country, $email, $price_level, $notes) {
global $db;
$query = 'UPDATE clients SET
full_name = "'.$full_name.'",
business_title = "'.$business_title.'",
mobile = "'.$mobile.'",
phone = "'.$phone.'",
address = "'.$address.'",
city = "'.$city.'",
state = "'.$state.'",
zipcode = "'.$zipcode.'",
country = "'.$country.'",
email = "'.$email.'",
price_level = "'.$price_level.'",
notes = "'.$notes.'"
WHERE client_id="'.$client_id.'" AND store_id="'.$_SESSION['store_id'].'"';
$result = $db->query($query) or die($db->error);
return 'Client updated Successfuly!';
}//update user level ends here.
function list_clients() {
global $db;
$query = 'SELECT * from clients WHERE store_id="'.$_SESSION['store_id'].'" ORDER by full_name ASC';
$result = $db->query($query) or die($db->error);
$content = '';
$count = 0;
while($row = $result->fetch_array()) {
extract($row);
$count++;
if($count%2 == 0) {
$class = 'even';
} else {
$class = 'odd';
}
$content .= '<tr class="'.$class.'">';
$content .= '<td>';
$content .= $client_id;
$content .= '</td><td>';
$content .= $full_name;
$content .= '</td><td>';
$content .= $business_title;
$content .= '</td><td>';
$content .= $mobile;
$content .= '</td><td>';
$content .= $phone;
$content .= '</td><td>';
$content .= $address.' '.$city.' '.$state.' '.$zipcode.' '.$country;
$content .= '</td><td>';
$content .= $email;
$content .= '</td><td>';
$content .= $price_level;
$content .= '</td><td>';
$content .= currency_format($this->get_client_balance($client_id));
$content .= '</td>';
if(partial_access('admin')) {
$content .= '<td><form method="post" name="edit" action="manage_client.php">';
$content .= '<input type="hidden" name="edit_client" value="'.$client_id.'">';
$content .= '<input type="submit" class="btn btn-default btn-sm" value="Edit">';
$content .= '</form>';
$content .= '</td><td>';
$content .= '<form method="post" name="delete" onsubmit="return confirm_delete();" action="">';
$content .= '<input type="hidden" name="delete_client" value="'.$client_id.'">';
$content .= '<input type="submit" class="btn btn-default btn-sm" value="Delete">';
$content .= '</form>';
$content .= '</td>';
}
$content .= '</tr>';
unset($class);
}//loop ends here.
echo $content;
}//list_notes ends here.
function delete_client($client_id) {
global $db;
$query = "SELECT * FROM customer_log WHERE client_id='".$client_id."'";
$result = $db->query($query) or die($db->error);
$num_rows = $result->num_rows;
if($num_rows > 0) {
return 'Please delete sale invoices, receivings, return invoices, return payments for related client first.';
} else {
$query = "DELETE FROM clients WHERE client_id='".$client_id."'";
$result = $db->query($query) or die($db->error);
return 'Client deleted successfuly!';
}
}//delete client ends here.
function client_options($client_id) {
global $db;
$query = 'SELECT * from clients WHERE store_id="'.$_SESSION['store_id'].'" ORDER by full_name ASC';
$result = $db->query($query) or die($db->error);
$options = '';
if($client_id != '') {
while($row = $result->fetch_array()) {
if($client_id == $row['client_id']) {
$options .= '<option selected="selected" value="'.$row['client_id'].'">'.$row['full_name'].' ('.$row['mobile'].')</option>';
} else {
$options .= '<option value="'.$row['client_id'].'">'.$row['full_name'].' ('.$row['mobile'].')</option>';
}
}
} else {
while($row = $result->fetch_array()) {
$options .= '<option value="'.$row['client_id'].'">'.$row['full_name'].' ('.$row['mobile'].')</option>';
}
}
return $options;
}//vendor options ends here.
function add_log($datetime, $client_id, $transaction_type, $type_table_id) {
global $db;
$query = "INSERT into customer_log(customer_log_id, datetime, client_id, transaction_type, type_table_id, store_id) VALUES(NULL, '".$datetime."', '".$client_id."', '".$transaction_type."', '".$type_table_id."', '".$_SESSION['store_id']."')";
$result = $db->query($query) or die($db->error);
return $db->insert_id;
}//add log ends here.
function add_receiving($date, $method, $ref_no, $memo, $amount, $client_id) {
global $db;
$query = "INSERT into receivings(receiving_id, datetime, method, ref_no, memo, amount, client_id, agent_id, store_id) VALUES(NULL, '".$date."', '".$method."', '".$ref_no."', '".$memo."', '".$amount."', '".$client_id."', '".$_SESSION['user_id']."', '".$_SESSION['store_id']."')";
$result = $db->query($query) or die($db->error);
return $db->insert_id;
}//add_payment ends here.
function add_return_payment($date, $method, $ref_no, $memo, $amount, $client_id) {
global $db;
$query = "INSERT into sale_return_payment(return_payment_id, datetime, method, ref_no, memo, amount, client_id, agent_id, store_id) VALUES(NULL, '".$date."', '".$method."', '".$ref_no."', '".$memo."', '".$amount."', '".$client_id."', '".$_SESSION['user_id']."', '".$_SESSION['store_id']."')";
$result = $db->query($query) or die($db->error);
return $db->insert_id;
}//add_payment ends here.
function get_client_balance($client_id) {
global $db;
$creditQuery = "SELECt * from creditors WHERE client_id='".$client_id."' AND store_id='".$_SESSION['store_id']."'";
$creditResult = $db->query($creditQuery) or die($db->error);
$receiveable = 0;
while($creditRow = $creditResult->fetch_array()) {
$receiveable += $creditRow['receiveable'];
if($creditRow['receiveable'] == 0) {
$receiveable -= $creditRow['received'];
}
}
$receivingQuery = "SELECt * from receivings WHERE client_id='".$client_id."' AND store_id='".$_SESSION['store_id']."'";
$receivingResult = $db->query($receivingQuery) or die($db->error);
while($recevingRow = $receivingResult->fetch_array()) {
$receiveable -= $recevingRow['amount'];
}
$sale_return_payment = "SELECt * from sale_return_payment WHERE client_id='".$client_id."' AND store_id='".$_SESSION['store_id']."'";
$sale_payment_result = $db->query($sale_return_payment) or die($db->error);
while($sale_return_row = $sale_payment_result->fetch_array()) {
$receiveable -= $sale_return_row['amount'];
}
return $receiveable;
}//get vendor balance ends here.
function list_receivings() {
global $db;
$query = 'SELECT * from receivings WHERE store_id="'.$_SESSION['store_id'].'" ORDER by receiving_id DESC';
$result = $db->query($query) or die($db->error);
$content = '';
while($row = $result->fetch_array()) {
extract($row);
$datetime = strtotime($datetime);
$date = date('d-M-Y', $datetime);
$client = $this->get_client_info($client_id, 'full_name');
$user = new Users;
$agent = $user->get_user_info($agent_id, 'first_name').' '.$user->get_user_info($agent_id, 'last_name');
$content .= '<tr><td>';
$content .= $receiving_id;
$content .= '</td><td>';
$content .= $date;
$content .= '</td><td>';
$content .= $method;
$content .= '</td><td>';
$content .= $ref_no;
$content .= '</td><td>';
$content .= $agent;
$content .= '</td><td>';
$content .= $client;
$content .= '</td><td>';
$content .= $memo;
$content .= '</td><td>';
$content .= $amount;
$content .= '</td>';
if(partial_access('admin')) {
$content .= '<td><form method="post" name="delete" onsubmit="return confirm_delete();" action="">';
$content .= '<input type="hidden" name="delete_receiving" value="'.$receiving_id.'">';
$content .= '<input type="submit" class="btn btn-default btn-sm" value="Delete">';
$content .= '</form>';
$content .= '</td>'; }
$content .= '</tr>';
unset($class);
}//loop ends here.
echo $content;
}//list_notes ends here.
function delete_receiving($receiving_id) {
global $db;
$query = "DELETE from receivings WHERE receiving_id='".$receiving_id."'";
$result = $db->query($query) or die($db->error);
$query = "DELETE from customer_log WHERE transaction_type='Sale Receiving' AND type_table_id='".$receiving_id."'";
$result = $db->query($query) or die($db->error);
$query = "DELETE from customer_log WHERE transaction_type='Receiving' AND type_table_id='".$receiving_id."'";
$result = $db->query($query) or die($db->error);
return 'Receiving deleted Successfuly.';
}//delete_purchase return receiving.
function list_return_payments() {
global $db;
$query = 'SELECT * from sale_return_payment WHERE store_id="'.$_SESSION['store_id'].'" ORDER by return_payment_id DESC';
$result = $db->query($query) or die($db->error);
$content = '';
while($row = $result->fetch_array()) {
extract($row);
$datetime = strtotime($datetime);
$date = date('d-M-Y', $datetime);
$client = $this->get_client_info($client_id, 'full_name');
$user = new Users;
$agent = $user->get_user_info($agent_id, 'first_name').' '.$user->get_user_info($agent_id, 'last_name');
$content .= '<tr><td>';
$content .= $return_payment_id;
$content .= '</td><td>';
$content .= $date;
$content .= '</td><td>';
$content .= $method;
$content .= '</td><td>';
$content .= $ref_no;
$content .= '</td><td>';
$content .= $agent;
$content .= '</td><td>';
$content .= $client;
$content .= '</td><td>';
$content .= $memo;
$content .= '</td><td>';
$content .= $amount;
$content .= '</td>';
if(partial_access('admin')) {
$content .= '<td><form method="post" name="delete" onsubmit="return confirm_delete();" action="">';
$content .= '<input type="hidden" name="delete_sale_return_payment" value="'.$return_payment_id.'">';
$content .= '<input type="submit" class="btn btn-default btn-sm" value="Delete">';
$content .= '</form>';
$content .= '</td>'; }
$content .= '</tr>';
}//loop ends here.
echo $content;
}//list_notes ends here.
function delete_sale_return_payment($return_payment_id) {
global $db;
$query = "DELETE from sale_return_payment WHERE return_payment_id='".$return_payment_id."'";
$result = $db->query($query) or die($db->error);
$query = "DELETE from customer_log WHERE transaction_type='Sale Return Refund' AND type_table_id='".$return_payment_id."'";
$result = $db->query($query) or die($db->error);
return 'Return Payment deleted Successfuly.';
}//delete_purchase return receiving.
function clear_creditors($amount, $client_id){
global $db;
$query = "SELECT * FROM creditors WHERE client_id='".$client_id."' ORDER by credit_id ASC";
$result = $db->query($query) or die($db->error);
while($row = $result->fetch_array()) {
extract($row);
if($receiveable == 0 || $receiveable == $received || $amount == 0) {
//do nothing.
} else {
if($received == 0) {
if($amount < $receiveable) {
$receive = $amount;
} else {
$receive = $receiveable;
}
$query_up = "UPDATE creditors SET
received = '".$receive."'
WHERE credit_id='".$credit_id."'
";
$amount -= $receive;
} else if($received != 0) {
$difference = $receiveable-$received;
if($amount < $difference) {
$receive = $amount+$received;
} else {
$receive = $difference+$received;
}
$query_up = "UPDATE creditors SET
received = '".$receive."'
WHERE credit_id='".$credit_id."'
";
$amount -= $difference;
}
$result_up = $db->query($query_up) or die($db->error);
}//main if ends here.
}//main loop ends.
}//debts clear ends here.--
function customers_balance_summary() {
global $db;
$query = "SELECT * FROM clients WHERE store_id='".$_SESSION['store_id']."' ORDER by full_name ASC";
$result = $db->query($query) or die($db->error);
$content = '';
$grand_total = 0;
while($row = $result->fetch_array()) {
extract($row);
//getting balance.
$balance = $this->get_client_balance($client_id);
$grand_total += $balance;
$content .= '<tr><td>';
$content .= $full_name;
$content .= '</td><td>';
$content .= $business_title;
$content .= '</td><td align="right">';
$content .= currency_format($grand_total);
$content .= '</td></tr>';
}
$new_store = new Store;
$currency = $new_store->get_store_info($_SESSION['store_id'], 'currency');
$content .= '<tr><th colspan="2" align="right">Grand Total</th><th align="right">'.$currency.' '.currency_format($grand_total).'</tH></tr>';
echo $content;
}//customers balance summary ends here.
function customer_ledger_summary($client) {
global $db;
$query = "SELECT * from customer_log WHERE client_id='".$client."' ORDER by customer_log_id ASC";
$result = $db->query($query) or die($db->error);
$balance = 0;
$content = '';
$balance = 0;
while($row = $result->fetch_array()) {
extract($row);
$datetime = strtotime($datetime);
$date = date('d-M-Y', $datetime);
$content .= '<tr><td>';
$content .= $transaction_type;
$content .= '</td><td>';
$content .= $date;
$content .= '</td><td>';
$content .= $type_table_id;
$content .= '</td><td>';
if($transaction_type == 'Sale Invoice' || $transaction_type == 'Cash Sale') {
//Invoice Details.
$sale_query = "SELECT * from sales WHERE sale_id='".$type_table_id."'";
$sale_result = $db->query($sale_query) or die($db->error);
while($sale_row = $sale_result->fetch_array()) {
$content .= $sale_row['memo'];
$content .= '</td><td>';
}
$sale_detail_query = "SELECT * from sale_detail WHERE sale_id='".$type_table_id."'";
$sale_detail_result = $db->query($sale_detail_query) or die($db->error);
$invoice_total = 0;
while($sale_detail_row = $sale_detail_result->fetch_array()) {
$credit_query = "SELECT * from creditors WHERE credit_id='".$sale_detail_row['credit_id']."'";
$credit_result = $db->query($credit_query) or die($db->error);
while($credit_row = $credit_result->fetch_array()) {
$invoice_total += $credit_row['receiveable'];
}
}
$balance = $invoice_total+$balance;
$content .= currency_format($invoice_total);
$content .= '</td><td>';
$content .= currency_format($balance);
$content .= '</td></tr>';
} else if($transaction_type == 'Sale Receiving' || $transaction_type == 'Receiving') {
//Cash receivign.
$receiving_query = "SELECT * from receivings WHERE receiving_id='".$type_table_id."'";
$receiving_result = $db->query($receiving_query) or die($db->error);
while($receiving_row = $receiving_result->fetch_array()) {
$content .= $receiving_row['memo'];
$content .= '</td><td>';
$balance = $balance-$receiving_row['amount'];
$content .= '('.currency_format($receiving_row['amount']).')';
$content .= '</td><td>';
$content .= currency_format($balance);
$content .= '</td></tr>';
}
} else if($transaction_type == 'Invoice Return' || $transaction_type == 'Sale Return') {
//sale return invoice.
$sale_query = "SELECT * from sale_returns WHERE sale_rt_id='".$type_table_id."'";
$sale_result = $db->query($sale_query) or die($db->error);
while($sale_row = $sale_result->fetch_array()) {
$content .= $sale_row['memo'];
$content .= '</td><td>';
}
$sale_detail_query = "SELECT * from sale_return_detail WHERE sale_rt_id='".$type_table_id."'";
$sale_detail_result = $db->query($sale_detail_query) or die($db->error);
$invoice_total = 0;
while($sale_detail_row = $sale_detail_result->fetch_array()) {
$credit_query = "SELECT * from creditors WHERE credit_id='".$sale_detail_row['credit_id']."'";
$credit_result = $db->query($credit_query) or die($db->error);
while($credit_row = $credit_result->fetch_array()) {
$invoice_total += $credit_row['received'];
}
}
$balance = $balance-$invoice_total;
$content .= '('.currency_format($invoice_total).')';
$content .= '</td><td>';
$content .= currency_format($balance);
$content .= '</td></tr>';
} else if($transaction_type == 'Sale Return Refund') {
//sale Return Payment.
$receiving_query = "SELECT * from sale_return_payment WHERE return_payment_id='".$type_table_id."'";
$receiving_result = $db->query($receiving_query) or die($db->error);
while($receiving_row = $receiving_result->fetch_array()) {
$content .= $receiving_row['memo'];
$content .= '</td><td>';
$balance = $balance+$receiving_row['amount'];
$content .= currency_format($receiving_row['amount']);
$content .= '</td><td>';
$content .= currency_format($balance);
$content .= '</td></tr>';
}
}
}//main loop ends here.
echo $content;
}//customer ledger summary ends here.
}//class ends here.
The shown screen code where this class functions are being used:
`
<?php
include('system_load.php');
//This loads system.
//user Authentication.
authenticate_user('subscriber');
//creating company object.
if(partial_access('admin') || $store_access->have_module_access('sales')) {} else {
HEADER('LOCATION: store.php?message=products');
}
if(!isset($_SESSION['store_id']) || $_SESSION['store_id'] == '') {
HEADER('LOCATION: stores.php?message=1');
} //select company redirect ends here.
if(isset($_POST['edit_purchase'])){ $page_title = 'Edit Sale'; } else { $page_title = 'Add Sale';}; //You can edit this to change your page title.
require_once("includes/header.php"); //including header file.
?>
<?php if(isset($_GET['sale_id'])) { ?>
<script type="text/javascript">
window.open('reports/view_sale_invoice.php?sale_id=<?php echo $_GET['sale_id']; ?>', '_blank');
</script>
<?php } ?>
<?php
//display message if exist.
if(isset($_GET['message']) && $_GET['message'] != '') {
echo '<div class="alert alert-success">';
echo $_GET['message'];
echo '</div>';
}
if(isset($message) && $message != '') {
echo '<div class="alert alert-success">';
echo $message;
echo '</div>';
}
?>
<style type="text/css">
textarea:hover, textarea:focus, #items td.total-value textarea:hover, #items td.total-value textarea:focus, .delme:hover { background-color:#EEFF88; }
#items input[type=text] {width:60px;border:0px;}
.delete-wpr { position: relative; }
.delme { display: block; color: #000; text-decoration: none; position: absolute; background: #EEEEEE; font-weight: bold; padding: 0px 3px; border: 1px solid; top: -6px; left: -22px; font-family: Verdana; font-size: 12px; }
</style>
<script type="text/javascript">
jQuery(function($) {
$('form[data-async]').on('submit', function(event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: 'includes/otherprocesses.php',
data: $form.serialize(),
dataType: 'json',
success: function(response) {
var message = response.message;
var client_options = response.client_options;
var client_id = response.client_id;
$('#client_id').html(client_options);
$("#client_id").select2().select2('val', client_id);
$('#success_message').html('<div class="alert alert-success">'+message+'</div>');
}
});
event.preventDefault();
});
});
</script>
<!-- Add new vendor modal starts here. -->
<div class="modal fade" id="addnewclient" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add new client</h4>
</div>
<div class="modal-body">
<form data-async data-target="#addnewclient" method="POST" enctype="multipart/form-data" role="form">
<div id="success_message"></div>
<table style="width:100%;">
<tr>
<td>
<div class="form-group">
<label class="control-label">Full Name*</label>
<input type="text" class="form-control" name="full_name" placeholder="Client full name" value="" required="required" />
</div>
</td>
<td>
<div class="form-group">
<label class="control-label">Business Title</label>
<input type="hidden" name="add_client" value="1" />
<input type="submit" id="submit" class="btn btn-primary" value="Add client">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!--add new vendor modal ends here.-->
<form action="includes/process_sale.php" method="post">
<div class="row">
<div class="col-sm-5">
<table border="0" cellpadding="5">
<tr>
<td width="110">Date</td>
<td width="300"><input type="text" name="date" class="form-control datepick" readonly="readonly" value="<?php echo date('Y-m-d'); ?>" /></td>
</tr>
<tr>
<td>Custom Inv#</td>
<td><input type="text" placeholder="Custom Invoice number" name="custom_inv_no" class="form-control" /></td>
</tr>
<tr>
<td>Memo</td>
<td><textarea placeholder="Memo" name="memo" class="form-control"></textarea></td>
</tr>
<tr>
<th>Select Client</th>
<td>
<select name="client_id" id="client_id" class="autofill" style="width:100%">
<option value="">Select Client by full name or mobile</option>
<?
=$client->client_options($client->client_id);
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><a class="btn btn-default btn-xs" data-toggle="modal" href="#addnewclient">Add new Client</a></td>
</tr>
</table>
</div><!--left-side-form ends here.-->
<script type="text/javascript">
function update_total() {
var grand_total = 0;
i = 1;
$('.total').each(function(i) {
var total = $(this).html();
total = parseFloat(total);
grand_total = total+grand_total;
});
$('#grand_total').html(grand_total.toFixed(2));
}//Update total function ends here.
<?php
require_once("includes/footer.php");
?>
What plattform do you use for the localhost? Online linux server and local a Windows machine? As it is run on the localhost, do you get warnings, notices or the sort? Or even a fatal?
<?
=$client->client_options($client->client_id);
?>
That looks odd. Why the '='?
Okay I finally figured how to make it work by the hint provided above. Actually this Syntax doesn't work in this case:
<?=$client->client_options($client->client_id);?>
The Solution is:
<?php echo $client->client_options($client->client_id); ?>
Thanks all anyway for brainstorming.
I had set a search button in my website. It works fine if there's a result but if there's none, it doesn't show the "no results.....".
What's wrong with my code?
<html !DOCTYPE HTML>
<body>
<?php
$title = "DenTEETH";
include('header.html');
//connect to database
$db = mysqli_connect("127.0.0.1", "root", "", "authentication");
if(isset($_GET['q']) && $_GET['q'] !== '')
{
$searchq = $_GET['q'];
$sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
$output='';
$results = mysqli_query($db, $sql);
if (count($results) == 0){
$output .= 'No search results for <b>"' . $searchq . '"</b>';
}
else{
while ($row = mysqli_fetch_array($results)){
$id = $row['search_id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';
$output .= '<div class="search_thumb">
<p class="search_cap">' . $img . '<h3>' . $title . '</h3>' . $desc .
'<div class="clear"></div></p></div>';
}
}
}
else{
header("location: ./");
}
print($output);
?>
<div class="row">
</div>
<?php
include('footer.html');
?>
</body>
</html>
Use mysqli_num_rows() try this
<html !DOCTYPE HTML>
<body>
<?php
$title = "DenTEETH";
include('header.html');
//connect to database
$db = mysqli_connect("127.0.0.1", "root", "", "authentication");
if(isset($_GET['q']) && $_GET['q'] !== '')
{
$searchq = $_GET['q'];
$sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
$output='';
$results = mysqli_query($db, $sql);
if (mysqli_num_rows($results ) == 0){
$output .= 'No search results for <b>"' . $searchq . '"</b>';
}
else{
while ($row = mysqli_fetch_array($results)){
$id = $row['search_id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';
$output .= '<div class="search_thumb">
<p class="search_cap">' . $img . '<h3>' . $title . '</h3>' . $desc .
'<div class="clear"></div></p></div>';
}
}
}
else{
header("location: ./");
}
print($output);
?>
<div class="row">
</div>
<?php
include('footer.html');
?>
</body>
</html>
I have created a simple search engine who gets data from a PHP table and post the results. But as of right now it posts the results in a single line and nothing to show that they're in fact 4 different columns. What I'm looking for is a way to create a table so it looks more presentable. I have the search engine divided into 2 .php files.
1st file:
<?php
error_reporting(1);
include('system_admin.inc.php');
$title = 'Leonard';
make_header($title,array('enable_ajax' => true));
?>
<html>
<head>
<title>Leonard</title>
<script type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type=text/javascript ">
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post("testsearch.php", {searchVal: searchTxt}, function(output) {
$("#output") .html(output);
});
}
</script>
</head>
<body>
<h1>Search</h1>
<form class="odd" action="leonard.php" method="post">
<input type="text" name="search" placeholder="Search" onkeyup="searchq();" />
<div id="output">
</div>
</form>
</body>
</html>
2nd file:
mysql_connect ("localhost","root","xxxxxx") or die ("Connectionissues");
mysql_select_db ("xxxxxx") or die("Can't find DB");
$output = '';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#^0-9#"," ",$searchq);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' LIMIT 100") or die("Search invalid!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Order have never been made before';
}else{
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
$output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.' </div>';
}
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
}
I want these four columns to be displayed in a presentable manner:
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
Can anyone help me with this? :-)
Thanks in advance for reading and any advice given!
Change this portion
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
$output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.' </div>';
}
to
$output .='<table>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';