PHP - Cart is not loading on the page - javascript

I have a website with a cart, the index file is as below:
<?php
session_start();
include_once 'product-action.php';
include_once 'cart.php';
$stmt = $conn->prepare("SELECT * FROM entertainment ORDER BY id ASC");
$stmt->execute();
$entertainment = $stmt->get_result();
echo '<table style="border: 1px solid #ddd;"><tr>';
if (!empty($entertainment)) {
foreach($entertainment as $product){
?>
<td style="border: 1px solid #ddd;">
<form method="post" action=""Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location?action=add&id=<?php echo $product['id']; ?>">
<div class="product-image"><img src="<?php echo $product['image']; ?>" width="200" height="200"></div>
<div><strong><?php echo $product["title"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product["offer_price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" /></div>
</form>
</td>
<?php
}
}
echo '</tr></table>';
?>
The other 2 pages are as follows:
cart.php
<?php
if(!empty($_SESSION["cart_item"])){
$item_total = 0;
?>
<input type="button" value="Empty Cart">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["product_title"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td><?php echo "$".$item["price"]; ?></td>
<td>Remove Item</td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
<?php
if(!empty($_SESSION["cart_item"])){
$item_total = 0;
?>
<input type="button" value="Empty Cart">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["title"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td><?php echo "$".$item["offer_price"]; ?></td>
<td>Remove Item</td>
</tr>
<?php
$item_total += ($item["offer_price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
The product_action.php
<?php
if(!empty($_GET["action"])) {
$productId = isset($_GET['id']) ? htmlspecialchars($_GET['id']) : '';
$quantity = isset($_POST['quantity']) ? htmlspecialchars($_POST['quantity']) : '';
switch($_GET["action"]) {
case "add":
if(!empty($quantity)) {
$stmt = $conn->prepare("SELECT * FROM entertainment where id= ?");
$stmt->bind_param('i',$productId);
$stmt->execute();
$productDetails = $stmt->get_result()->fetch_object();
$itemArray = array($productDetails->id=>array('product_title'=>$productDetails->title, 'id'=>$productDetails->id, 'quantity'=>$quantity, 'price'=>$productDetails->offer_price));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productDetails->id,array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productDetails->id == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $quantity;
}
}
} else {
$_SESSION["cart_item"] = $_SESSION["cart_item"] + $itemArray;
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productId == $v['id'])
unset($_SESSION["cart_item"][$k]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
I am completely new to PHP. I tried searching alot but didn't find what I wanted. now the problem is while loading the page, the page is not displaying, its still loading. can anyone tell me how to fix it. any help would be great.

Related

How to change each html table row accordian

I want to create a dynamic table which gets its all its entries coming from database. I know how to make a simple table accordian by this Twitter Bootstrap Use collapse.js on table cells [Almost Done] .
If I apply that technique it turns table rows into accordian but no matter which row i click it only expands first row.
Here is the code of my table getting data from database.
<div class="container">
<h2>All the Suggestions of HR</h2>
<div class="table-responsive">
<table id="myTable" width="100%" class="table table-striped display responsive nowrap table-hover">
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">Date</th>
<th scope="col">Time</th>
<th scope="col">Subject</th>
<th scope="col">Message</th>
<th scope="col">department</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM hr;";
$result = mysqli_query($conn , $sql);
$nr = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr>
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php if ($status == 'true') {
echo "Answerd";
}
else{
echo "Pending";
} ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
Whenever a row is clicked there is a content i want to show which is different for every row.
<?php
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr data-toggle="collapse" data-target="#accordian_<?php echo $id; ?>" class="accordion-toggle">
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php echo $status == 'true' ? 'Answerd' : 'Pending'; ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<tr >
<td colspan="6" class="hiddenRow">
<div class="accordian-body collapse" id="accordian_<?php echo $id; ?>"> Your Text here</div>
</td>
</tr>
<?php endwhile; ?>

how to output specific information in a modal from a database

I am trying to display more information about the user in my table. The goal is to see more information about the user in a modal, than you can see in the table overview. Unfortunately, I don't manage to display only the information of the respective user in the modal. I already have the following code which doesn't work like i want. At the moment, the modal stays emtpy.
Greetings
admin.php
<?php
include('connection.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/viewDetail_/i',$key_post) ) {
$id_to_view = (integer) str_replace('viewDetail_','', $key_post);
$viewQuery = "SELECT * FROM card WHERE id = " . $id_to_view;
$statement = $pdo->prepare($viewQuery);
$statement->execute();
$viewResult = $statement->fetchAll();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>
<input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<input type="submit" name="viewDetail_<?php echo $row['ID']; ?>" value="modal" data-target="modal1" class="modal-trigger">
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
<br>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Absenden
<i class="material-icons right">send</i>
</button>
</div>
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
</tr>
<?php
foreach ($viewResult as $row){ ?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
<div class="modal-footer">
Zurück
</div>
</div>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>

Sec0nd Data Accessing error in my view page

I have fetched some 10 records from database and accessed in view page using foreach.But i can only give approve to first record not other records.Why it is not happening.
this is my view page
<tr><tbody>
<?php`enter code here`
if(isset($result))
{
foreach($result as $value){?>
<td><?php echo $name;?> </td>
<td><?php echo $value->date1;?> </td>
<td><?php echo $value->purpose;?> </td>
<td><?php echo $value->amount;?> </td>
<td><?php echo $value->rep_date;?> </td>
<?php if($value->temp==1){?>
<td> Approved </td>
<td><?php echo $value->id;?> </td>
<?php } else {?>
<td>
<form method="POST" action="<?php echo base_url().'Admin/updatestatus';?>">
<input type="hidden" name ="hid" id="hid" value="<?php echo $value->id;?>">
<input type="submit" name="sub" id="sub" class="btn green" value="Approve" >
</td>
<?php }?>
<td> chat </td>
</tr>
<?php } }?>
</tbody>
This is my controller
function updatestatus()
{
$this->load->database();
$this->load->library('session');
$this->load->model('lpmodel');
$frmdate=$this->session->userdata('from');
$todate=$this->session->userdata('to');
$status=$this->session->userdata('stat');
$emp_id=$this->session->userdata('id');
$hidid=$this->input->post('hid');
echo $hidid;
$data=array(
'temp'=>'1');
$query=$this->lpmodel->ad_aprove_data($emp_id,$data,$hidid);
if($query==true)
{ //echo "hi";
?> <script>
alert('Approved');
</script><?php
//$value['result']=$this->lpmodel->selectapproverow($id);
$name=$this->session->userdata('name');
$value['name']=$name;
//$status=$this->session->set_userdata('stat');
$value['result']=$this->lpmodel->ad_selectreimb($emp_id,$status,$frmdate,$todate);
$this->load->view('ad_reimbursement',$value);
}
else
{// echo "error";
?> <script>
alert('Try Again');
</script><?php
$value['new']=$this->lpmodel->fetchname();
$this->load->view('ad_reimbursement',$value);
}
}

CodeIgniter AJAX Pagination Delete issue

I have managed to implement ajax pagination and delete in my code. However, I can only delete one only.The first time succeeds. The next time however, gives me an error that says there is no such file or directory to unlink. I suspect it has something to do with how my ajax pagination works but I am unsure of how to fix it.
The way my code works is that when I click on a pagination link, ajax_pagination.php is loaded. The javascript is only contained in the index.php view. Adding the script to both pages won't work though unfortunately.
This is my controller
public function index()
{
$conditions = array();
$data = array();
$totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
$config['target'] = '#list';
$config['base_url'] = site_url('/AdminDocuments/Search');
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->get_per_page();
$this->ajax_pagination->initialize($config);
$data['links'] = $this->ajax_pagination->create_links();
$data['datatable'] = $this->DocumentModel->admin_get_and_search(array('limit'=>$this->get_per_page()));
$data['user'] = $this->AccountModel->get_person($this->get_person_id());
// print_r($totalRec);
// print_r($data['datatable']);
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/header');
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/navigation');
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/title');
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/errors');
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/search');
$this->load->view('admins/documents/index',$data);
$this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/footer');
}
public function search(){
$conditions = array();
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
$keywords = $this->input->post('keywords');
if(!empty($keywords)){
$conditions['search']['keywords'] = $keywords;
}
$totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
$config['target'] = '#list';
$config['base_url'] = site_url('/AdminDocuments/Search');
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->get_per_page();
$this->ajax_pagination->initialize($config);
$conditions['start'] = $offset;
$conditions['limit'] = $this->get_per_page();
$data['links'] = $this->ajax_pagination->create_links();
$data['datatable'] = $this->DocumentModel->admin_get_and_search($conditions);
$this->load->view('admins/documents/ajax_pagination', $data, false);
}
public function delete(){
$id = $this->input->post('id');
unlink($this->DocumentModel->admin_get($id)['file_location']);
$result = $this->DocumentModel->delete($id);
$activity = array(
'page'=>'Documents',
'action'=>'Deleted document',
'created_on'=>date('Y-m-d'),
'time_on'=>date('h:i:s')
);
$this->ActivitiesModel->admin_create($activity);
if($result===false){
echo "false";
}else{
echo "true";
}
}
My view
<script>
function searchFilter(page_num) {
page_num = page_num?page_num:0;
var keywords = $('#search').val();
$.ajax({
type: 'POST',
url: '<?php echo site_url('/AdminDocuments/Search/'); ?>'+page_num,
data:'page='+page_num+'&keywords='+keywords,
beforeSend: function () {
$('.loading').show();
},
success: function (html) {
$('#list').html(html);
$('.loading').fadeOut("slow");
}
});
}
function deleteDocument(input){
var id = input;
console.log("javascript id "+id);
$.ajax({
type:'POST',
url:'<?php echo site_url('/AdminDocuments/Delete/'); ?>',
data:'id='+id,
beforeSend: function () {
$('.loading').show();
},
success:function(result){
console.log(result);
// $('#list').html(result);
searchFilter();
$('.loading').fadeOut("slow");
}
});
}
</script>
<div id="maincontainer">
<div id="list" class="row">
<div class="col-sm-12 table-responsive">
<table class="table">
<thead>
<tr class="text-left">
<td>Period</td>
<td>Name</td>
<td>Policy Number</td>
<td>Agent ID</td>
<td>Client ID</td>
<td>Policy Type</td>
<td>Transaction ID</td>
<td>File Name</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr class="text-left">
<td><?php echo $data['month']."/"; ?><?php echo $data['year']; ?></td>
<td><?php echo $data['first_name']; ?> <?php echo $data['last_name']; ?></td>
<td><?php echo $data['policy_number']; ?></td>
<td><?php echo $data['agent_id']; ?> </td>
<td><?php echo $data['client_id']; ?> </td>
<td><?php echo $data['policy_type']; ?></td>
<td><?php echo $data['transaction_id']; ?></td>
<td><?php echo $data['file_name']; ?></td>
<td><i class="fa fa-file" aria-hidden="true"></i> View</td>
<!--<td><i class="fa fa-trash" aria-hidden="true"></i> Delete</td>-->
<td><i class="fa fa-trash" aria-hidden="true"></i> Delete</td>
</tr>
<?php
}
}else{
?>
<tr>
<td colspan="7">You do not have any items at the moment</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="pagination-links pull-right">
<?php echo $links; ?>
</div>
</div>
<div class="loading" style="display: none;">
<div class="content">
<img src="<?php echo base_url().'assets/images/loading/loading.gif'; ?>"/>
</div>
</div>
</div>
Ajax pagination page which is what loads when a pagination link is selected
<div id="list" class="row">
<div class="col-sm-12 table-responsive">
<table class="table">
<thead>
<tr class="text-left">
<td>Period</td>
<td>Name</td>
<td>Policy Number</td>
<td>Agent ID</td>
<td>Client ID</td>
<td>Policy Type</td>
<td>Transaction ID</td>
<td>File Name</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr class="text-left">
<td><?php echo $data['month']."/"; ?><?php echo $data['year']; ?></td>
<td><?php echo $data['first_name']; ?> <?php echo $data['last_name']; ?></td>
<td><?php echo $data['policy_number']; ?></td>
<td><?php echo $data['agent_id']; ?> </td>
<td><?php echo $data['client_id']; ?> </td>
<td><?php echo $data['policy_type']; ?></td>
<td><?php echo $data['transaction_id']; ?></td>
<td><?php echo $data['file_name']; ?></td>
<td><i class="fa fa-file" aria-hidden="true"></i> View</td>
<td><i class="fa fa-trash" aria-hidden="true"></i> Delete</td>
</tr>
<?php
}
}else{
?>
<tr>
<td colspan="7">You do not have any items at the moment</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="pagination-links pull-right">
<?php echo $links; ?>
</div>
</div>
Pagination javascript located in the library
<script>
function getData(page){
// console.log(page);
$.ajax({
method: "POST",
url: "<?php echo $this->base_url; ?>"+page,
data: { page: page },
beforeSend: function(){
$('<?php echo $this->loading; ?>').show();
},
success: function(data){
$('<?php echo $this->loading; ?>').hide();
$('<?php echo $this->target; ?>').html(data);
}
});
}
</script>
I am unsure of how I can get the page number from the page itself.
You have to pass page_num in your function :
Check comment on below code:
success:function(result){
console.log(result);
// $('#list').html(result);
searchFilter(); // look at here
$('.loading').fadeOut("slow");
}
function searchFilter(page_num) // look at here

How Do Set open popup And get Chacked value in another page

(1) Select To book(onchange value) Then open popup is ok.
(2)open popup in Checked NetAmount then i want to get Total in Text
Box.
(3)then Cheked NetAmount To Display This Record in index page.
i Want To point(2,3).
index.php
script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function ()
{
$("#Debit_id").change(function ()
{
var Debit_id = document.getElementById('Debit_id').value;
if(Debit_id!="")
{
window.open("getpopup.php?Debit_id="+Debit_id,'popup','width=700,height=400,left=200,top=200,scrollbars=1');
}
});
});
</script>
Book : <select name="Debit_id" id="Debit_id">
<option value="" selected="selected">Select Book</option>
<?php
$result1 = mysql_query("SELECT AccountName,CID FROM account")or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
if($row1)
{ ?>
<option value="<?php echo $row1['CID']; ?>"><?php echo $row1['AccountName']; ?></option><?php
} ?>
</select>
<br />
NetAmount Total : <input type="text" class="input" name="NetAmount" id="TotalCost">
popup.php
<?php
include("../config.php");
$Debit_id = intval($_GET['Debit_id']);
?>
<table>
<tr>
<th>SrNo</th>
<th>EntryDate</th>
<th>NetAmount</th>
</tr>
<?php
$sql="SELECT * FROM transaction WHERE Credit_id = '".$Debit_id."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$SrNo =$row['SrNo'];
$EntryDate = $row['EntryDate'];
$NetAmount =$row['NetAmount'];
?>
<tr>
<td><?php echo $SrNo ?></td>
<td><?php echo $EntryDate ?></td>
<td><?php echo $NetAmount ?><input type="checkbox" name="<?php echo $SrNo ?>" value="<?php echo $NetAmount ?>"></td>
</tr>
<?php }?>
<tr>
<td colspan="3" align="center">
<input type="submit" value="ok" />
</td>
</tr>
</table>

Categories

Resources