PHP Script - Activity Description not reflecting record values - php

I had a need for a system that could track billable tasks for tickets that we processed. I have a script that is almost complete, but I cannot seem to figure out why the tickets_tasks_activity field is not pulling the right enum_short_description when displaying. When I add a task the values are added correctly, its only not displaying them correctly. It seems that I can add one activity... and then every additional activity has the same description even though the values are different in the database. Could someone please help me out.
Here is the script to pull the information from the database.
* Get the tasks associated with a ticket.
*
* #company_id The ID of the company that is being queried.
* #ticket_id The ID of the ticket that is being queried.
*/
function get_ticket_tasks($company_id, $ticket_id)
{
$sql = "SELECT u.user_first_name,
u.user_last_name,
u.user_id,
tn.tickets_tasks_posted_date,
tn.tickets_tasks_task,
tn.tickets_tasks_activity,
(SELECT enum_short_description FROM tbl_enum WHERE enum_id = tn.tickets_tasks_activity) tickets_tasks_activity_description
FROM tbl_tickets_tasks tn,
tbl_user u
WHERE tn.tickets_tasks_company_id = {$company_id}
AND tn.tickets_tasks_ticket_id = {$ticket_id}
AND tn.tickets_tasks_posted_user_id = u.user_id;";
$query = $this->db->query($sql);
// If nothing was returned invalid query.
if($query->num_rows() == 0)
return false;
return $query->result_array();
}
Here is the code for the View Page
<div class="tab-pane" id="tasks">
<div class="row">
<div class="col-lg-12">
<?php
if($tasks)
{ ?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Service Provided</th>
<th>Value</th>
<th>Who?</th>
<th>When?</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < count($tasks); ++$i)
{ ?>
<tr>
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
<td><?php echo nl2br($tasks[$i]['tickets_tasks_task']); ?></td>
<td><?php echo $tasks[$i]['user_first_name'].' '.$tasks[$i]['user_last_name']; ?></td>
<td><?php echo date("m/d/Y", strtotime($tasks[$i]['tickets_tasks_posted_date'])); ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<p>This ticket does not have any tasks.</p>
<?php
} ?>
</div>
</div>
</div>

Here you have (at the beginning of the loop):
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
you're printing always $task[0], you should print $task[$i]
PS: You should probably state your JOIN in the FROM section of your SQL clause (instead to put it on the WERE section), at first I though you had none :-)

Related

Displaying query rows dependent on another query's result

I have a function in PHP that prints a list of inventories with their IDs.
Another function lists the products in the inventory with the corresponding ID from the inventory list.
But I don't know how to get the inventory ID from the list correctly into the product list, and it doesn't work for me. I don't know exactly where I'm doing wrong.
When I list the products with a specific ID and refresh the page, I want the list to stay the same.
A function to get a list
<?
php function fetchSeznamInventur() {
global $userCon;
global $rowcount;
global $query;
global $inventuraId;
global $row;
$query = mysqli_query($userCon, "SELECT * FROM seznamInventur");
//$inventuraId = $row["id"];
$rowcount = mysqli_num_rows($query);
}
?>
List listing to HTML
<?php
<table border="1" class="invTable">
<thead>
<th>Datum</th>
<th>ID</th>
<th>Název</th>
</thead>
<?php
for ($inv = 1; $inv <= $rowcount; $inv++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["createdate"]?></td>
<td name="id"><?php echo $row["id"] ?></td>
<td><?php echo $row["name"]?></td>
<td><input type="submit" name="submit" value="Detail"></td>
</tr>
<?php
}
?>
</table>
?>
Obtaining products by inventory ID
<?php
function fetchInventura() {
global $userCon;
global $inventuraId;
global $rowcount;
global $query;
$query = mysqli_query($userCon, "SELECT * FROM inv WHERE inventuraId='$inventuraId'");
$rowcount = mysqli_num_rows($query);
echo $inventuraId;
}
?>
List of products in html
<?php
for ($products = 1; $products <= $rowcount; $products++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["inventuraId"] ?></td>
<td style="display: none;"><?php echo $row["name"]?></td>
<td><?php echo $row["ean"]?></td>
<td style="display: none;"><?php echo $row["plu"]?></td>
<td style="display: none;"><?php echo $row["externalId"]?></td>
<td style="display: none;"><?php echo $row["productId"]?></td>
<td><?php echo $row["quantity"]?></td>
<td><?php echo $row["versiondate"]?></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="submit" value="Detail">
won't do much, as it's not inside a form.
You probably need a hyperlink which goes to a "products" page and passes the inventory ID as a query parameter, e.g.
Display
And then on the products page, you'd use $_GET["inventoryID"] to get the ID, and pass that into your SQL query (but please use a parameter, don't inject variables directly into the SQL, it's a security risk).

Fetch data that don't exist in a table with a specific id from another table

I'm new to MySQL and PHP and I need your help.
I have made a web application for the check-ins that employees make at the company.
I have an SQL table named tblemployees that has the employees' emp_id, Name, Email ID etc.
The other table is named tblentries and has all the entries of each employee and variables such as id. emp_id, Name, Date, Hour etc.
The primary key of tblemployees is emp_id and emp_id is the foreign key of the tblentries table.
I want to make a table in my web app that shows the pending checks of the day.
To be more specific, my code so far is:
<div class = "pb-20" id = "tab">
<table class = "data-table table stripe hover nowrap">
<thead>
<tr>
<th class = "table-plus">Full Name</th>
<th>Email</th>
<th>1st Comp.</th>
<th>2nd</th>
<th>3rd</th>
<th>4th</th>
<th>5th</th>
<th>Department</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$sql = "SELECT tblemployees.Name, tblemployees.EmailId, tblemployees.Company1, tblemployees.Company2, tblemployees.Company3, tblemployees.Company4, tblemployees.Company5, tblemployees.Department, tblemployees.role FROM tblemployees LEFT JOIN tblentries ON tblentries.Date < '$todaysdate'";
$query = mysqli_query($conn, $sql) or die(mysqli_error());
while ($row = mysqli_fetch_array($query)) {
?>
<td class = "table-plus">
<div class = "name-avatar d-flex align-items-center">
<div class = "txt">
<div class = "weight-600"><?php echo $row['Name']; ?></div>
</div>
</div>
</td>
<td><?php echo $row['EmailId']; ?></td>
<td><?php echo $row['Company1']; ?></td>
<td><?php echo $row['Company2']; ?></td>
<td><?php echo $row['Company3']; ?></td>
<td><?php echo $row['Company4']; ?></td>
<td><?php echo $row['Company5']; ?></td>
<td><?php echo $row['Department']; ?></td>
<td><?php echo $row['role']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
*'$todaysdate' is a variable that stores today's date. It has the same format as tblentries.Date.
Whatever I have tried so far I haven't managed to show the right results.
I have made some entries for testing the app and the table that I want to make shows the names of the employees whose entries I have made for testing. (The test entries have made in the past, so their dates are smaller than $todaysdate.
I don't know which is better to use and how Not In, Not Exists or a Left Join?
Thank you :)
Found the solution. I used WHERE NOT EXISTS(), so as to fetch the names etc of those who belong to tblemployees table and do not have an entry for CURATE() in tblentries table :
$sql = "SELECT DISTINCT tblemployees.Name, tblemployees.EmailId, tblemployees.Company1, tblemployees.Company2, tblemployees.Company3, tblemployees.Company4, tblemployees.Company5, tblemployees.Department, tblemployees.role
FROM tblemployees
WHERE NOT EXISTS ( SELECT DISTINCT tblentries.emp_id, tblentries.Date FROM tblentries WHERE tblentries.emp_id = tblemployees.emp_id AND tblentries.Date = '$todaysdate')";

How can i use the relationship between tables to show data in php and mysql

i build a project about online shop and there is a page in the admin dashboard that show the information about the orders that coming from the users ..there is many tables
there is orders table with Order.php class.
there is products table with Product.php class.
there is orderdetails table with OrderDetails.php class ..in this table there is
id,order_id,product_id.
every thing is working well put i can't show the $product['name'] and $product['price']
because $orderDet return null value , I don't know how to catch the id from orderdetails table to use it to get the product table data .
the getOne function from class OrderDetails.php
//get one
public function getOne($id){
$query="SELECT * FROM `orderdetails`
WHERE `id` = '".$id."'";
$result=$this->connect()->query($query);
$orderdetails=null;
if($result->num_rows == 1)
{
$orderdetails = $result->fetch_assoc();
}
return $orderdetails;
}
Orders.php where i show the the customer's information and the orders they buy for the admin
<?php
session_start();
require_once 'classes/product.php';
require_once 'classes/Order.php';
require_once 'classes/Orderdetails.php';
require_once 'classes/Category.php';
require_once 'inc/header.php';
$ord=new Order;
$ordDet=new OrderDetails;
$prod=new Product;
$orders=$ord->getAll();
if(!isset($_SESSION['id']))
{
header('location:Login.php');
die();
}
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Email</th>
<th>Customer Phone</th>
<th>Customer Address</th>
<th>Product Name</th>
<th>Product Price</th>
</tr>
</thead>
**the problem is here**
<?php
foreach($orders as $order)
{
$orderDetails=$ordDet->getOne($order['order_id']);
//var_dump($orderDetails);
$product=$prod->getOne($orderDetails['product_id']);
?>
<tbody>
<tr>
<td scope="row"><?php echo $order['customerName']; ?></td>
<td><?php echo $order['customerEmail']; ?></td>
<td><?php echo $order['customerPhone']; ?></td>
<td><?php echo $order['customerAddress']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo $product['price']; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
</div>
<?php require_once 'inc/footer.php';?>
that is an image when i make var_dump($orderDetails)
Try the following.
$query = "SELECT * FROM `orderdetails` WHERE `id` != ''";
// or $query = "SELECT * FROM `orderdetails` WHERE `id` != 0";
$result = $con->query($query);
while($row = $result->fetch_assoc()){
$id = $row["id"];
echo "<h3>id is now $id</id>";
}
// The following line is where your problem lies
$query = "SELECT * FROM `orderDetails` WHERE `order_id` != ''";
/* I assume you know the best way to handle the following part */
$result = $con->query($query);
echo $result[0]["order_id"]; // or $result["order_id]
As an advice you need to have a way to select only new orders that you have not processed. You would not want your selection to include orders you have handled each time you want to get new data from the table.

Loop through database fields PHP MySQL

I am working on an election system and I am having difficulty solving a problem. Following is how my application looks like.
At the moment I get these results by adding the query multiple times on my php file. (for example run it where RaceID = 9, RaceID = 10 .........)
I am sure there should be a way of reading the RaceID as an array or some other way and get the results that way. Since these are JOIN tables I am also looking for a way of retrieving the RaceName (Presidential Names, Justice Supreme Court ... etc) and MainRaceName(Titles with red, blue, gray) as well. I hope I am making some sense so far...
Following is my code for
<!-- MAIN ELECTION TICKET MainID loop should control this -->
<div class="panel panel-red margin-bottom-40">
<div class="panel-heading">
<h2 class="panel-title"> <strong>REPUBLICAN NATIONAL</strong> </h2>
</div>
<!-- End then SUB ELECTION TICKET RaceID loop should control this section-->
<h3 style="background-color:#f5f5f5; margin:30px; padding:5px; font-weight:Bold ">Presidential Race </h3>
<!-- Finally Results section ELECTION RESULTS -->
<table class="table table-hover">
<thead>
<tr>
<th></th>
<th>Candidate</th>
<th>Votes</th>
<th>%</th>
</tr>
<!-- This area gets the sum of the votes for a specific RaceID -->
<?php
$result = mysql_query('SELECT SUM(CandidateVotes) AS value_sum FROM candidates WHERE RaceID =9');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
?>
</thead>
<tbody>
<?php
$query = "SELECT candidates.CandidateName, candidates.CandidateVotes, candidates.Party, mainrace.MainRaceName, race.RaceName, candidates.win
FROM candidates
JOIN race ON race.RaceID = candidates.RaceID
JOIN mainrace ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = 9";
$result = mysql_query($query) or die(mysql_error());
for($i=0; $row = mysql_fetch_array($result); $i++){
?>
<tr>
<!--Show winner Icon if the win field is selected as 1 from database -->
<td width="5px">
<?php if ($row['win']==1)
{
echo "<span class=\"glyphicon glyphicon-check\"></span>";
}
else
{
echo "<span class=\"glyphicon glyphicon-unchecked\" style=\"color:#cccccc\"></span>";
}
?>
</td>
<td><?php if ($row['win']==1){
echo "<strong>";
echo $row['CandidateName'];
echo "</strong>";
}
else {
echo $row['CandidateName'];
}
?>
</td>
<td width="20%"><?php echo $row['CandidateVotes']; ?></td>
<td width="30%"><?php
if ($row['CandidateVotes']>0){
echo number_format((float)(($row['CandidateVotes']/$sum)*100), 2, '.', ''). ' %';
}
else{
echo "N/A";
}
?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td><strong>Total Votes:</strong></td>
<td><strong><?php echo $sum ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
I really appreciate if you can provide some samples (loops) how I can resolve this problem. Your help is much appreciated. Sincerely,
You should be able to pull down everything you need in one query.
In order to sum up votes by candidate, you need to make sure you GROUP BY the candidate name.
Try something like this:
SELECT
SUM(candidates.CandidateVotes) AS value_sum,
candidates.CandidateName,
candidates.Party,
mainrace.MainRaceName,
race.RaceName,
candidates.win
FROM candidates
JOIN race
ON race.RaceID = candidates.RaceID
JOIN mainrace
ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = :raceId
GROUP BY candidates.CandidateName

Matching id's from two database tables to get username from one

I am wanting to match my user_id column from my announcements table to the id column in my users table. I then want to get the username from the users table where the id's match.
I initially had the following query
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements"))
I am getting the following error with my current code..
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in
Which I know what this means, but do I need to add in every column table from my users table for this to work or is there another way to do this? If I do need to add all of the columns as variables in my bind_result, does it matter which order I put them in? Announcements first or users or vise versa?
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements
INNER JOIN users
ON announcements.user_id = users.id")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id,
$announcements_user_id, $announcements_messages, $announcements_date);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $announcements_stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
}
update..
if ($announcements_stmt = $con->prepare("SELECT announcements.id, announcements.user_id, announcements.messages, announcements.date, users.username FROM announcements
INNER JOIN users
ON announcements.user_id = users.id")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id,
$announcements_user_id, $announcements_messages, $announcements_date, $announcements_username);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $announcements_stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
}
</table>
<?php
}
}
The warning indicates when you are binding the result fields into variables, the number of variables does not match the number of fields in the result set:
$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date, $announcements_username);
The easy way around this is to always specify the fields in the SELECT statement (just an example):
SELECT t1.id, t1.user_id, t1.messages, t1.date, t2.username
Instead of:
SELECT *

Categories