$result = mysql_query("SELECT * FROM marias_products WHERE product_id = $id ");
$projects = array();
while($rowrow = mysql_fetch_assoc($result)){
$projects[] = $rowrow;
}
foreach ($projects as $project) {
$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");
while($rows = mysql_fetch_array($resultq))
{
$qwerty=$rows['qtyleft'];
}
if ($qwerty!=0)
{
// echo '<input type="text" name="pizasize" value="'.$project['product_size_name'].'"/>';
echo '<input hidden type="text" name="idd" value="'.$project['mp_id'].'"/>';
echo '<tr>';
echo '<td>'.$project['product_size_name'].'</td>';
echo '<td>'.$project['product_price'].'</td>';
echo '<td>'.$project['mp_id'].'</td>';
echo '<td>'.'<input name="but" type="image" value="'.$project['mp_id'].'" src="images/button.png" />'.'</td>';
echo '</tr>';
}
else
{
echo 'not available';
}
}
I'm having a problem with getting the selected 'mp_id' inside in foreach loop. What happen is that once I select by clicking the button. I've selected the last ID of the entire table. Hope someone could help. Ty. Just learning PHP.
When i select a product in my gallery it goes to my cart.
I get my product id and store them in a session.
And in my cart i got to select size and which fabric i want to order my product.
My product is cushion.
My cart.php has all function to make the cart session work,
and has all the buttons functions too.
I display my selected products in my page checkout.php ... i call my cart function in that page...
To select size and fabric i have 2 dropdowns, the size dropdown is static html options, and my fabrics are getting data from my DB.
Look this image before...
As you see i got these two tables.
When i select my fabric and it size i want to fetch the value in the fabrics prices table.
My cart a got a session where it grabs the user state.
If the user is in the same state that i am,the price of the fabric will be one cost else it will be other cost.(IN_state,Out_state columns).
My QNTD row in the cart table is quantity $value
Valor is value of costs in pt-br.
A bad thing is that my dropdown dont save the selected option.
lets say that i selected a fabric Velvet and size 50x50.
And i click on add or less button my page will refresh and all options will be lost.
What i did is store the selected option using LocalStorage.
It works only for the first row in the checkout.
How can i get all these data to work with out submitting something.
Maybe i can use more sessions to get all data and make the calculation.
But im a bit lost in all this.
What can i do to get this working all together ?
my cart code..
<?php
include_once '../incluedes/conn_cms.php';
// gets the product id from gallery and store in session and add +1
if(isset($_GET['add'])){
$select = "SELECT * FROM gallery2 WHERE id=" . escape_string($_GET['add'])." ";
$run_selection = mysqli_query($conn,$select);
while($rows = mysqli_fetch_assoc($run_selection)){
if($rows['id'] != $_SESSION['product_'.$_GET['add']]){
$_SESSION['product_' . $_GET['add']]+=1;
header('Location: index.php');
}else{
$msg = "error";
header('Location: checkout.php');
}
}
}
//the plus button
if(isset($_GET['plus'])){
$_SESSION['product_'.$_GET['plus']]+=1;
if($_SESSION['product_'.$_GET['plus']] < 1){
header('Location: checkout.php');
}else{
header('Location: checkout.php');
}}
//the minus button
if(isset($_GET['remove'])){
$_SESSION['product_'.$_GET['remove']]--;
if($_SESSION['product_'.$_GET['remove']] < 1){
header('Location: checkout.php');
}else{
header('Location: checkout.php');
}
}
//the remove button
if(isset($_GET['delete'])){
$_SESSION['product_'.$_GET['delete']] = '0';
header('Location: checkout.php');
}
function cart(){
global $conn;
$fabric_options = '';
$query2 = "SELECT * FROM almofadas";
$result = mysqli_query($conn,$query2);
while($rows = mysqli_fetch_assoc($result)){
$fabric_options .= "<option value=''>{$rows['tecido']}</option>";
}
foreach ($_SESSION as $name => $value) {
if($value > 0){
if(substr($name, 0, 8 ) == "product_"){
$length = strlen($name) -8;
$item_id = substr($name,8 , $length);
$query = "SELECT *
FROM gallery2
WHERE gallery2.id =".escape_string($item_id). "";
$run_item = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($run_item)){
$vari = $rows['variante'];
$num = $rows['title'];
$id = $rows['id'];
$btn_add='<a class="btn btn-success" href="cart.php?plus='.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></a>';
$btn_remove = '<a class="btn btn-warning" href="cart.php?remove='.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></a>';
$btn_delete='<a class="btn btn-default delete_btn" href="cart.php?delete='.$id.'"><i class="fa fa-times fa-lg" aria-hidden="true"></i></a>';
if($rows['variante'] < 1){
$vari="";
}else{
$vari = "-".$rows['variante'];
}
$product = '
<td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
<td>'.$num.''.$vari.'</td>
<td style="width:15%;">
<select id="fabric" class="form-control selectpicker" required="" >
'. $fabric_options . '
</select>
</td>
<td>
<select id="size" class="form-control selectpicker" required style="width:80%;" >
<option value="50">50x50</option>
<option value="45">45x45</option>
</select>
</td>
<td>'.$value.'</td>
<td>R$</td>
<td>sub.total</td>
<td>
'.$btn_add.' '.$btn_remove.' '.$btn_delete.'
</td>
</tr>';
echo $product;
}
}
}
}
}
My LocalStorage script in checkout.php
<script >
$(function() {
if (localStorage.getItem('fabric')) {
$("#fabric option").eq(localStorage.getItem('fabric')).prop('selected', true);
}
$("#fabric").on('change', function() {
localStorage.setItem('fabric', $('option:selected', this).index());
});
});
</script>
<script >
$(function() {
if (localStorage.getItem('size')) {
$("#size option").eq(localStorage.getItem('size')).prop('selected', true);
}
$("#size").on('change', function() {
localStorage.setItem('size', $('option:selected', this).index());
});
});
</script>
I have two tables in database of my users.
First table contains user unique ID, Name, Contact No and other personal information.
Second table contains unique id of user from first table and device information like his first machine number, second machine number and many others also.
My table no 2 structure is..
On the reports page, I am showing all the information in a table form using this
$sql = "SELECT e.* ,d.* FROM emitra_basic As e INNER JOIN emitra_device as d ON d.uid=e.uid";
$result = $conn->query($sql);
if ($result->num_rows>0) {?>
<table ><tr><td> Uid</td><td> Name</td>
<td> Micro Atm</td>.......and all column of both tables </tr>
<?php while($row = $result->fetch_array()) {
echo "<td>". $row['uid']. "</td>";
echo "<td>". wordwrap($row['name'],15,"\n",1). "</td>"; ....and all
} echo "</table>";
It works fine. But I want to show a customised report. It means I want to give check box/radio button for user of tables field. If he select field uses check box then its show only those value which check box/radio button are selected. It likes if user select three check box/radio button like Uid, name, m_atm. It shows only details of three columns from both tables and display table view accordingly these columns.
If I undestand you, to do that you need add to ON d.uid=e.uid" something like this ON d.uid=e.uid" AND Uid=$id AND name=$name And m_atm=$atm, or to add this to where (to where I thinght is not good)
For example
HTML:
<form method="get" action="/a.php">
<input type="checkbox" name="check1" value="text1"/>
<input type="checkbox" name="check2" value="text2"/>
<input id="submit" onclick="f();return false;" type="button" value="ok"/>
</form>
PHP (test.php)
if(isset($_GET['check1'])) $id=" AND Uid='$_GET[check1]'"; //if is checked first
if(isset($_GET['check2'])) $name=" AND name='$_GET[check2]'"; //if is checked second
/* . . . */
$sql = "SELECT e.* ,d.* FROM emitra_basic As e INNER JOIN emitra_device as d ON (d.uid=e.uid $id $name )";
var_dump($sql);
JS:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function f() {
var url;
var xmlhttp,
url="/text.php?"+$('form').serialize(); //change text.php
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
myfunction(xmlhttp.responseText);
}
}
xmlhttp.send(null);
function myfunction(response) { alert(url+' '+response);
//do something
}
}
</script>
That Php code is simply, but you can use loop and key value to make it look more good
For example you can use <input name=text[]> for all ckeckboxes elements and do this
foreach ($_GET['text'] as $key => $value) {
if($key==0) $key='uid'; else
if($key==1) $key='name'; else
if($key==2) $key='m_atm';
$q.="$key='$value' AND ";
}
$q=substr($q,0,strlen($q)-5);
$sql2 = "SELECT e.* ,d.* FROM emitra_basic As e INNER JOIN emitra_device as d ON (d.uid=e.uid $q )";
var_dump($sql2);
When you want to show table with dynamic column you can use if....else loop if you know exactly the number of columns query retrieve. Similar to your problem I have created 2 tables.First is employee
eId,Name,Address
And Second Is job
jobId,eId,postName,Skill
Query for retrieving data is:
$query="SELECT e.Name,e.Address,j.postName,j.Skill FROM employee AS e INNER JOIN job AS j ON e.eId=j.eId";
For each column there is checkbox
<input type="checkbox" name="chkName"/>
<input type="checkbox" name="chkAddress"/>......for all columns.
GET values will be compared with the respective columns.
To show columns dynamically using checkbox for above query:
$checkArray=array();
if(isset($_GET['chkName']))
$checkArray[0]=1;
else
$checkArray[0]=0;
if(isset($_GET['chkAddress']))
$checkArray[1]=1;
else
$checkArray[1]=0;
if(isset($_GET['chkPost']))
$checkArray[2]=1;
else
$checkArray[2]=0;
if(isset($_GET['chkSkill']))
$checkArray[3]=1;
else
$checkArray[3]=0;
$query="SELECT e.Name,e.Address,j.postName,j.Skill FROM
employee AS e INNER JOIN job AS j ON e.eId=j.eId";
$result = $con->query($sql);
if ($result->num_rows>0) {
?>
<table >
<tr>
<?php
if($checkArray[0]==1){
?>
<td> Name</td>
<?php
}
if($checkArray[1]==1){
?>
<td> Address</td>
<?php
}
if($checkArray[2]==1){
?>
<td> Post</td>
<?php
}
if($checkArray[3]==1){
?>
<td>Skioll</td>
<?php
}
?>
</tr>
<?php
while($row = $result->fetch_array()) {
echo "<tr>";
if($checkArray[0]==1)
{
echo "<td>". $row[0]. "</td>";
}
if($checkArray[1]==1)
{
echo "<td>". $row[1]. "</td>";
}
if($checkArray[2]==1)
{
echo "<td>". $row[2]. "</td>";
}
if($checkArray[3]==1)
{
echo "<td>". $row[3]. "</td>";
}
echo "</tr>";
}
Hope this will help. Create a variable eg: $condition and assign value like
$condition = "";
if($check1) {
$condition = " AND Uid=$id";
}
if($check2) {
$condition = " AND name=$name";
}
and append this $condition variable in your query. you will get dynamic values. To show the result simple method is to use AJAX on checkbox checked.
You can simply use the checkboxes in the following manner in your page
mypage.html
<form>
<input class="form-check" type="checkbox" name="checkName"><label class="form-label">Name</label>
<input class="form-check" type="checkbox" name="checkMicroATM"><label class="form-label">MicroATM</label>
<!--and so on for each field-->
</form>
<div id="dataTable">
<!--Here your table will be displayed-->
</div>
myquerypage.php
if($_POST){
$sql = "SELECT e.* ,d.* FROM emitra_basic As e INNER JOIN emitra_device as d ON d.uid=e.uid";
$result = $conn->query($sql);
$displayColCount=0; // maintain the count of columns to be displayed
$displayCol=array(); // contains the database column names to be displayed on the page
echo '<div id="dataTable">'; // very useful for replacing the content using ajax call if required
echo '<table>';
echo '<tr>';
if(isset($_POST['checkName'])){
$displayCol[$displayColCount++]='name';
echo '<th>Name</th>';
}
if(isset($_POST['checkMicroATM'])){
$displayCol[$displayColCount++]='m_atm';
echo '<th>MicroATM</th>';
}
.
.
// and so on for each column
echo '</tr>';
while($row = $result->fetch_array()) {
echo '<tr>';
$i=0;
while($i< $displayColCount){
echo "<td>". $row[displayCol[i++]]. "</td>";
}
echo '</tr>';
}
echo '</table>';
echo '</div>';
}
you can call the above page using ajax as
$('.form-check').change(function (e) {
var form=this.form;
var formData = $('form').serialize();
$.ajax({
type: 'POST',
url: 'myquerypage.php',
data: formData,
cache: false,
success: function (html)
{
$("#dataTable").html(html);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
I hope this helps you.
I think I get your point. The following code can be used in the same file but for obvious reasons it's better to separate them.
what I have done, I have got a list of the checkboxes based on the fields that you've got in the database.
Post these fields and run query to get back data for only these fields that are posted.
After I generate the html code/table regarding the posted fields and the data
<?php
$result = [];
$fields = [];
$dbFields = [];
$sql = "";
$fieldsNameMapping = [
'e.uid' => 'Uid',
'd.block' => 'Blocked',
'd.m_atm' => 'Atm',
'd.uid_name' => 'Name'
];
if (isset($_POST)) {
// build query based on the posted fields
if (isset($_POST['fields']) && !empty($_POST['fields'])) {
$sql = "SELECT ";
foreach ($_POST['fields'] as $fieldValue) {
$sql .= $fieldValue . ", ";
// get field names
$fields[] = $fieldsNameMapping[$fieldValue];
// mapping db field names, remove first two characters
$dbFields[] = substr($fieldValue, 2);
}
// remove last comma
$sql = substr($sql, 0, -2);
$sql .= " FROM emitra_basic As e INNER JOIN emitra_device as d ON d.uid=e.uid";
// get result
$result = $conn->query($sql);
}
}
?>
<form method="post">
<input type="checkbox" name="fields[]" value="e.uid" /> Uid<br />
<input type="checkbox" name="fields[]" value="d.block" /> Blocked<br />
<input type="checkbox" name="fields[]" value="d.m_atm" /> Atm<br />
<input type="checkbox" name="fields[]" value="d.uid_name" /> Name<br />
<input type="submit" value="Show Result" />
</form>
<?php if (!empty($fields)) { ?>
<table>
<thead>
<tr>
<?php foreach($fields as $fieldName) { ?>
<th><?php echo $fieldName ?></th>
<?php } ?>
</tr>
</thead>
<?php if (!empty($result) && $result->num_rows > 0) { ?>
<tbody>
<?php while($row = $result->fetch_array()) { ?>
<tr>
<?php foreach($dbFields as $fieldDbName) { ?>
<td><?php echo $row[$fieldDbName] ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
<?php } ?>
I am building a simple cart with a while loop. I am showing product ID and Quantity. The user can change the quantity, and I want a single "UPDATE" button at the bottom of the cart that will update foreach of the items in the cart. It needs to pull the product ID and the new quantity entered by the user. How can I do this.
I can do it for a single cart item, but I can't figure out how to pass the multiple items as an array and treat the data once the UPDATE button is pressed.
My code:
<?
$data = mysql_query(.............);
if (isset($_POST['update']))
{
foreach ($array as $id => $qty)
{
$product_id = ...??..;
$new_qty = = ...??..;
$sql = "INSERT INTO.....".$product_id." and.... ".$new_qty."... ";
$insert = mysql_query($sql);
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" >
<?
while ($data2 = mysql_fetch_array( $data)) { ?>
QTY: <input type="number" name="qty" class="form-field" value="<?=$data2['qty']?>" >
<input name="products_id" value="<?=$data2['product_id']?>" type="hidden">
<? } ?>
<input class="submit-button" type="submit" name="update" value="UPDATE QTY" />
</form>
You have to assign name attribute with square brackets to indicate array for your form elements.
Look here for explanation: here
Look here for simple concept example and alter it to suit your code: here
<?
while ($data2 = mysql_fetch_array( $data)) { ?>
QTY: <input type="number" name="qty[]" class="form-field" value="<?=$data2['qty']?>" >
<input name="products_id[]" value="<?=$data2['product_id']?>" type="hidden">
<? } ?>
then
if (isset($_POST['update']))
{
$array = $_POST['products_id'];
$quantities = $_POST['qty'];
foreach ($array as $key => $id)
{
$product_id = $id;
$new_qty = = $quantities[$key];
$sql = "INSERT INTO.....".$product_id." and.... ".$new_qty."... ";
$insert = mysql_query($sql);
}
}
I am trying to get records from my DB using 2 seperate tables. The query is
public function get_pages_by_campign_id($campaignID) {
$campaignID = $this->real_escape_string($campaignID);
return $this->query("SELECT pid, campaignid FROM pages,campaigns WHERE 'pages.campaignid = campaigns.id'");
}
And this is called into play by :
$counter = 1;
$userID = PageDB::getInstance()->get_user_id_by_name($_SESSION['user']);
$result = PageDB::getInstance()->get_pages_by_campaign_id($campaignID);
$i=0;
while ($row = mysqli_fetch_array($result)):
$style = "";
if($i%2==0)
{
$style = ' style="background-color: #EFEFEF"';
}
echo "<tr".$style.">";
echo "<td style='width:140px;'> Page " . $counter . "</td>";
echo "<td> </td>";
echo "<td></td>";
//The loop is left open
?>
<td>
<form style="display:none;"></form>
<form name="editPage" action="editPage.php" method="GET">
<input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']; ?>"/>
<input type="submit" name="editPage" value="Edit"/>
</form>
</td>
<td>
<form name="deletePage" action="deletePage.php" method="POST">
<input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']?>"/>
<input type="submit" name="deletePage" value="Delete" onclick = "javascript: return confirm('Delete Page <?php echo $counter ?> ?');"/>
</form>
</td>
<?php
$pageID = $row['pid'];
$counter++;
$i++;
echo "</tr>\n";
endwhile;
mysqli_free_result($result);
?>
This should list all "page" Records, but it is returning nothing.
I have set up the "id" column in table "campaigns" to correspond with "campaignid" in table "pages" using foreign keys.
I manged to fix it with this :
public function get_pages_by_campaign_id($campaignID) {
$campaignID = $this->real_escape_string($campaignID);
return $this->query("SELECT pid, campaignid FROM pages WHERE campaignid = 'campaigns.id'");
}
"SELECT pid, campaignid FROM pages,campaigns WHERE pages.campaignid = campaigns.id"
?