Ive created a page that lists existing hardware transactions ('else' clause below) and filters the transactions based on criteria associated with a specific row ('If' clause). The Else clause displays fine, but when I enter Criteria (e.g. Mouse)and select a Filter (e.g. device) that I know match in the database, no results are shown, when there should be.
The frustrating part is that when I change line 12 to:
WHERE device = '$criteria' //This will work
For some reason the original code wont work even though the variable $filter equals the string "device".
Please someone help, this seems like a bug to me :(
PHP Code:
function display_transactions() {
//Display Transactions table
GLOBAL $transactions;
GLOBAL $db;
try {
if (isset($_POST['submit_criteria'])) {
GLOBAL $transactions_sql;
$filter = $_POST['filter-by'];
$criteria = $_POST['text_criteria'];
$transactions_sql = "SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
WHERE '$filter' = '$criteria'
";
} else {
$transactions_sql = 'SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
ORDER BY datetime DESC';
echo "UNFILTERED!";
}
$transactions = $db->query($transactions_sql);
} catch (Exception $e) {
echo $e->getMessage();
}
}
HTML page:
<?php display_transactions() ?>
<?php while($row = $transactions->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['device']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['transaction_type']; ?></td>
<td><?php echo $row['ticket_id']; ?></td>
<td><?php echo $row['region']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['datetime']; ?></td>
</tr>
<?php } ?>
Related
I have this code in php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Sec_No"]; ?></td>
<td><?php echo $row["Sec_Sub"]; ?></td>
<td><?php echo $row["Sec_Lec"]; ?></td>
<td><a href="Theo_Entery_Sheet.php?Sec_No=<?php echo $row["Sec_No"]; ?>"> ُEnter Marks/>a></td>
</tr>
<?php
I want to pass the field [Sec_Sub] beside the field [Sec_No] that is exist in href statment as query string to another page (Theo_Entery_Sheet.php)
Theo_Entery_Sheet.php
i want the part of SQL "WHERE" take two conditions Sec_No AND Sec_Sub that is comming from above page
...
...
...
$sql = "SELECT theo_stu_sections.Sec_No, theo_stu_sections.St_No, students.Name, theo_stu_sections.Mid,
. "FROM students INNER JOIN theo_stu_sections ON students.St_ID = theo_stu_sections.St_No\n"
. "WHERE Sec_No='".$_GET['Sec_No']."'\n"
. "ORDER BY students.Name;";
thank You
I want the page to receive two parameters from the page which send query string
I was wondering if I could get some help echoing the sum of each column based on a query result. Screenshot of table output
<?php
if ($stationid=="") {
$query = "select * from " . $tablename . " where date_created >= '" . $startdate . "' and date_created <= '" . $enddate . "' and status='1' order by id desc"; ?>
$result = do_query($query);
$total = mysql_num_rows($result);
if ($total>0) {
while($row = do_fetch_result($result)){
?>
<?php
$sum_static = $row['SUM(element_10)'];
$sum_gif = $row['SUM(element_11)'];
$sum_ibv = $row['SUM(element_12)'];
$sum_html5 = $row['SUM(element_13)'];
$sum_landing = $row['SUM(element_14)'];
$sum_revs = $row['SUM(element_15)'];
?>
<tr>
<td><?php echo $sum_static; ?></td>
<td><?php echo $sum_ibv; ?></td>
<td><?php echo $sum_gif; ?></td>
<td><?php echo $sum_html5; ?></td>
<td><?php echo $sum_landing; ?></td>
<td colspan=1><?php echo $sum_revs; ?></td>
</tr>
Simply specify columns name with aggregate function.
SELECT SUM(column1),SUM(column2)
FROM tablename
<?php
$overallstatic="0";
$overallgif="0";
$overallibv="0";
$overallhtml5="0";
$overalllanding="0";
$overallrev="0";
<?
<?php
$overallstatic=$overallstatic+$row['element_10'];
$overallgif=$overallgif+$row['element_11'];
$overallibv=$overallibv+$row['element_12'];
$overallhtml5=$overallhtml5+$row['element_13'];
$overalllanding=$overalllanding+$row['element_14'];
$overallrev=$overallrev+$row['element_15'];
?>
<?php
<td><?php echo $overallstatic; ?></td>
<td><?php echo $overallibv; ?></td>
<td><?php echo $overallgif; ?></td>
<td><?php echo $overallhtml5; ?></td>
<td><?php echo $overalllanding; ?></td>
<td colspan=1><?php echo $overallrev; ?></td>
?>
This worked.
If we need to calculate sum from individual rows (if all we want to return is totals, then this is not the most efficient way to get totals), we could initialize the $sum_foo variables to zero...
$sum_static = 0;
$sum_foo = 0;
And then for each row fetched, add the column value to the running total.
$sum_static += $row['static'];
$sum_foo c += $row['foo'];
After the last row is fetched, the $sum_foo variables will have the total for that column for all rows.
In terms of performance, this is a very inefficient way to get just totals. We can have the database return the totals. We ditch the * in the SELECT list, and specify aggregate functions in the SELECT list:
SELECT SUM(static) AS sum_static
, SUM(foo) AS sum_foo
FROM ...
Execute the query, and fetch the one row that is returned, and the row will have the precomputed totals...
$row['sum_static']
$row['sum_foo']
I am fetching data in php from mysql table. Let say I retrieved 15 rows containing 15 unique ids. Now I want to get the the very first fetched id from the results.
This is what I'm trying..but its giving me the last id
<?php
$results = $db->query("SELECT * FROM orders where Sales_Rep='$sales_rep'");
while($row = $results->fetch_assoc()){
?>
<tr>
<td><?php echo $row["Order_ID"] ?></td>
<td><?php echo $row["Company_Name"]?></td>
</tr>
<?php
$last_order_date= $row["Order_ID"];
echo $last_order_date;
} //end of while loop
?>
you could simply add index like this
$i = 0;
while loop
$i++;
if($i === 1)
{get ID}
//rest of your while loop
endwhile;
you can modify your sql to
SELECT * FROM orders where Sales_Rep='$sales_rep' Order By Order_ID DESC
or you use this code
<?php
$results = $db->query("SELECT * FROM orders where Sales_Rep='$sales_rep'");
$i =0;
$first_id = 0;
while($row = $results->fetch_assoc()){
if ($i===0){
$first_id = $row["Order_ID"];}
$i++;
?>
<tr>
<td><?php echo $row["Order_ID"] ?></td>
<td><?php echo $row["Company_Name"]?></td>
</tr>
<?php
$last_order_date= $row["Order_ID"];
echo $last_order_date;
} //end of while loop
?>
your first Id is in $first_id
I have created simple html form which has two major inputs:
Checkbox for multiple choices (for info: user can select multiple districts)
radio button for single input (for info: user after marking the checkbox in step one will now chose another only one input)
after this Table should be generated:
Note: Multiple tables have same column name so that the displayed table will include the merged data for selected district names
Till now, I have created table for only one district i.e. for individual district. (my database resides in PostgreSQL)
This is database connection for my Table:
$db = pg_connect('host=localhost port=5433 dbname=MergedDB user=postgres password=admin');
I have two array for my table:-
$userclass = array('0-5','6-10','11-15', '>15','Total');
$btotal = array();
The query fetching code is:-
$query = " select * from "Arscenic_Test"
.
.
.
/* some query here*/";
$btresult = pg_query($db, $query);
while($btresults = pg_fetch_row($btresult)){
$count = count($btresults);
$y = 0;
while ($y < $count)
{
$c_row = current($btresults);
$btotal[] = $c_row;
next($btresults);
$y = $y + 1;
}
}
And my HTML table is:-
<?php
for($i=0; $i<5; $i++){
?>
<tr>
<td><?php echo $userclass[$i];?></td>
<td><?php echo $btotal[$i];?></td>
<td><?php echo $perb10[$i];?></td>
<td><?php echo $bettotal[$i];?></td>
<td><?php echo $pbet[$i];?></td>
<td><?php echo $b51_100total[$i];?></td>
<td><?php echo $pb51_100[$i];?></td>
<td><?php echo $bt101_300total[$i];?></td>
<td><?php echo $pb101_300[$i];?></td>
<td><?php echo $abov300total[$i];?></td>
<td><?php echo $pabov300[$i];?></td>
<td><?php echo $total[$i];?></td>
<td><?php echo $ptotal[$i];?></td>
</tr>
How to give User a multiple choices and generate the table as per his inputs?
First of all fetch all the districts from the database and render it to the html table. Then you can choose different district from drop-down menu.
Sample Code:
<?php
$con=mysqli_connect("localhost","nalin","nalin123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT district FROM District");
echo "<table border='1'>
<tr>
<th>District</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['district'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Note: I do NOT want to touch any of the data inside of the database. I'm talking about deleting the HTML table.
Also, I have tried using ".html()" and ".remove()" on my table but they end up breaking the formatting and or not allowing any information to show up.
I am going to try to explain my problem to the best of my ability with as much code as I can, so if anything is missing please let me know and I will add it.
Right now I have an HTML page where I have a table.
This HTML table contains several rows of MYSQL data which is fetched from a database.
On page load the initial data is fetched and shown inside the table.
I have several different sorting options on the top of my page that I am trying to make sort the data in the table based on things like date created, is the person paid, and are the verified. (These are just a few examples.
I want to be able to clear the table on click of one of these sorting options and have PHP re-query the database and pull the information with the sorted condition attached.
The query works, and the data is added to the table. HOWEVER, instead of replacing the old data and showing the sorted data, the new (sorted) data, is APPENDED to the top of the table, thus leaving the old data.
How do I fix this? I only want the new sorted data to show.
This if statement checks to see if a drop-down menu called hasBeenSubmitted has had one if it's options clicked, and if so, it does the below based on the option selected.
if(checkValue($_POST['hasBeenSubmitted'])) {
switch($_POST['hasBeenSubmitted']) {
case 'yes':
$query1 = "SELECT *
FROM APPLICATION_Primary
INNER JOIN Application ON Application_primary.applicationId = Application.applicationId AND hasBeenSubmitted = 1
INNER JOIN Applicant ON Applicant.applicantId = Application.applicantId
INNER JOIN APPLICATION_Degree ON Application.applicationId = APPLICATION_Degree.applicationId
INNER JOIN (SELECT DISTINCT (
department_code
), academic_programCode, department_nameFull
FROM AcademicProgram) AS
ac_program ON APPLICATION_Degree.academic_program = ac_program.department_code
INNER JOIN APPLICATION_Transaction ON Application.transactionId = APPLICATION_Transaction.transactionId
ORDER BY Applicant.applicantId ASC";
//SOMEHOW CLEAR THE TABLE HERE?
//QUERY
$updatedapplicants = $db->query($query1);
populateTable($updatedapplicants);
break;
case 'no':
$query1 = "SELECT *
FROM APPLICATION_Primary
INNER JOIN Application ON Application_primary.applicationId = Application.applicationId AND hasBeenSubmitted = 0
INNER JOIN Applicant ON Applicant.applicantId = Application.applicantId
INNER JOIN APPLICATION_Degree ON Application.applicationId = APPLICATION_Degree.applicationId
INNER JOIN (SELECT DISTINCT (
department_code
), academic_programCode, department_nameFull
FROM AcademicProgram) AS
ac_program ON APPLICATION_Degree.academic_program = ac_program.department_code
INNER JOIN APPLICATION_Transaction ON Application.transactionId = APPLICATION_Transaction.transactionId
ORDER BY Applicant.applicantId ASC";
//SOMEHOW CLEAR THE TABLE HERE?
//QUERY
$updatedapplicants = $db->query($query1);
populateTable($updatedapplicants);
break;
}
}
Here is populateTable() which does exactly what you'd think. It takes the information received from the applicants variables and pulls the columns it needs and echo's them inside the table.
function populateTable($updatedapplicants) {
foreach($updatedapplicants as $applicant) {
$color = $color == 'light'? 'dark' : 'light';
?>
<tr
class='<?php echo $color;?>' id='applicant_data'>
<td style="text-align: center;"><?PHP echo '<input type="checkbox" class="processed_check" id="applicant-' . $applicant['applicant_id'] . '" ';
if($applicant['status'] == 1){
echo "checked";
}
echo '>';
?></td>
<td><?php echo $applicant['applicantId']; ?></td>
<td><?php echo $applicant['isPayingOnline']; ?></td>
<td><?php echo ($applicant['hasBeenSubmitted'] == 1) ? 'yes' : 'no'; ?></td>
<!-- applicant info -->
<td><?php
if($applicant['hasBeenSubmitted '] == 1) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$pdftitle = $applicant['applicationId']."_".$applicant['familyName']."_".$applicant['givenName']."_".$newDOB.".p df";
echo "<a href='getFile.php?FileName=" . $pdftitle . "&FileType=application' target='_blank'>" . $applicant['applicant_name'] . "</a>";
} else {
echo $applicant['givenName']." ".$applicant['familyName'];
}
?></td>
<!-- academic info -->
<td><?php echo $applicant['academic_programCode'] . ' - ' . $applicant['department_nameFull']; ?></td>
<td><?php echo $applicant['submittedDate'] ?></td>
<td><?php echo $applicant['startSemester'] ?></td>
<td><?php echo $applicant['startYear'] ?></td>
<td><?php echo ($applicant['isCompleted'] == 'Y') ? "yes" : "no"; ?></td>
<td><?php echo $applicant['studentType'] ?></td>
<td><?php echo $applicant['academic_load'] ?></td>
<td><?php
$email = $applicant['loginEmail'];
echo "<a href='mailto:$email'>$email</a>";
?></td>
<td><?php
if ($applicant['essayOldFileName']) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$ext = end(explode(".", $applicant['essayOldFileName']));
$filename = "essay_".$applicant['applicantId']."_".$applicant['givenName']."_".$applicant['familyName']."_".$new DOB.".".$ext;
echo "<a href='getFile.php?FileName=$filename&FileType=essay' target='_blank'>Essay</a>";
}
?></td>
<td><?php
if($applicant['resumeOldFileName']) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$ext = end(explode(".", $applicant['resume_file_name']));
$filename = "resume_".$applicant['applicant_id']."_".$applicant['given_name']."_".$applicant['family_name']."_". $newDOB.".".$ext;
echo "<a href='getFile.php?FileName=$filename&FileType=resume' target='_blank'>Resume</a>";
}
?></td>
</tr>
<?php }
}
Thank you, I appreciate all help. I am really stuck on this issue.