Order Results By Row Valuation Calculation Answer - php

I have the following query providing me with a table full of the correct descriptions and figures:
$result = mysqli_query($con,"SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID");
$total=0;
while($row = mysqli_fetch_array($result))
{
$quantity = $row['QUANTITY'];
$description = $row['NAME'];
$unitprice = $row['PRICE'];
$lineprice = $row['PRICE']*$row['QUANTITY'];
$total=$total+$lineprice;
$tbl_header = '<table style="width: 650px;" cellspacing="0" cellpadding="5">';
$tbl_footer = '</table>';
$tbl = '';
$tbl .= '
<tr>
<td style="width: 50px; text-align: left; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($quantity,0).'</p></td>
<td style="width: 350px; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.$description.'</p></td>
<td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($unitprice,2).'</p></td>
<td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;" align="right" ><p style="color:#808080;">'.number_format($lineprice,2).'</p></td>
</tr>
';
As you will be able to see the $lineprice is calculated by:
$lineprice = $row['PRICE']*$row['QUANTITY'];
Now I would like to order the table results based on the value in this field. I've tried:
SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID ORDER BY '$lineprice' ASC
But that doesn't work. How could I order the results in the table by this column?
Thank you in advance,
Andy

ORDER BY PRICE * QUANTITY
Ordering by a fixed number, like you're trying won't work, because the DB will have NO idea what field(s) to compare that fixed value against. But if you do the multiplication within the deb, as in the above snippet, you'll get your expected sort order.

SELECT quantity * price as total FROM b_sale_basket ORDER BY total;

Related

mysql table values merged into one value instead of multiple

i've ran into an issue with my code which has left me stumped. in the code below, i have created a table for mysql data to be fetched into. however when running my code all of the data (which includes titles, descriptions and prices), seem to be merged into one continuous row which fills the entire page. i'm not sure if this has to do with the size of the data i'm trying to fit into a specific row, or a fault in my code. so any help would be appreciated.
styling used:
table {
border: none;
border-collapse: collapse;
width: 100%;
color: #ffffff;
font-size: 15px;
}
th {
background-color: #a6a6a6;
color: white;
}
table td {
font-size: 5px;
border-left: 1px solid #000;
border-right: 1px solid #000;
}
table td:first-child {
border-left: none;
}
table td:last-child {
border-right: none;
}
</style>
table and php:
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table>
<?php //Beginig of php script for database events
include "Database_connection.php"; //Will make the database connection
$sql = "
SELECT e.eventTitle
, c.catDesc
, e.eventDescription
, v.venueName
, e.eventStartDate
, e.eventEndDate
, e.eventPrice
FROM NEE_venue v
JOIN NEE_events e
ON e.venueID = v.venueID
JOIN NEE_category c
ON c.catID = e.catID
ORDER
BY eventTitle ASC
";
$queryResult = $dbConn->query($sql);
if($queryResult === false) { //will detect if the connection failed, and give an error message
echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
exit;
}
else { //if connection is succsessful, code will retrive the events
while($row = $queryResult->fetch_assoc()){
echo "<tr><td>". $row["eventTitle"] ."</td><td>". $row["catDesc"] ."</td><td>". $row["eventDescription"] ."</td><td>". $row["venueName"] ."</td><td>". $row["eventStartDate"] ."</td><td>". $row["eventEndDate"] ."</td><td>". $row["eventPrice"]
."</td></tr>";
}
echo "</table>";
}
$queryResult->close();
$dbConn->close();
?>
screenshot of the issue:
mysql issue
Get rid of the end table tag in the top section.
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table> <--- That one.
Looks like from both the code and the image that the data gets printed outside of the table, because the end table tag gets added too early.

Pass certain table cell values to SQL update query

I feel stuck with a bit unusual situation for me up until now ... I constructed a HTML table that shows quantities from different locations by reading and rows from SQL DB into a $locations variable that is essentially an array - here's the code:
while ($location = mssql_fetch_array($locations)){
echo"
<tr>
<td style='text-align:center; padding-left: 2px; padding-right: 2px'>$location[1]</td>
<td style='text-align:center; padding-left: 2px; padding-right: 2px'>$location[2]</td>
<td width='50' style='text-align:center; padding-left: 2px; padding-right: 2px'><input type='number' style='border:none;text-align:center;' class='input' name='qty_to_add[]' type='text' value='' min='1' max='999'></td>
</tr>";
};
But the real problem is how do I read only cells in the third column to the right, that have values imputed and pass only those to an update query that will further update only those rows that contain locations POL_12 (15 pcs + 5 more) and POL_54 (30 pcs + 7 more)?
I need to do this in a procedural way, since I am not yet familiar with OOP and PDO (but I am striving to learn it soon) :/
Please advise, all suggestions are welcome! Thank Y'all!
In this scenario, following Barmar's advice (because it seemed most familiar to me in this moment) - using hidden fields in a form combined with for loop worked like a charm!
<form method="post" enctype="multipart/form-data">
<table style="width:100%" border="2">
<tr>
<th style="text-align:center; padding-left: 2px; padding-right: 2px">Location</th>
<th style="text-align:center; padding-left: 2px; padding-right: 2px">Qty</th>
<th style="text-align:center; padding-left: 2px; padding-right: 2px">Qty to add</th>
</tr>
<?php
$locations = mssql_query("my query for reading item name, location and quantity per location ");
while ($location = mssql_fetch_array($locations))
{
echo"
<tr>
<input style='border:none;text-align:center;' class='input' name='location_id[]' type='hidden' value='$location[0]'>
<input style='border:none;text-align:center;' class='input' name='item_id[]' type='hidden' value='$location[3]'>
<td style='text-align:center; padding-left: 2px; padding-right: 2px'>$location[1]</td>
<td width='2' style='text-align:center; padding-left: 2px; padding-right: 2px'><input type='number' style='border:none;text-align:center;' class='input' name='current_qty[]' type='text' value='";if (!isset($location[2])) {echo "0";} else {echo $location[2];}; echo "'></td>
<td width='50' style='text-align:center; padding-left: 2px; padding-right: 2px'><input type='number' style='border:none;text-align:center;' class='input' name='qty_to_add[]' type='text' value='0' min='0' max='999'></td>
</tr>";
};
?>
</table></br>
<input id="post" class="search" name="submit" type="submit" value="POST" style="position: relative"></br>
</form>
<?php
if (isset($_POST['submit']))
{
$location_id = $_POST['location_id'];
$ite_id = $_POST['item_id'];
$curr_qty = $_POST['current_qty'];
$added_qty = $_POST['qty_to_add'];
$rowCount = count($_POST['location_id']);
for ($i = 0; $i < $rowCount; $i++)
{
if ($added_qty > 0)
{
$query = mssql_query("update [my-table-name] set qty_per_location = cast('$curr_qty[$i]' as decimal (5,2)) + cast('$added_qty[$i]' as decimal (5,2)) where item_id = $item_id[$i] and item_location_id = $location_id[$i]");
echo "<br/><br/><span>Data inserted to DB successfully!</span>";
}
}
}
?>
I don't want to sound disrespectful to others, that suggested possible solutions - I chose this solution merely because it was understandable for my current novice level of knowledge! Instead a big shoutout to all that sacrificed a lil' bit their time to contribute. Thank y'all!

MYSQL fetching rows to different tables

I'm here with following problem. I have table with multiple records. Each row represents news of some kind, so I need to distinguish one from the another. The way to do that, is to make a table for each of these news(or a wrapper of some kind), however I encoutred a problem with fetching each result to each table(wrapper).
I can't figure out how to tell SQL to put the record separatley. Below is chunk of my code, as well as the design for the table.
.news{
width:400px;
height:auto;
border: 1px solid red;
float:left;
}
.news tr:first-child{
font-weight: bold;
height:40px;
text-align: center;
}
.news tr:last-child{
background-color: whitesmoke;
height: 200px;
padding-bottom: auto;
text-align: center;
}
.details td{
width:200px;
height: 40px;
text-align: center;
}
echo "<table class='news'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>
<td colspan='2'>
$row[0]
</td>
</tr>
<tr class='details'>
<td>
$row[1]
</td>
<td>
$row[2]
</td>
</tr>
<tr>
<td colspan='2'>
$row[3]
</td>
</tr>";
echo "</table>";
}
$query = " SELECT headline, date, concat (firstName,' ',lastName), body FROM "
. "school_info natural join teachers ORDER BY id_school_inf DESC LIMIT 2;";
$result = mysqli_query($conn, $query);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This is my query. There is a limit, so i can always fetch latest (let's say last 5) news.
Any help would be much appreciated
You need to create a separate table for every type of record you have? Simple enough, if you order your records by the type:
SELECT * FROM whatever ORDER BY type_of_record
then
$previous = null;
while(... fetch from db ...) {
if ($row['type'] != $previous) {
... got new record type, start new table
}
... display row
$previous = $row['type']; // store current type for comparison later
}

List files in folder sub-directory

I have a script that when clients create a pdf file, it is placed in a folder named with there own code. This is in a main folder called 'destcerts' and the folder structure ends up looking like the illustration below. What I need to do as admin is have access to these files and the only way I can do that at the moment is to hard code the sub-directory in the code and that is not practical for obvious reasons.
What I thought I could do is, have a select dropdown with a list of clients which when selected could trigger perhaps a change event which would say loop for the chosen client and display the files. Unfortunately, my php or jquery is not good enough to achieve this and I would be grateful if someone could show me the way to go forward with this. Many thanks
php code
<div id="viewCerts" style="display:none;">
<?php
$sub = 'destcerts/';
// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
// SKIP THE "DOT" FILES
if ($filedata->isDot()) continue;
// ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
if ($filedata->getExtension() != 'pdf') continue;
// CREATE LINKS TO THESE FILES
$nom = $filedata->getFilename();
$lnk
= '<img src="destcerts/PDF_icon_100.png" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
. $sub
. '/'
. $nom
. '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
. $nom
. '</a>'
;
// COLLECT THE LINKS HERE
$sss[] = $lnk;
}
// ACCUMULATE THE TABLE ROWS HERE
$trs = NULL;
// COLLECT GROUPS OF FOUR
If(!empty($sss)){
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
}
else{
$msg = "There are no files to display";
$tr = <<<EOD
<tr>
<td style="text-align:center; padding: 10px !important; font-weight: normal;">$msg</td>
</tr>
EOD;
$trs .= $tr;
}
// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table id="pdfDownload" width="97%" align="center" border="1" cellspacing="10" cellpadding="0" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
<th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
<tr>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
</div>
directory structure
.
..
destcerts
client1
client2
client3
etc

want to insert in attendence first time not update it

hi this is my code which updates an attendance system, but I dont know how to insert attendance for first time. Where should I use insert query? it also shows error and not updating the table. Somebody please guide me
<?php
$connection=mysql_connect("localhost","root","") ordie(mysql_error()
$db=mysql_select_db("Project") or die(mysql_error());
$sql = "SELECT Fac_name FROM Faculty ORDER BY Fac_name ASC ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<form name="Attendence" method="post" action="A.php">
<table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px">
<tbody>
<tr>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Faculty Name</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Abesent</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Present</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Total present</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)) {
?>
<tr>
<td class="table1">
<? $id[] = $rows['Fac_name']; ?><? echo $rows['Fac_name'];?>
</td>
<td class="table1">
<input name="date[<? echo $rows['Fac_name']; ?>]" type="text" >
</td>
<td id="present">
<input type="radio" name="Present[<? echo $rows['Fac_name']; ?>]" checked="checked" >Present
</td>
<td id="absent">
<input type="radio" name="Absent[<? echo $rows['Fac_name']; ?>]" value="ABSENT">Absent
</td>
<td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"></td>
</tr>
<?php }?>
<tr>
<td colspan="7" style="vertical-align:middle; text-align: center;">
<br><br>
<input id="Submit" type="submit" name="Submit" value="Insert" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;">
</td>
</tr>
</tbody>
</table>
</form>
<?php
if(isset($_POST['Submit'])) {
foreach($_POST['Present'] as $id => $value) {
$date=$_POST['date'];
$present=$_POST['Present'];
$absent=$_POST['Absent'];
$sql = "INSERT INTO Attendence(Fac_name, date, Present, Absent) VALUES ('".$id."', '$date[$value]', '$present[$value]', '$absent[$value]', '".$value."') ";
$result = mysql_query($sql);
}
}
if($result) {
//header("location:A.php");
} else {
//print_r ($_POST);
echo "Your entry is not completed at this time.............";
}
if(isset($_POST['submitattend'])) {
set_time_limit(0);
$class1 = $_SESSION['bra'];
$q3 = mysql_query("Select Id from `Faculty` order by `Id` ASC"); // get all roll numbers
$count = mysql_num_rows($q3);
$j = 1;
while($q4 = mysql_fetch_array($q3)) {
if(isset($_POST['chk'.$j])) {
$v2 = $q7['finalattend']+1; //total attendance of student
$v3 = $q7['totalattend']+1; //total attendance taken by teacher
mysql_query("UPDATE `Attendence` SET `finalattend`='".$v2."', `totalattend`='".$v3."' where `attenduser`='".$v1."'") or die(mysql_error());
} else {
$v2=$q7['totalattend']+1;
mysql_query("UPDATE `Attendence` SET `totalattend`='".$v2."' where `attenduser`='".$v1."'") or die(mysql_error());
}
$j=$j+1;
}
header("Location: 12.html"); //logout after taking attendance..
}
?>
$date_value = $date[$value];
$present_value = $present[$value];
$absent_value = $absent[$value];
$check_result = mysql_query("selct count(*) from Attendence where Fac_name = '$id' AND date = '$date_value' AND Present = '$present_value' AND Absent = '$absent_value' ");
if($check_result == 0)
{
// insert query
}
else
{
// update query
}
I think your INSERT query line has Syntax error Please correct it . You have only three row names - Fac_name, date, Present, Absent but are inserting 5 values. Besides that quotes are not used correctly

Categories