SQL query html table format - php

How to echo the left column table to "Daily" "Monthly" and "Yearly"? It currently displays repeatedly because the results of SQL queries on the right side are 3 items.
$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
$SubmissionID = $_GET["view_data"];
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";
echo "<table>";
foreach ($results as $result) {
echo "<tr>";
echo "<td>";
echo "<p>test1</p>";
echo "</td>";
echo "<td>";
echo $result->value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h4><a href='?view_all'>Back to List</a></h4>";
}
The current output of my code:
test1
dynamic_data_1
test1
dynamic_data_2
test1
dynamic_data_3
The output I need:
Daily
dynamic_data_1
Monthly
dynamic_data_2
Yearly
dynamic_data_3

add a counter:
$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
$SubmissionID = $_GET["view_data"];
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";
$count = 0;
echo "<table>";
foreach ($results as $result) {
$count++;
$title = 'DEFAULT Title';
if( $count == 1 ){
$title = 'Daily';
}
elseif( $count == 2 ){
$title = 'Monthly';
}
elseif( $count == 3 ){
$title = 'Yearly';
}
echo "<tr>";
echo "<td>";
echo "<p>" . $title ."</p>";
echo "</td>";
echo "<td>";
echo $result->value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h4><a href='?view_all'>Back to List</a></h4>";
}

Related

Decrypt data from MySQL database

I have the below code to show all data from a MySQL database in a HTMl database:
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!
For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=
Is there a way to decrypt the data before displaying it in the table?
I usually decrpyt the date in the following way:
$name_firstD = simple_decrypt($name_first , "secured");
You need to have an array of columns which are encrypted.
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
$encrypted_columns = array('password','code', 'first_name');
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
put the name of your encrypted column inside $encrypted_columns array.

output data based on two tables

I need to output a table based on two other tables as shown below:
case: there are two table "tbl_schedule" and "tbl_report"
this is my script:
$sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die (mysql_error());
while ($data = mysql_fetch_array($sql)) {
$account = schAccount($data['schedule_account']);
$sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
echo "<tr>";
echo "<td>".ucfirst($account['admin_fullname'])."</td>";
while ($data2 = mysql_fetch_array($sql2)) {
echo "<td>".$data2['rating']."</td>";
}
echo "<td>".$data['schedule_date']."</td>";
echo "</tr>";
}
So far I don't get the desired output. How should I change the script?
Your code is almost correct.
Add following lines:
$sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die(mysql_error());
while ($data = mysql_fetch_array($sql)) {
$account = schAccount($data['schedule_account']);
$sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
echo "<tr>";
echo "<td>" . ucfirst($account['admin_fullname']) . "</td>";
$bad = $good = $vGood = 0; // <-- ADD THIS LINE
while ($data2 = mysql_fetch_array($sql2)) {
if($data2['rating'] <=2){ // BAD
$bad++;
} else if($data2['rating'] <= 3){ // GOOD
$good++;
} else if($data2['rating'] > 3){ // VERY GOOD
$vGood++;
}
}
echo "<td>" . $bad . "</td>"; // Display the final value for bad
echo "<td>" . $good . "</td>"; // Display the final value for good
echo "<td>" . $vGood . "</td>"; // Display the final value for very good
echo "<td>" . $data['schedule_date'] . "</td>";
echo "</tr>";
}

Finding library details based on user search input - using LIMIT and OFFSET

Basically I have 4 fields in a form. I want to the user search for books in a library by either title, or by author or both. I also want the user to set the length of the list of items and from the starting point, these are not restrictions though the user does not have to specify.
Here is the code:
require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_GET["title"];
$authors = $_GET["authors"];
$st = $_GET["start"];
$ln = $_GET["length"];
$stmt = $dbh->prepare("SELECT title, authors, description, price FROM books WHERE title = :title LIMIT :length OFFSET :start");
$stmt->execute(array(':title' => $title,':start' => $st,':length' => $ln));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
}
echo "<table>";
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
So far it literally just returns me what I have typed in the input - so nothing much at all! Does anyone know how I can do this?
Adapt your code this way:
echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";
to print all the rows returned by the query. Your code was printing just the last row.

Displayind data in a codeigniter view using a table

Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.
i want it to echo a (-) where a student does not take the subject.thanks in advance
<table border='1'>
<tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
</tr>
<?php
//loop to display the names of students
$i = 1;
foreach ($overallresults as $result) {
echo "<tr>";
echo "<td>";
echo $i++;
echo "</td>";
$admNo = $result->admNo;
$total_marks = $result->total_marks;
$agp = $result->aggregate_points;
$mean_grade = $result->mean_grade;
$agp = $result->aggregate_points;
$result_id = $result->result_id;
$fname = "";
foreach ($students as $student) {
// print_r($student);
$admNo1 = $student->admNo;
if ($admNo == $admNo1) {
// print_r($student);
$fname = $student->firstName;
$mname = $student->middleName;
$lname = $student->lastName;
//}
// }
//echo "<tr>";
echo "<td>";
echo $fname . " " . $mname . " " . $lname;
echo "</td>";
}
}
foreach ($subjectresults as $subresult) {
// print_r($result);
$score = "0";
$admNo3 = $subresult->admNo;
$subCode = $subresult->subCode;
$score = $subresult->score;
if ($admNo == $admNo3) {
if ($subCode == '232') {
$score = $score;
}
if ($subCode == '101') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '102') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '121') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '231') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '232') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '233') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '311') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '312') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '313') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '443') {
echo "<td>";
if (!$score) {
echo 0;
} else {
echo $score;
}
echo "</td>";
}
if ($subCode == '565') {
echo "<td>";
echo $score;
echo "</td>";
}
}
}
?>
<?php
if (isset($term)) {
$term = $term;
}
if (isset($form)) {
$form = $form;
}
if (isset($year)) {
$year = $year;
}
if (isset($examCategory)) {
$examCategory = $examCategory;
}
//if ($admNo == $admNo1) {
// print_r($student);
//}
// }
echo "<td>";
echo $total_marks;
echo "</td>";
echo "<td>";
echo $mean_grade;
echo "</td>";
echo "<td>";
echo $agp;
echo "</td>";
echo "<td>";
echo "</td>";
//}
}
?>
</table>
<?php
}
?>
</div>
the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121
I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;
<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
101=>20,
102=>30,
103=>30,
104=>45
),);
$result2 = array(
'studentId'=>2,
'score'=>array(
101=>34,
102=>54,
103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>
Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.

Php undefine variable issue

I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c

Categories