Sorry if I do something wrong it is my first time using stackflow and just beginning php and mysql.
The problem I am getting is that the table updates AFTER it echos. I would like to display the updated table.
while($row = mysql_fetch_array($result)) {
$bids = $row['bids'];
$bids +=1;
mysql_query("UPDATE items SET bids = " .$bids.", cost=" . $price. " WHERE items.name =". $item );
echo "<tr>
<td>".$row['name']."</td>
<td align=\"right\">".$row['cost']."</td>
<td align=\"right\">".$row['bids']."</td>
<td align=\"right\">".$row['seller_name']."</td>
</tr>";
}//end while
You need to add one more line in between your update statement and display statement.
$new_record = mysql_fetch_array(mysql_query("SELECT * FROM items WHERE items.name =". $item"));
Then use your echo statement as :
echo "<tr><td>".$new_record ['name']."</td>
<td align=\"right\">".$new_record['cost']."</td>
<td align=\"right\">".$new_record['bids']."</td>
<td align=\"right\">".$new_record['seller_name']."</td>
</tr>";
You should select rows from the table again, if You want to show them after update. In the code snippet above You actually echos old entries from the table.
Just wrap it in an if block
if(mysql_query("UPDATE items SET bids = " .$bids.", cost=" . $price. " WHERE items.name =". $item )){
echo "<td>".$new_record ['name']."</td>
<td align=\"right\">".$new_record['cost']."</td>
<td align=\"right\">".$new_record['bids']."</td>
<td align=\"right\">".$new_record['seller_name']."</td>
</tr>";
}
Related
I am trying to make a while loop which retrieves all the relevant data from Mysql database but it doesn't work for more than one variable, the problem I think is with the while loop because I have echoed the sql statement and it retrieved the values of the variables right, the code is:
$wherein = implode(',', $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID = '$wherein'";
$result = mysqli_query($conn, $sql);
echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";
while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td> <select>
<option value= '1'>1</option>
<option value= '2'>2</option>
<option value= '3'>3</option>
</select>
</td>";
echo "</tr>";
}
echo "</table>";
I have tried many things with the code but the problem still exists, I would really appreciate your help, Thank you.
The </table> statement is inside the while loop. Hence once the table is closed after the first loop the rest of the data might not be showing on the browser (it will still be in your html source code). Try it with moving the </table> statement to the last line.
assuming that by imploding the variable you will have multiple IDs then you should use the IN statement in the sql.
$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
The rendered output can be simplified slightly and the table should be closed after the loop! Not knowing the contents of $wherein makes it tricky to answer but I would think the IN statement seems to fit the nature of a comma separated value being used in the query better than a direct equals =
$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
$result= mysqli_query( $conn, $sql );
echo "
<table style='width:100%' border='1' >
<tr>
<th> Product Name</th>
<th>Product Price </th>
<th>Quantity </th>
</tr>";
while ( $row = mysqli_fetch_object( $result ) ){
echo "
<tr>
<td>{$row->Name}</td>
<td>${$row->Price}</td>
<td>
<select>
<option value= '1'>1
<option value= '2'>2
<option value= '3'>3
</select>
</td>
</tr>";
}
echo "
</table>";
/* Example of using an array of IDs and imploding to generate where conditions for IN clause */
$cart=array('BGL1','BJL');
/* session - cart */
$wherein=implode( "','", $cart );
/* correctly add quotes around each string */
$sql="select ID, Name, Price from lamps WHERE ID IN ('$wherein');";
/* use the new $wherein string within quotes */
echo $sql;
>> select ID, Name, Price from lamps WHERE ID IN ('BGL1','BJL');
Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.
<?php require_once('../includes/header.php');?>
<?php require_once('../includes/connection.php');?>
<?php include('../includes/get_username.php');?>
<?php
include('sanitise.php');
$month = sanitise($_GET['month']);
?>
<table id="contentbox">
<tr>
<td>
<?php
$color="1";
//view record
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
//RESPO/Office
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id'];
if($color==1){
echo "<tr bgcolor='#ffffff'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
I Have all it work. Only 1 PROBLEM.. I CANNOT DISPLAY the selected RESPO/OFFICE. As I select the RESPO/OFFICE it will sort out the selected RESPO/OFFICE... What should I resvise my code or add code.. Please Help!
A few suggestions first:
mysql is depreciated and you should move to PDO prepared statements
or mysqli. You can consult the php manual for this.
comment your code, you are not able to follow along unless you read
from top to bottom.
Now onto your code:
<?php
// Start document with includes needed, header, connection, functions.
require_once('../includes/header.php');
require_once('../includes/connection.php');
include('../includes/get_username.php');
include('sanitise.php');
// Start with the month variable posted from $_GET, this will begin the initial display.
$month = sanitise($_GET['month']);
?>
<!-- Begin Table Display and Layout -->
<table id="contentbox">
<tr>
<td>
<?php // Open PHP Tag to get variables to display in the table
$color="1";
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
// Format the table rows for display
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
// Fill the SELECT option with all the RESPO/Office data
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
// Display the select with the newly populated content
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
// While the result contains data we will display respo as a select option.
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
// Finish the table header
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
// While we have data from the original query we will display it
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id']; // grab the dv_id
if($color==1){ // Setup color scheme
echo "<tr bgcolor='#ffffff'> // Color for the row
// Let's begin filling the table with data
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
After reviewing the above code it is clear that you haven't setup a query to filter the data that you are displaying based on the selected option.
Your first query:
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
You could append a new variable for the selected option, expand your WHERE statement. WHERE monthname(date_added) = '$month' && respo = '$respo' and move this query after the select option so it has access to the value chosen by the select option. Lastly, unless you are going to use jQuery to deal with the select box changing values, you would need a submit button to show the state change for the select option.
Pseudo Code:
Check for Submit
Change value of the department in the query WHERE clause
Display the requested data
This should get you in the direction you need to go in order to achieve the desired result.
EDIT:
In the 3rd answer you posted:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
You said it is not filtering by the month being passed in, you are not adding it into your query, specifically the WHERE clause. So it should be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND month(month_added)='$month' AND year(date_added)='$year' GROUP BY date_added ";
FINAL EDIT:
Your first issue stems from not sending the $_GET['date_added'] value to the view page, so you will never display anything. Properly format the select box and add the data for the RESP and Date data from the database:
echo "<option value='". $row['respo'] . "+". $row['date_added'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
Now that we are passing the respo and the date string we can work with the data on the next page:
/* UNCOMMENT NEXT LINE FOR ERROR DETECTION */
//error_reporting( E_ALL );
/* GET THE ENTIRE STRING PASSED FROM THE SELECT OPTION */
$respo = $_GET['respo'];
/* EXPLODE THE DATA BY + SYMBOL TO GET THE DATA */
$data = explode("+", $respo);
/* FORMAT THE DATETIME FROM THE STRING TO A FRIENDLY FORMAT */
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";
This is how I choose to work with the data and get what I needed, so now the query would be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month' ";
This will only display the records for the RESPO and the date by year and month passed into the page via $_GET.
<?php require_once('../includes/header.php');?>
<?php
$respo = $_GET['respo'];
$month = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('m'));
$year = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('Y'));
//database call here
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
$result = mysql_query($viewrecord, $db) or die (mysql_error());
$num_result = mysql_num_rows($result);
{
echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
etc...... (header here)
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
echo "<tr ><td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
etc...
}
mysql_close($db);
?>
This is the code i come up. This is just an alternative solution. But the problem with this solution is that. I won't display the record in MONTH as call. THe respo is working as I select the RESPO it will display the all RESPO I ask, but the problem is that it include all the previous months with the RESPO I select. When I just select the specific month only..
//RESPO/Office
**echo '<form action="view.php" method="post">';**
$byrespo = mysql_query("SELECT DISTINCT count(*), respo FROM tbl_dv WHERE monthname(date_added)='$month' GROUP BY respo");
echo "<select name='respo' onchange='this.form.submit()'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($byrespo)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
}
echo "</select></form>";
I just rushing to came up with solution.. So I add a and I create another view.php
I am new to PHP, and was wondering if you could help with my select sum query?
I am wanting to add the total amount from my expenses table in my database based on user_id?
Here is what I have so far
<?php
//sets up thisPage
$pageSize=10;
if (isset($_POST["thisPage"]))
$thisPage = $_POST["thisPage"];
else
$thisPage=1;
//selects all distinct expenses that have been uploaded
$dbQuery="SELECT SUM(amount) AS TotalExpenditure FROM expenses WHERE user_id = '$userID'; ";
echo $dbQuery;
$dbResult=mysqli_query($db_connection, $dbQuery) or die (mysqli_error($db_connection));
echo "<table> <thead>";
//echo '<tr> <th>Project ID</th><th>Project Name</th></tr> </thead>';
while ($dbRow=mysqli_fetch_array($dbResult)){
// display row with expense
echo '<tr> <td>'. $dbRow['amount'] .'</td>';
}
echo "</table>";
echo "</form>";
?>
You have
$dbRow['amount']
but you use
SUM(amount) AS TotalExpenditure
// ^ this is the name (alias) of your column now
TotalExpenditure is the name of the amount, so the last part should be
echo '<tr> <td>'. $dbRow['TotalExpenditure'] .'</td> </tr>';
You have to use TotalExpenditure as key of your array, so
$dbRow['TotalExpenditure'] instead of $dbRow['TotalExpenditure']?
You're giving an alias to your count() sql function so you have to use it
I have use MySQL Table get values on table ,that values used another table .i got array values
first table query:
$ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
$i=0;
while($rows = mysql_fetch_array($customerorder ))
{
$orderproduct= $rows['Order_product_ID'];
$orderids= $rows['Order_ID'];
$productId[$i] = $rows['Product_ID'];
$sizeId[$i] = $rows['Size_ID'];
$colorId[$i] = $rows['Color_ID'];
$quantiy = $rows['Order_Quantity'][$i];
$price = $rows['Unit_Price'][$i];
$subTotal = $rows['Sub_Total'];
$customerccode = $rows['Record_Status'];
$customerphone = $rows['Created_Time'];
$i++;
}
table column productid array values get used another table:
given:
for($i=0;$i<count($productId);$i++)
{
$product = "SELECT * FROM `product` WHERE `Product_ID`='".$productId[$i]."'";
$products = mysql_query($product);
while($rows = mysql_fetch_array($products))
{
$productnames = $rows['Product_Name'];
}
}
color id used :
for($i=0;$i<count($colorId);$i++)
{
$color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
$cname = mysql_query($color);
while($rows = mysql_fetch_array($cname))
{
$colorname = $rows['Color_Name'];
}
}
same way used the values table
my questions that values displayed table continuely
table structure
$message .= "<table width='100%' border='1' cellspacing='0' cellpadding='1'>
<thead>
<tr style='background: #eee;'>
<th><strong>Quantity</strong> </th>
<th><strong>ProductName</strong> </th>
<th><strong>Size</strong> </th>
<th><strong>Style</strong> </th>
<th><strong>Price</strong> </th>
<th><strong>Total</strong> </th>
</tr>
";
for($i=0;$i<count($productId);$i++)
{
$message .=" <tr style='color: #c40000;'>
<th>" .$quantiy. "</th>
<th>" .$productnames. "</th>
<th>" .$sizename. "</th>
<th>" .$colorname. "</th>
<th>" . $price. "</th>
<th>" . $subTotal. "</th>
</tr>";
}
this format is correct more values not displayed only first values display multiple time
This is a hard one to answer because there there is a lot of issues going on....
First off....unless this is a private/internal application, you cant place variables directly into your query...look into a proper database class, I can recommend : http://www.imavex.com/php-pdo-wrapper-class/
Secondly, your are mixing your php code and html too much...I'm not going to go into this too much but in general you want to keep this stuff seperate...research MVC programming patterns.
Now to answer your question...The reason you are seeing the same data repeat is because you you are using only one variable that is getting overwritten each time...ie. $quantiy (which is spelled wrong fyi)...so to hack your approach, assign it to an array and then use an $i counter on it like you are doing with $productID
A better approach would be to do this stuff in the loop and avoid assign the vars in the first place...I'm assuming you have assign stuff to the message var...if you can skip that and echo it out directly that would probably be better.
$ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
$i=0;
while($rows = mysql_fetch_array($customerorder ))
$product = "SELECT * FROM `product` WHERE `Product_ID`='".$row[$i]."'";
$products = mysql_query($product);
$color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
$cname = mysql_query($color);
Now you would create your table from the $rows data directly and when you goto the color/size stuff you could look through in there.
}
Now I'm not 100% sure it will work for you but you could probably do something similar to above with a single query using JOINS
SELECT * FROM `order_item` LEFT JOIN product ON order_item.productID = product.productID LEFT JOIN color ON order_item.colorID = color.colorID WHERE order_item.`Order_ID`='".$orderid ."'
Please pardon the syntax/naming conventions...the above command will not run properly on your system, it is only meant to give you an idea on what to do.