I am attempting to create a loop to display the shopping cart total on my checkout page. I attempted to create some array variables in the loop to hold my values but console give me a "
unexpected [ " error.
* My "i" values attached to "p" & "q" are meant to reference IDs in my shopping cart table for the products displayed.
shopping cart table code:
<?php
$query = "select * from tbl_cart WHERE
member_id= '$member_id'";
$result = mysqli_query($conn,$query);
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table id='anyclass'>";
echo "<tr>
<th style= border-right-style:none;
id='image1'></th>
<th style= text-align: center;
border-left-style:none;
id='product1'>Product</th>
<th id='price1'>Price</th>
<th id='quantity1'>Quantity</th>
<th id='remove1'>Remove</th>
</tr>";
$i=1;
while($row = $result->fetch_assoc()) {
echo "<tr class= 'trhide'>";
echo '<td style=border-right-
style:none;><img src="' . $row['image']
. '" /></td>';
echo "<td style=border-left-
style:none;
id='product'>".$row['product_id']."</td>";
echo "<td style= 'text-align: right;' id=
'p$i' >".$row['price']."
</td>";
echo "<td id= 'quantity' >". "
<input id= 'q$i' style='border:none'
type = 'text' class= 'change' value=
$row[quantity] name= $row[id] data-id=
$row[id]
>" . "</td>";
echo "<td id= 'delete'><span
class='delete' data-id= $row[id] ><img
src=delete.svg></span></td>";
echo "<td id= 'o$i' >".$row['oz']."
</td>";
echo "</tr>";
$i++;
}
echo "<tr>
<td>Total:</td>
<td id= 'total'></td>
</tr>";
echo "</table>";
?>
<script>
var total0=0;
var sum=0;
var i;
for (i = 1; i < 1000; i++) {
if(document.getElementById("p"+i)==null){
var p[i]= 0;
}
else{varp[i]=parseFloat
(document.getElementById
('p'+i).innerText)*100;}
if(document.getElementById('q'+i)==null){
var q[i]= 0;
}
else{var q[i]=
document.getElementById('q'+i).value;
}
var total[i]= parseInt(p[i]) *
parseInt(q[i]);
var sum= sum + total[i];
}
window.alert(sum);
</script>
I'm not sure if this is good code but it worked for me and solved my problem.
<script>
var total0=0;
var sum=0;
var i;
var q = new Array();
var p = new Array();
var total = new Array();
for (i = 1; i < 1000; i++) {
if(document.getElementById("p"+i)==null){
p[i]= 0;
}
else{p[i]=parseFloat(document.getElementById('p'+i)
.innerText)*100;
}
if(document.getElementById('q'+i)==null){
q[i]= 0;
}
else{q[i]= document.getElementById('q'+i).value;
}
total[i]= parseInt(p[i]) * parseInt(q[i]);
var sum= sum + total[i];
}
var result= sum/100;
document.getElementById('total').innerHTML = '$' +
result;
Related
enter image description here
Please help me..
auto fill works for the first field
but I have problem in the second row and next row too. When I change the select option on second row, the previous field changed (not same row as what I have changed):
enter image description here
var Nomor = $('#TabelTambahBarang tbody tr').length + 1;
var Baris = "<tr>";
Baris += "<td class='txt-center'>"+Nomor+"</td>"var Baris = "<tr>";
Baris += "<td class='txt-center'>"+Nomor+"</td>";
Baris += "<td>";
Baris += "<select placeholder='Items Name' class='form-control input-sm select2' name='nama_barang[]' id='nama_barang" + Nomor + "' data-Nomor='" + Nomor + "'>";
Baris += "<option disabled value='' name='nama_barang[]' id='nama_barang" + Nomor + "' data-Nomor='" + Nomor + "' selected hidden>-- Silahkan Nama Barang --</option>";;enter code here
<?php
if (!empty($dataBarang)) {
foreach ($dataBarang as $k) { ?>
Baris += "<option value='<?php echo $k->nama_barang; ?>'><?php echo $k->nama_barang; ?></option>";
<?php }
}
?>
Baris += "</select>";
Baris += "</td>";
Baris += "<td class='txt-center'><input class='form-control input-sm' id='harga_barang' placeholder='Harga Barang' name='harga_barang'/></td>";
Baris += "<td align='center'><a id='HapusBaris'>Delete</a></td>";
Baris += "</tr>";
$('#TabelTambahBarang tbody').append(Baris);
Nomor++;
select2();
}
$(document).on("change", ".select2", function(e) {
var select = $(this);
var nama_barang = select.val();
$.ajax({
type : "POST",
url : "<?php echo base_url('get_barang')?>",
dataType : "json",
data : {nama_barang: nama_barang},
cache:false,
success: function(data){
$.each(data,function(harga_barang){
$('[id="harga_barang"]').val(data.harga_barang);
});
}
});
});
function select2(){
$('.select2').select2()
}
Controller
public function get_barang(){
$nama_barang = $this->input->post('nama_barang');
$data = $this->m_testing->get_barang($nama_barang);
echo json_encode($data);
}
Model
function get_barang($nama_barang){
$hsl = $this->db->query("SELECT * FROM tabel_barang WHERE nama_barang='$nama_barang'");
if($hsl->num_rows()>0){
foreach ($hsl->result() as $data) {
$hasil=array(
'nama_barang' => $data->nama_barang,
'harga_barang' => $data->harga_barang,
);
}
}
return $hasil;
}
I need help with this for loop. I want it to print controls on the side of the row for each item listed but its listing 5 instead of 2 sets of controls.
<tbody>
<?php
//get list of supplies
$numOfRows = 0;
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';
?>
</tbody>
review your code, second for is using numOfRows variable which is not right at all, you are iterating as many rows your table have is not just 5 times, if you have 100 records then you'll see that TD printed 100 times, BTW you can put those 2 buttons into a single TD that way you will not need the second for at all
I don't think there is another loop required for controls. Try this i hope it'll help you out. Thanks
<?php
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
}
echo '</tr>';
?>
If you want each item on it's own row showing the item ($item) in the first column and the edit/delete links in the 2nd column you only need the while to go through each row found.
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td>' . $item . '</td>';
echo '<td>
<a><i class="fas fa-edit"></i></a>
<a><i class="fas fa-trash-alt"></i></a>
</td>';
echo '</tr>';
}
// In your code you have the
// "<tr>" element inside of your loop.
// It needs to come before your loop. Try the code below
// This is where you need <tr>
echo "<tr>";
while ($row = mysqli_fetch_assoc($result)) {
// this is where you had tr // echo <tr>
foreach ($row as $item) {
echo '<td>' . $item . '</td>';`enter code here`
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';
I need to check/uncheck a table checkbox based on a value stored in a database. I'm not an experienced programmer. I've been Google-ing this for a whole day with no luck. Hope someone's going to help me.
Here's the code :
echo "<table data-toggle='table' data-click-to-select='true' id='potable'>";
echo "<thead>";
echo "<tr>";
echo "<th data-field='state' data-checkbox='true'></th>";
echo "<th data-field='code'>Code</th>";
echo "<th data-field='description'>Description</th>";
echo "<th data-field='verb'>Verb</th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>"; ?>
<script>
var select = <?php echo "{$selected}"; ?>;
if (select == 1) {
// INSERT HERE JAVASCRIPT LINE THAT CHECKS THE CURRENT CHECKBOX
else {
//UNCHECK THE CURRENT CHECKBOX
}
</script>
<?php
echo "</td>";
echo "<td>{$code}</td>";
echo "<td>{$description}</td>";
echo "<td>{$verb}</td>";
echo "<a type='button' class='btn btn-primary left-margin' href='po-info.php?id={$id}'>Info</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Finally managed to make it work
Used
echo "<th data-field='selected' data-checkbox='true' data-formatter='stateFormatter'></th>";
then
<script>
function stateFormatter(value, row, index) {
if (value == 1) {
return {
checked: true
};
} else {
return {
checked: false
};
}
return value;
}
</script>
Try
$("th[data-checkbox='true'] input[type=checkbox]").attr('checked', <?=$selected?'true':'false'?>);
<table>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$checked_ids = array();
$checked_ids[] = $id;
$chk_str = '<input data-index="%s" name="chk" type="checkbox">';
echo "<tr>";
echo "<td>";
printf($chk_str, $id);
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="hidden" id="db_checked" value="<?php echo implode(",",$checked_ids); ?>" />
<script>
// need jquery.js
$(function () {
var $table = $('#potable'),
var string = $('#db_checked').val();
var array = string.split(',');
for (var ck_id in array) {
$table.bootstrapTable('check', parseInt(ck_id, 10));
// check or uncheck
}
}
</script>
maybe "data-index" is write at "tr" tags
I have used this function to pop up a window, when a user clicked the table row
<script type="text/javascript">
$(document).ready(function() {
$("td.abc").click(function() {
var t = 'Ticket ID: ' + $(this).find('td:eq(0)').html()+'\n'+'\n';
var r = 'Subject: ' + $(this).find('td:eq(1)').html()+'\n'+'\n';
var e = 'Messege: ' + $(this).find('td:eq(2)').html()+'\n'+'\n';
var f = 'Developer: ' + $(this).find('td:eq(3)').html()+'\n'+'\n';
var w = 'Current Status: ' + $(this).find('td:eq(4)').html()+'\n'+'\n';
var q = 'Uploaded Date & Time: ' + $(this).find('td:eq(5)').html()+'\n'+'\n';
var o = $("#79").attr("name")+'\n'+'\n';
alert(t+r+e+f+w+q+o);
});
});
</script>
my table is as follows
echo "<br> <table border='0' id='example' width='980' align='center'>
<tr>
<th width='60'>Ticket ID</th>
<th width='150'>Subject</th>
<th width='670'>Messege</th>
<th width='60'>Developer</th>
<th width='70'>Status</th>
<th width='105'>Date - Time</th>
</tr>";
while($row = mysql_fetch_array($result)) {
//echo $row['jobSatus'];
echo "<tr>";
echo "<td class=\"abc\" width='60'>" . $row['id'] . "</td>";
echo "<td class=\"abc\" width='150'>" . $row['subject'] . "</td>";
echo "<td class=\"abc\" width='670'>" . $row['msg'] . "</td>";
echo "<td class=\"abc\" width='60'>" . $row['developer'] . "</td>";
echo "<td class=\"abc\"width='60'>" . $row['jobstatus'] . "</td>";
echo "<td class=\"abc\" width='105'>" . $row['date_time'] . "</td>";
echo "<input class=\"abc\" type='hidden' name=".$row['image']." id=".$row['id'].">";
echo "</tr>";
}
echo "</table>";
Now inside the pop window I don't get any values. It says "undefined". I would be very thankful if someone could show what I have done wrong.
try to change this...
var t = 'Ticket ID: ' + $(this).find('td:eq(0)').html()+'\n'+'\n';
to this
var t = 'Ticket ID: ' +$(this).closest('td:eq(0)').attr('class');
Hope this will help....
You are clicking on a table cell, not a table row
$("td.abc").click(function() {
^^
Change it to look at the table
$("#example").on("click", "tr", function () {
or
$("#example").on("click", "tr:has(td.abc)", function () {
And your HTML markup is invalid since an input element can not be a child element of a tr.
Set onclick on table-row
$("tr.ticker-row").click(function(ev) {
var $this = $(ev.currentTarget);
var t = 'Ticket ID: ' + $this.find('td:eq(0)').html()+'\n'+'\n';
var r = 'Subject: ' + $this.find('td:eq(1)').html()+'\n'+'\n';
var e = 'Messege: ' + $this.find('td:eq(2)').html()+'\n'+'\n';
var f = 'Developer: ' + $this.find('td:eq(3)').html()+'\n'+'\n';
var w = 'Current Status: ' + $this.find('td:eq(4)').html()+'\n'+'\n';
var q = 'Uploaded Date & Time: ' + $this.find('td:eq(5)').html()+'\n'+'\n';
var o = $this.find('input').attr('name') + '\n'+'\n';
alert(t+r+e+f+w+q+o);
});
On php, do not insert any node in tr except td
while($row = mysql_fetch_array($result)) {
//echo $row['jobSatus'];
echo "<tr class=\"ticket-row\" data-row-index=\"".$row['id']."\">";
echo "<td class=\"abc\" width='60'>" . $row['id'] . "<input class=\"abc\" type='hidden' name=".$row['image']." ></td>";
echo "<td class=\"abc\" width='150'>" . $row['subject'] . "</td>";
echo "<td class=\"abc\" width='670'>" . $row['msg'] . "</td>";
echo "<td class=\"abc\" width='60'>" . $row['developer'] . "</td>";
echo "<td class=\"abc\"width='60'>" . $row['jobstatus'] . "</td>";
echo "<td class=\"abc\" width='105'>" . $row['date_time'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to create a javascript that will change the value of anotif to 0 once clicked:
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
But I am not sure how do that, considering that I'm so new in javascript. I've googled around a bit but I wasn't able to find the answer.
function validator(){
if(document.anotif.clicked){ // I'm not really sure if this is correct.
// if clicked, change value to zero
}
To be more precise, I want it to look like this once clicked:
From:
Approved:3
To:
Approved:0
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
This code will lead me to ?anotif for me to view some data from the database in a table. Once approved: is clicked, it must be set to zero at the same time I move to the other page.
The problem is it requires refreshing for it to be set to 0 by the system.
Here is the rest of the code:
$firstname = getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.<br/>';
$anotif = 0;
$dnotif = 0;
$anotif = $anotif + getuserfield('approved_notif');
$dnotif = $dnotif + getuserfield('disapproved_notif');
?>
<h3>Notifications:</h3>
Approved:<?php if($anotif<1){echo 0;} else { echo '<a class="anotif" onClick="validator(this);" href="?anotif">'.$anotif.'</a>';}?><br/> //problem is over here
<?php
if(isset($_GET['anotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 1 AND checked = 0";
$query_run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$anotif = 0;
$query3 = "UPDATE hrms_emp_info SET approved_notif=$anotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
?>
Disapproved:<?php if($dnotif<1){echo 0;} else { echo ''.$dnotif.'<br/>';} ?> //and here
<?php
if(isset($_GET['dnotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 2 AND checked=0";
$query_run = mysql_query($query);
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$dnotif = $dnotif-1;
$query3 = "UPDATE hrms_emp_info SET disapproved_notif=$dnotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
You could change your anchor a bit like this:
<a class="anotif" onClick="return validator(this)" href="?anotif">
Then change the validator() function like this:
function validator(element)
{
// element is clicked already, so you can change its value
element.innerHTML = '0';
// prevent click from changing page
return false;
}
However, the real problem here is that the page will be changed to ?anotif and whatever you changed will be wasted effort.
Updated the code with a way to prevent the click from changing the page and just reducing the number to '0'.