I have a form where I'm submitting some values (from checkboxes) to another page and on that page I'm using an explode function to break the string inside the array. But I'm getting an additional (+1) value when I put the count() function on the explode.
HTML
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
</form>
PHP (on page 2)
$prd = implode(",",$_POST['addcart']);
$final = explode(",", $prd);
for($i=0; $i<=count($final); $i++)
{
echo count($final); //this is where I'm getting the +1 to original count and hence everything falls apart.
echo $final[$i];
}
Note: I have include all the essential files like config.php and everything in the PHP file already.
Why that much extra code, directly do like below:-
foreach($_POST['addcart'] as $val){
echo $val;
}
Since count() starts counting from 1 and for loop starting from 0.
Replace this line:
for($i=0; $i<=count($final); $i++)
with
for($i=0; $i<=count($final)-1; $i++)
Related
I have this table into a modal form:
<div class="form-group">
<!-- Record list -->
<table id="tabscarichi" class="table table-bordered table-striped" style='border-collapse: collapse;' >
<tr style='background: whitesmoke;'>
<th>Check</th>
<th>Id</th>
<th>Formulario</th>
<th>EER</th>
<th>Data FR</th>
<th>Q.tà</th>
</tr>
<?php
$query = "SELECT * FROM registro_c_s where stato_carico='1'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result) ){
$id = $row['id'];
$fr = $row['formulario'];
$data = $row['data_fr'];
$eer = $row['codice_eer'];
$qta = $row['quantità'];
$stato = $row['stato_carico'];
?>
<tr>
<!-- Checkbox -->
<td><input type='checkbox' class="box" name='update[]' value='<?= $id ?>' ></td>
<td><?= $id ?></td>
<td><?= $fr ?></td>
<td><?= $eer ?></td>
<td><?= $data ?></td>
<td class="qta_scarichi"><?= $qta ?></td>
</tr>
<?php
}
?>
</table>
</div>
I'm trying to write a script to sum values of class .qta_scarichi and reporting it in another input number with .qtas class.
I tried this:
$('.box').change(function(){
var total = 0;
$('.box:checked').each(function(){
total+=parseFloat($(this).parent().next('td').find('.qta_scarichi').text());
});
$('#qtà').val(total);
});
But when i check some rows..nothing happen.. #qtà is the id of the input number that i want to fill..
Problem found! in table with id="tabscarichi" i have the following script:
$(function() {
$('#tabscarichi').hide();
$('#tipo').change(function(){
if($('#tipo').val() == 'scarico') {
$('#tabscarichi').show();
console.log('0'); }
else { $('#tabscarichi').hide();
} }); }); </script> ```
i choose to show the table with a select... if i disable the script.. the css selector works! But i don't understand why..
$(this).parent().next('td') just selects <td><?= $id ?></td>, which doesn't contain .qta_scarichi.
Use $(this).closest('tr').find('.qta_scarichi').
I suspect you may have intended ".siblings('td'). .next()` only selects the immediately following element.
I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>
I'm struggeling with this one. How can list all devices, their model and supplier from my database? The data is stored comma-separated in the fields device, model and supplier.
<?php
while ($row = mysqli_fetch_array($result)) {
$device_explode = explode(',', $row['device']);
$model_explode = explode(',', $row['model']);
$supplier_explode = explode(',', $row['supplier']);
$device = array(array($device_explode[0]), //count function? How would I do that?
array($model_explode[0]),
array($supplier_explode[0])
);
echo "<table border='1'>
<tr>
<th>Møterom</th>
<th>Romnummer</th>
<th>Lokasjon</th>
<th>Antall sitteplasser</th>
<th>Tilgjengelig utstyr</th>
<th>Bilde av rommet</th>
<th>Endre</th>
<th>Slett</th>
</tr>
<tr>
<td>".$row['name']."</td>
<td>".$row['roomnbr']."</td>
<td>".$row['location']."</td>
<td>".$row['seats']."</td>
<td>".$device[0][0]."<br>".$device[1][0]."<br>".$device[2][0]."<br></td> //also count?
<td><img src='../img/uploads/".$row['img']."' width='150px' ></td>
<td><a href='?p=editconfroomv2&id=".$row['id']."'>Endre</a></td>
<td><a href='./index.php?p=deleteconfroomdb&id=".$row['id']."' class='delete'>Slett</a></td>
</tr>
</table>";
}
?>
It can be done this way (if I understand correct your table structure). I assume, that $device_explode, $model_explode and $supplier_explode have equal number of elements:
<?php
// Table Header
echo "<table border='1'>
<tr>
<th>Møterom</th>
<th>Romnummer</th>
<th>Lokasjon</th>
<th>Antall sitteplasser</th>
<th>Tilgjengelig utstyr</th>
<th>Bilde av rommet</th>
<th>Endre</th>
<th>Slett</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
$device_explode = explode(',', $row['device']);
$model_explode = explode(',', $row['model']);
$supplier_explode = explode(',', $row['supplier']);
// Table rows
$td = '';
$td .=
"<tr>
<td>".$row['name']."</td>
<td>".$row['roomnbr']."</td>
<td>".$row['location']."</td>
<td>".$row['seats']."</td>
<td>";
for ($i = 0; $i < count($device_explode); $i++) {
$td .=$device_explode[$i]." ".$model_explode[$i]." ".$supplier_explode[$i]."<br>";
}
$td .=
"</td>
<td><img src='../img/uploads/".$row['img']."' width='150px' ></td>
<td><a href='?p=editconfroomv2&id=".$row['id']."'>Endre</a></td>
<td><a href='./index.php?p=deleteconfroomdb&id=".$row['id']."' class='delete'>Slett</a></td>
</tr>";
echo $td;
}
echo "</table>";
?>
I would suggest looping trough the arrays in the markup and generating the output using the html templating syntax and alternative control structures.
Some things i have done[not related directly to the question]:
switched the array() syntax for the shorthand [can be done since version 5.4]
switched from using a single echo to utilizing the html templating syntax [by expluding non dynamic parts from php and echoing the dynamic parts in]
Switched from using the echo statement to using the shorthand
Switched to alternative control structures
PHP documentation for strings
<?php
while ($row = mysqli_fetch_array($result)) {
$device_explode = explode(',', $row['device']);
$model_explode = explode(',', $row['model']);
$supplier_explode = explode(',', $row['supplier']);
//I am assuming you want to loop trough all exploded fields related to each row
$devices = [
$device_explode,
$model_explode,
$supplier_explode
];
?>
<?php while(($row = mysqli_fetch_array($result))) : ?>
<table border='1'>
<tr>
<th>Møterom</th>
<th>Romnummer</th>
<th>Lokasjon</th>
<th>Antall sitteplasser</th>
<th>Tilgjengelig utstyr</th>
<th>Bilde av rommet</th>
<th>Endre</th>
<th>Slett</th>
</tr>
<tr>
<td><?= $row['name'] ?></td>
<td><?= $row['roomnbr'] ?></td>
<td><?= $row['location'] ?></td>
<td><?= $row['seats'] ?></td>
<td>
<?php foreach ($devices as $deviceSpecs) : //Here we loop trough the first dimension fo the array ?>
<?php foreach ($deviceSpecs as $deviceSpec): //Here we loop trough the second dimension ?>
<?= $deviceSpec ?><br>
<?php endforeach; ?>
<?php endforeach; ?>
</td>
<td><img src='../img/uploads/<?= $row['img'] ?>' width='150px' ></td>
<td><a href='?p=editconfroomv2&id=<?= $row['id'] ?>>Endre</a></td>
<td><a href='./index.php?p=deleteconfroomdb&id=<?= $row['id'] ?>' class='delete'>Slett</a></td>
</tr>
</table>
<?php endwhile; ?>
I am facing trouble in using $_SESSION when I open viewrcd.php file it opens directly instead of opening admin_login.php page. (b'coz to view this page "viewrcd.php" admin logged in is must...)
admin_login.php
<?php
session_start();
require 'testdbcheking.php';
if(isset($_POST['login']))
{
$a_name = $_POST['admin_name1'];
$a_pword = $_POST['admin_password1'];
$_SESSION['admin_name1'] = 'admin_name1';
if(!empty($_POST['admin_name1']) && !empty($_POST['admin_password1']))
{
$query10="SELECT * FROM admin_login WHERE admin_name = '$a_name' AND admin_password = '$a_pword'";
if($query10_run = mysql_query($query10))
{
$query10_num_rows = mysql_num_rows($query10_run);
if($query10_num_rows == 0)
{
echo 'you are not an admin';
}
if($query10_num_rows == 1)
{
echo "<script>window.open('viewrcd.php?logged= Admin logged in Success','_self')</script>";
}
}
}
else
{
echo 'provide both......';
}
}
?>
<html>
<title>Admin Login Page</title>
<body>
<form action = 'admin_login.php' method = 'POST' >
<table align = 'center' border = '3' bgcolor = 'gray'>
<tr ><td colspan = 8 align ='center'>Admin Login FORM</td></tr>
<tr>
<td>Admin Username:</td>
<td><input type = 'text' name ='admin_name1'></td>
</tr>
<tr>
<td>Admin Password:</td>
<td><input type = 'password' name ='admin_password1'></td>
</tr>
<tr> <td colspan = '4' align ='center'>
<input type = 'submit' name='login' value = 'Login'>
</td>
</tr>
</table>
</form>
</body>
</html>
viewrcd.php
<?php
session_start();
require 'testdbcheking.php';
if($_SESSION['admin_name1'])
{
//echo 'qqq';
header('location :admin_login.php');
//header('location : viewrcd.php');
}
?>
<a href= 'admin_logout.php'>ADMIN_logout </a>
<html>
<head><center><h1>Viewing all the rocords</center></h1><head/>
<body>
<table align = center border = 1>
<tr align = 'center'>
<th>SR.NO.</th>
<th>Username</th>
<th>Firstname</th>
<th>Surname</th>
<th>Delete</th>
<th>Edit</th>
<th>Details</th>
</tr>
<?php
$query4 = 'SELECT * FROM users';
$query4_run = mysql_query($query4);
while (#$data_row = mysql_fetch_array($query4_run))
{
$u_id = $data_row [0];
$u_name = $data_row[1];
$f_name = $data_row [3];
$s_name = $data_row [4];
?>
<tr align = 'center'>
<td> <?php echo #$u_id;?></td>
<td> <?php echo #$u_name;?></td>
<td> <?php echo #$f_name;?></td>
<td> <?php echo #$s_name;?></td>
<td><a href = 'delete.php?del=<?php echo $u_id ;?>'>Delete</a></td>
<td><a href = 'edit.php?edit=<?php echo $u_id ;?>'>Edit</a></td>
<td><a href = 'viewrcd.php?details=<?php echo $u_id ;?>'>Details</a></td>
<?php } ?>
</tr>
</table>
<?php
$record_details = #$_GET['details'];
$query8 = "SELECT * FROM users WHERE id = '$record_details'";
$query_run8 = mysql_query($query8);
while($query_run8_row = mysql_fetch_array($query_run8))
{
$d_name = $query_run8_row[1];
$d_firstname = $query_run8_row[3];
$d_surname = $query_run8_row[4];
?>
<table align = center border = 1>
</tr>
<tr ><td colspan = 10 align = center> your details are here </td></tr>
<tr align = center>
<td><?php echo $d_name ; ?></td>
<td><?php echo $d_firstname ; ?></td>
<td><?php echo $d_surname ; ?></td>
</tr>
<?php } ?>
</table>
<font bgcolor = 'green' align = 'center' size = '6'>
<?php echo #$_GET['deleted'];?>
</font>
<font color = 'green' align = 'center' size = '6'>
<?php echo #$_GET['updated'];?>
<?php echo #$_GET['logged'];?>
</font><br><br><br><br><br>
<form action = "viewrcd.php" method = "get">
Search record: <input type = "text" name = "search" >
<input type = "submit" name = "submit" value ="Find Record">
</form>
<?php
if (isset($_GET['search']))
{
$search_record = $_GET['search'];
$query9 = "SELECT * FROM users WHERE id = '$search_record' OR firstname ='$search_record' ";
$query9_run = mysql_query($query9);
while($query9_run_row = mysql_fetch_assoc($query9_run))
{
//$find_id = $query9_run_row['id'];
$find_username = $query9_run_row['username'];
$find_firstname = $query9_run_row['firstname'];
$find_surname = $query9_run_row['surname'];
?>
<br><br><br>
<table border = '2' bgcolor ='green' align ='center'>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Surname</th>
<tr>
<td><?php echo $find_username ; ?></td>
<td><?php echo $find_firstname ; ?></td>
<td><?php echo $find_surname ; ?></td>
</tr>
</table>
<?php } } ?>
</body>
</html>
admin_logout.php
<?php
require 'testdbcheking.php';
session_start();
session_destroy();
header('Location: admin_login.php');
?>
replace
if($_SESSION['admin_name1'])
with
if(!$_SESSION['admin_name1'])
in viewrcd.php
replace
if($_SESSION['admin_name1'])
with
if(empty($_SESSION['admin_name1']))
i have updated my answer ..try now
admin_login.php
<?php
session_start();
require 'testdbcheking.php';
global $a_name;
if(isset($_POST['login']))
{
$a_name = $_POST['admin_name1'];
$a_pword = $_POST['admin_password1'];
if(!empty($_POST['admin_name1']) && !empty($_POST['admin_password1']))
{
$query10="SELECT * FROM admin_login WHERE admin_name = '$a_name' AND admin_password = '$a_pword'";
if($query10_run = mysql_query($query10))
{
$query10_num_rows = mysql_num_rows($query10_run);
if($query10_num_rows == 0)
{
echo 'you are not AN admin';
}
if($query10_num_rows == 1)
{
$admin_id = mysql_result($query10_run,0,'admin_name');
$_SESSION['admin_id'] = $admin_id;
echo "<script>window.open('viewrcd.php?logged= Admin logged in Success','_self')</script>";
}
}
}
else
{
echo 'provide both......';
}
}
?>
<br><br><br>
<?php //echo $a_name ; ?>
<html>
<title>Admin Login Page</title>
<body>
<form action = 'admin_login.php' method = 'POST' >
<table align = 'center' border = '3' bgcolor = 'gray'>
<tr ><td colspan = 8 align ='center'>Admin Login FORM</td></tr>
<tr>
<td>Admin Username:</td>
<td><input type = 'text' name ='admin_name1'></td>
</tr>
<tr>
<td>Admin Password:</td>
<td><input type = 'password' name ='admin_password1'></td>
</tr>
<tr> <td colspan = '4' align ='center'>
<input type = 'submit' name='login' value = 'Login'>
</td>
</tr>
</table>
</form>
</body>
</html>
viewrcd.php
<?php
session_start();
require 'testdbcheking.php';
if(!isset($_SESSION['admin_id']))
{
?>
First Login here..
<?php
}
else
{
?>
<a href= 'admin_logout.php'>ADMIN_logout </a>
<html>
<head><center><h1>Viewing all the rocords</center></h1><head/>
<body>
<table align = center border = 1>
<tr align = 'center'>
<th>SR.NO.</th>
<th>Username</th>
<th>Firstname</th>
<th>Surname</th>
<th>Delete</th>
<th>Edit</th>
<th>Details</th>
</tr>
<?php
$query4 = 'SELECT * FROM users';
$query4_run = mysql_query($query4);
while (#$data_row = mysql_fetch_array($query4_run))
{
$u_id = $data_row [0];
$u_name = $data_row[1];
$f_name = $data_row [3];
$s_name = $data_row [4];
?>
<tr align = 'center'>
<td> <?php echo #$u_id;?></td>
<td> <?php echo #$u_name;?></td>
<td> <?php echo #$f_name;?></td>
<td> <?php echo #$s_name;?></td>
<td><a href = 'delete.php?del=<?php echo $u_id ;?>'>Delete</a></td>
<td><a href = 'edit.php?edit=<?php echo $u_id ;?>'>Edit</a></td>
<td><a href = 'viewrcd.php?details=<?php echo $u_id ;?>'>Details</a></td>
<?php } ?>
</tr>
</table>
<?php
$record_details = #$_GET['details'];
$query8 = "SELECT * FROM users WHERE id = '$record_details'";
$query_run8 = mysql_query($query8);
while($query_run8_row = mysql_fetch_array($query_run8))
{
$d_name = $query_run8_row[1];
$d_firstname = $query_run8_row[3];
$d_surname = $query_run8_row[4];
?>
<table align = center border = 1>
</tr>
<tr ><td colspan = 10 align = center> your details are here </td></tr>
<tr align = center>
<td><?php echo $d_name ; ?></td>
<td><?php echo $d_firstname ; ?></td>
<td><?php echo $d_surname ; ?></td>
</tr>
<?php } ?>
</table>
<font bgcolor = 'green' align = 'center' size = '6'>
<?php echo #$_GET['deleted'];?>
</font>
<font color = 'green' align = 'center' size = '6'>
<?php echo #$_GET['updated'];?>
<?php echo #$_GET['logged'];?>
</font><br><br><br><br><br>
<form action = "viewrcd.php" method = "get">
Search record: <input type = "text" name = "search" >
<input type = "submit" name = "submit" value ="Find Record">
</form>
<?php
if (isset($_GET['search']))
{
$search_record = $_GET['search'];
$query9 = "SELECT * FROM users WHERE id = '$search_record' OR firstname ='$search_record' ";
$query9_run = mysql_query($query9);
while($query9_run_row = mysql_fetch_assoc($query9_run))
{
//$find_id = $query9_run_row['id'];
$find_username = $query9_run_row['username'];
$find_firstname = $query9_run_row['firstname'];
$find_surname = $query9_run_row['surname'];
?>
<br><br><br>
<table border = '2' bgcolor ='green' align ='center'>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Surname</th>
<tr>
<td><?php echo $find_username ; ?></td>
<td><?php echo $find_firstname ; ?></td>
<td><?php echo $find_surname ; ?></td>
</tr>
</table>
<?php } } ?>
</body>
</html>
<?php }?>
admin-logout.php
<?php
session_start();
// session_destroy();
unset($_SESSION['admin_id']);
header('Location: loginform.php');
?>
I'm trying to display my products from the database. I'm trying to split it using the if 4%=0, but I can't get it to display 4 items in a row.
Here is my codes:
<table>
<tr>
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
?>
<td>
<table class="normal_text" align="center">
<tr>
<td><a href="product.php?product_id=<?php echo $id?>">
<img width="200px" height="133px" src="products/<?php echo $product_code?>.jpg" />
</a></td>
</tr>
<tr>
<td align="center" style="font-weight:bold"><?php echo $title;?></td>
</tr>
<tr>
<td align="center">$<?php echo $price;?></td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You've got the PHP logic before, rather than inside your HTML table output.
Try reorganizing like this:
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
?>
<table class="normal_text" align="center">
<tr>
<?php
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
// output product details -- note use of quotes to include PHP vars
$rowHTML = "<td><a href='product.php?product_id=$id'>";
$rowHTML .= "<img width='200px' height='133px' src='products/$product_code.jpg' />";
$rowHTML .= "</a><br/>";
$rowHTML .= "<strong>$title</strong>";
$rowHTML .= "$price</td>";
echo $rowHTML;
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
}
?>
</tr>
</table>
while($row=mysql_fetch_assoc($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
if ($split % 4 === 0) echo "\n<tr>";
?>
// ONLY <td> CODE </td> here. NO <tr> and NO </table>
<?php
if ($split % 4 === 3) echo "</tr>";
$split++;
}
?>
You are starting and ending at same time and then putting table for each.
You want to start <tr> when %4==0 and end </tr> when %4==3.
Rather than use modulus (%), just add a second variable called $cols, assign it to the amount of columns you want, and compare it. For example:
$rows = 1;
$cols = 4;
$display_all = mysql_query("SELECT * FROM products ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($rows==$cols){
echo '</tr><tr>';
$rows = 1;
}