Codeigniter Update cart - php

I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>

Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html

Related

PHP: Show <td> elements if certain criteria is true

Basically I need to have the columns with my delete and edit buttons visible only if the admin logs in, I know how to determine if an admin is logged in. Is there a way to echo the tags that need to be shown? Thanks in advance!
<table>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Status</th>
<th>Edit?</th>
<th>Delete?</th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
<td><?php echo $user->getUser_id();?></td>
<td><?php echo $user->getFirstName().' '.$user->getLastName();?></td>
<td><?php echo $user->getEmailAddress(); ?></td>
<td><?php echo $user->getAddress();?></td>
<td><?php echo $user->getCity(); ?></td>
<td><?php echo $user->getState(); ?></td>
<td><?php echo $user->getZipCode(); ?></td>
<td><?php echo $user->getActiveDescription();?></td>
these two <td> should only be shown if x is true:
<td>
<form action ="" method="post">
<input type="hidden" name="controllerRequest"
value="show_user_edit">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Edit"></form>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="controllerRequest"
value="delete_user">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Delete" id="red">
</form>
</td>"
;
}
}
?>
</tr>
<?php endforeach; ?>
</table>
You need to use <?php if(x): ?> and <?php endif ?>see: https://www.php.net/manual/en/control-structures.if.php#112231

POST array does not retain variables

I am trying to process form fields via POST method. I will post a long code for you to inspect, however the structure of the code is simply like this
<form> <table> <th> </th> <td> </td> </table> </form>
so it is a table inside a form. When the form is submitted, I am taken to the "cart.php" page which is expected, but that page shows the echo statement and shows the variables correctly for a split second, then shows the rest of the page but the variables in the echo statement become "undefined".
P.S: Please don't mark as duplicate with this question, thanks.
This is the first page index.php
<div class="table-responsive" id="order_table">
<form method="POST" action="cart.php" class="form-group">
<table class="table table-bordered">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Sub Total</th>
<th width="5%">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$sub_total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?php echo $values["product_name"]; ?></td>
<td><input type="text" name="quantity[]" id="quantity<?php echo $values["product_id"]; ?>" value="<?php echo $values["product_quantity"]; ?>" data-product_id="<?php echo $values["product_id"]; ?>" class="form-control quantity" /></td>
<td align="right">$ <?php echo $values["product_price"]; ?></td>
<td align="right">$ <?php echo number_format($values["product_quantity"] * $values["product_price"], 2); ?></td>
<td><button name="delete" class="btn btn-danger btn-xs delete" id="<?php echo $values["product_id"]; ?>">Remove</button></td>
</tr>
<?php
$sub_total = $sub_total + ($values["product_quantity"] * $values["product_price"]);
$tax = $sub_total * 0.075;
$total = $sub_total + $tax;
}
?>
<tr>
<td colspan="3" align="right"><span style="font-size:1.3em;">Tax</span></td>
<td align="right">$ <?php echo number_format($tax,2); ?></td>
</tr>
<tr>
<td colspan="3" align="right"><span style="font-size:1.3em;">Total</span></td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<tr>
<td colspan="5" align="center">
<textarea name="comments" id="comment" class="form-control" placeholder="Please enter any special instructions for the order"></textarea> <br>
<input type="submit" name="place_order" id="place_order" class="btn btn-warning" value="Place Order" />
</td>
</tr>
<?php
}
?>
</table>
</form>
And this is the beginning of cart.php:
$comment = $_POST['comments'];
echo $comment. $_POST['place_order'];
print_r($_POST);
How I know that the variables do get passed is cause I noticed some result appearing for a split second so I took a video and paused it there and it had the expected POST array, before the page suddenly appears as expected but with
"Undefined index: comments "
and
"Undefined index: place_order"
and "Array()" at the top. cart.php does not have any commands that make it refresh. Thank you.

Call all posts from Wordpress database with category names

Our Wordpress site are infected and we are deleted all files. we have now only database and want to extract all information from db and afther this copy on other site.
so I am using this php class to connect db and call some information. I am calling now posts with data, but need categories also.
For example:
Post Title, Post Content, Post Category
here is my working code without category names
$posts = $db->get('c3624322676f_wp_posts');
but I have no idea how to call categories names here.
This is my html + foreach
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This should work
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<?php
$separator = ',';
$output = '';
$categories = get_the_category($post['post_title']);
if ($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Get php checkbox data from multiple tables

First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.

How to show multiple results in a query result?

I have a table that I have to populate him with a query result. One of the rows is the name of the group. My result3 inside the while is a query where I get the name of the group. I want to put that name in each row. But for some reason I can only get the first position of the query result. Can you help me?
This is my result3 variable query(in a resumed way):
$sql3 = "SELECT * from titulogrupo where idutilizador ={$_SESSION['id_utilizador']}";
$result3 = mysqli_query(mysqli_connect(", "u518178166_serra"), $sql3);...etc etc...
<table table id= "myTable" class="table table-hover"> <br>
<thead>
<tr>
<th>Os meus Chips</th>
<th>NIF</th>
<th>Marca de Exploracao</th>
<th>Marca do Auricular</th>
<th>Data de Nascimento</th>
<th>Observacoes</th>
<th>Estado</th>
<th>Data1</th>
<th>Data2</th>
<th>Data3</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row['numerochip'];?><input style="float: left; margin: 0 5px;" type="checkbox" value="<?php echo $row['numerochip']; ?>" name="numero[]" id="numero"></td>
<td><?php echo $row['NIF']; ?></td>
<td><?php echo $row['MarcaExploracao']; ?> </td>
<td><?php echo $row['MarcaAuricular']; ?> </td>
<td><?php echo $row['Data_Nascimento']; ?> </td>
<td><?php echo $row['observacoes'];?>
<div class="w3-container" id="....kli..">
<input style="display: none; type="text" id="numerochipescondido" name="numerochipescondido" value="<?php echo $row['numerochip']; ?>">
<input style="display: none; type="text" id="observacoesescondido" name="observacoesescondido" value="<?php echo $row['observacoes']; ?>">
<input style="display: none; type="text" id="idutilizadorescondido" name="idutilizadorescondido" value="<?php echo $_SESSION['id_utilizador']; ?>">
<center>
<button type="button" id="<?php echo $row['numerochip']; ?>" onclick="document.getElementById('id111').style.display='block'" class="glyphicon glyphicon-pencil changeobervations" width:50px height:50px;"></button>
</center>
</div>
</td>
<td>
<?php
if($row['velha']){
echo 'Velha ';
}
if($row['prenha']){
echo 'Prenha ';
}
if($row['refugo']){
echo 'Refugo ';
}
if($row['grupo4']){
while ($row = mysqli_fetch_array($result3)){
echo $row['titulogrupo'];
}
}
?>
</td>
<td><?php echo $row['Data1']; ?> </td>
<td><?php echo $row['Data2']; ?> </td>
<td><?php echo $row['Data3']; ?> </td>
</tr>
<?php } ?>
</table>
<br>
<input type="submit" class="btn btn-warning"name="submit_x" value="Submeter Lista de Animais a Encontrar(Individual)"> <br> <br>
<input type="submit" class="btn btn-danger"name="submit_y" value="Eliminar Animais Selecionados(Individual)"> <br> <br>
<form action="deletedb.php" method="post">
<input type="submit" class="btn btn-danger"name="submit_d" value="Apagar a minha lista total">
</form>
</form>
<br>
</br>
<table class="table table-hover" style="width:300px">
<thead>
<tr>
<th>Os meus Chips a encontrar</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result2)){
?>
<tr>
<td><?php echo $row['ovelhas'];?></td>
</tr>
<?php } ?>
<!-- Começa aqui o apagar -->
<form action="apagarOvelhasEncontrar2.php" method="post">
<br>
<tr>
<td>
<button type="submit" name="submit_x" class="btn btn-danger">Apagar Ovelhas a Encontrar</button>
</td>
</tr>
</form>
</table>
</br>
</div>
Use a different variable when fetching $result3 etc.
if($row['grupo4']){
while ($row3 = mysqli_fetch_array($result3)){
echo $row3['titulogrupo'];
}

Categories