how to show specific to mysql database - php

this is showing all result
but i want to show only if pserialno row is here then it will be show
how can i do this please help me
$result1 = mysql_query("SELECT * FROM workorder where opendate BETWEEN '$a' AND '$b' order by opendate ASC");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['opendate'] . '</td>';
echo '<td>' . $row['number'] . '</td>';
echo '<td>' . $row['pserialno'] . '</td>';

but i want to show only if pserialno row is here then it will be show
Just append your WHERE clause to return only those results where pserialno is not blank.
SELECT * FROM workorder where opendate BETWEEN '$a' AND '$b'
AND pserialno <> ""
ORDER BY opendate ASC

Related

How do i loop through but replace missing data with blanks in table

I am trying to display a table to show all the subjects the first student takes, then all the progress grades the student has made in that subject.
However, a student may not have a grade in a certain column so i need to place a blank or 'no grade' in place of it.
Instead i get them stacked side by side...
As you can see below '7(Pc3)' in English should be in the 'PC3' column and 'PC2' should say no grade or blank.... If possible -
Thanks
I have the loop working to fetch the students, plus the loop working to fetch all the subjects for that student.
And can display all the grades - but they don't line up with the right column
while ($res = $result->fetch_assoc()) {
echo "<tr><td>" . $res['subname'] . "</td>";
$result2 = mysqli_query($mysqli, "SELECT *
FROM grades
JOIN gradesets ON grades.gradeset_id = gradesets.id
WHERE grades.student_id = {$row['id']}
AND grades.subject_id = {$res['id']}
ORDER BY grades.gradeset_id ") or die($mysqli->error);
while ($res2 = $result2->fetch_assoc()) {
echo "<td>" . $res2['grade'] . "</td>";
//echo "<td>" . $res2['gradeset_id'] . "</td>";
//print_r($res2);
$resset = $res2['gradeset'];
$resset2 = substr($resset, -1);
//print_r($resset);
//print_r($resset2);
}
}
So i can echo out the right grades, but need to test they match up in the right columns... Here is the full code if needed...
$student = $mysqli->query("SELECT * FROM student");
echo "<center>";
echo "<h2>Data Wall</h2>";
echo "<h3>PHP</h3>";
echo "<hr/>";
while ($row = $student->fetch_assoc()) {
echo "<table border='1'>
<tr>
<th>ID</th>
<th>STUDENT</th>
<th>HOUSE</th>
</tr><br>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['stuname'] . "</td>";
echo "<td>" . $row['house'] . "</td>";
echo "</tr><br><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>";
echo "<tr><th>SUBJECTS</th><th>PC1</th><th>PC2</th><th>PC3</th><th>PC4</th></tr>";
$result = mysqli_query($mysqli, "SELECT subjects.id,subjects.subname
FROM student
JOIN grades ON student.id = grades.student_id
JOIN subjects ON subjects.id = grades.subject_id
WHERE student.id = {$row['id']}
GROUP BY subjects.subname ORDER BY subjects.id ") or die($mysqli->error);
while ($res = $result->fetch_assoc()) {
echo "<tr><td>" . $res['subname'] . "</td>";
$result2 = mysqli_query($mysqli, "SELECT *
FROM grades
JOIN gradesets ON grades.gradeset_id = gradesets.id
WHERE grades.student_id = {$row['id']}
AND grades.subject_id = {$res['id']}
ORDER BY grades.gradeset_id ") or die($mysqli->error);
while ($res2 = $result2->fetch_assoc()) {
echo "<td>" . $res2['grade'] . "</td>";
//echo "<td>" . $res2['gradeset_id'] . "</td>";
//print_r($res2);
$resset = $res2['gradeset'];
$resset2 = substr($resset, -1);
//print_r($resset);
//print_r($resset2);
}
}
}
echo "</tr>";
echo "</table>";
echo "</center>";
$mysqli->close();
?>
Since PHP 5.3 you can use Elvis operator - ?:
And since PHP 7 you are able to use Null Coalescing Operator - ??
Either of these you can use to display some other information if you row is empty. For example (PHP 7+):
echo "<td>" . ($res2['grade'] ?? 'No grade') . "</td>";
Would result to either a grade, or No grade text if string is empty or false.
Hope this helps!
In your inner query, you're doing an INNER JOIN, which selects only those rows that have a match in the gradeset table. It looks like you want a LEFT OUTER JOIN, so that you get null placeholders where there is no match:
SELECT *
FROM grades
LEFT JOIN gradesets ON grades.gradeset_id = gradesets.id
WHERE grades.student_id = {$row['id']}
AND grades.subject_id = {$res['id']}
ORDER BY grades.gradeset_id
This way, in your query result, instead of getting:
4 (PC1)
7 (PC3)
6 (PC4)
You'll get:
4 (PC1)
null
7 (PC3)
6 (PC4)
You could build an array of empty grades and then replace them with any data from the query. Like so:
$grades = [1 => '', 2 => '', 3 => '', 4 => ''];
while ($res2 = $result2->fetch_assoc()) {
$grades[$res2['gradeset']] = $res2['grade'];
}
foreach ($grades as $grade) {
echo "<td>" . $grade . "</td>";
}

Display column from SQL query with same name

how can I display the name of both teams (lteam and vteam)? Query works now... This is a screenshot of the SQL results:
http://prntscr.com/f9mkqh
$sql = "
SELECT *
FROM fixtures
LEFT
JOIN teams AS a
ON fixtures.lteam = a.id
LEFT
JOIN teams AS b
ON fixtures.vteam = b.id
WHERE date_ko = '2017-05-19'
";
echo '<table>';
echo '<tbody>';
foreach($pdo->query($sql) as $row)
{
echo '<tr>';
echo '<td>' . $row['lteam'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>-</td>';
echo '<td>' . $row['vteam'] . '</td>';
echo '<td>' . $row['b.name'] . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
Thanks!
The SQL result header is somewhat confusing, and as I don't know fully how the tables and schemas are structured, I have to assume you are having trouble with getting the name attribute from the teams table, as this is joined in two times.
You will need to select the columns explicitly and naming them something else for this to work. For example:
SELECT *, `a`.`name` as `team1name`, `b`.`name` as `team2name` FROM fixtures [...]
Now you should be able to grab the team names under their assigned aliases.

how to pull up the last entry of each product

I have an inventory table that gets updated with each transaction. Currently there are 4 products, but at some point there may be more. The following is the table I used to pull everything.
<?php
$db = new mysqli('', '', '', 'inventory');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM inventory";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table style='border: 2px;font-family: tahoma;'><caption><b>Entire Database Contents</b></caption><tr><td>ID</td><td>Time Stamp</td><td>Staff</td><td>Client</td><td>Needed</td><td>Product</td><td>Amount</td><td>Totals</td><td style='width: 200px;'>Comments</td></tr>";
while($row = $result->fetch_assoc()){
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['timeStamp'] . '</td>';
echo '<td>' . $row['staff'] . '</td>';
echo '<td>' . $row['client'] . '</td>';
echo '<td>' . $row['dateNeeded'] . '</td>';
echo '<td>' . $row['product'] . '</td>';
echo '<td>' . $row['amt'] . '</td>';
echo '<td>' . $row['tot'] . '</td>';
echo '<td>' . $row['comments'] . '</td></tr>';
}
echo "</table>";
?>
What I'd like to do is to pull ONLY the most recent entries of each product. I'm thinking maybe DISTINCT and LAST probably comes into play here but have no idea how to set it up. Any pointers?
You should create date_added field and sort by it:
$sql = "SELECT * FROM inventory ORDER BY date_added DESC";
You need to select only the products with MAX timestamp:
SELECT i.* FROM inventory i
JOIN (SELECT product, MAX(timestamp) timestamp FROM inventory GROUP BY product) i2
ON i2.product = i.product
AND i2.timestamp = i.timestamp
This should work:
select distinct product, id, timeStamp, staff, client, dateNeeded, amt, tot, comments from inventory order by timestamp desc;

How to join two tables with multiple common fields for search filter

I have 2 table bpi_registration and bpi_teamProfile. The fields in bpi_registration are : id, id_school, first_name,last_name,email,city,state,country and fields in bpi_teamProfile table are id_team, team_name, id_student1, id_student2, id_student3 now id_student1, id_student2, id_student3 contains the same id of students that were in bpi_registration. I am not sure how to connect multiple fields. The query that i have written is below. Please correct me if i am wrong:
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON bpi_registration.id=bpi_teamProfile.id_student1
AND bpi_registration.id=bpi_teamProfile.id_student2
AND bpi_registration.id=bpi_teamProfile.id_student3
AND bpi_registration.id=bpi_teamProfile.id_student4
AND bpi_registration.id=bpi_teamProfile.id_student5
I am trying to implement search filter in such a way that when someone clicks on Team dropdown then the firstname,lastname,email,city,state,country from bpi_registrationshows up. Below is my PHP code
if (isset($_GET['Team']))
{
$sql="SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON bpi_registration.id=bpi_teamProfile.id_student1
AND bpi_registration.id=bpi_teamProfile.id_student2
AND bpi_registration.id=bpi_teamProfile.id_student3
AND bpi_registration.id=bpi_teamProfile.id_student4
AND bpi_registration.id=bpi_teamProfile.id_student5"
$userQuery = "{$sql} WHERE bpi_teamProfile.team_name = :team_id";
$user = $db->prepare($userQuery);
$user->execute(['team_id' => $_GET['Team']]);
$selectedUser=$user->fetch(PDO::FETCH_ASSOC);
if(isset($selectedUser))
{
echo '<tr>';
echo '<td>' . $selectedUser['first_name'] . '</td>';
echo '<td>' . $selectedUser['last_name'] . '</td>';
echo '<td>' . $selectedUser['email'] . '</td>';
echo '<td>' . $selectedUser['address_city'] . '</td>';
echo '<td>' . $selectedUser['address_state'] . '</td>';
echo '<td>' . $selectedUser['address_country'] . '</td>';
echo '</tr>';
}
}
The URL looks when we click on team filter looks like this - https://www.example.com/retrieve1.php?Grade=&School=&Team=mary+winners&Students=
However i am not able to get the desired result.
It seems unlikely the student id columns in a given row all map back to the same id (in registration)...or the query would be returning properly. If they are different, do want to 'OR' the join?
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON (bpi_registration.id=bpi_teamProfile.id_student1)
OR (bpi_registration.id=bpi_teamProfile.id_student2)
OR (bpi_registration.id=bpi_teamProfile.id_student3)
OR (bpi_registration.id=bpi_teamProfile.id_student4)
OR (bpi_registration.id=bpi_teamProfile.id_student5)

Display Event Date, Name and then List Names who have signed up?! MySQL PHP

Really need help! I know this is easy to do but can't get my mind to switch on. I have a db table called 'Bookings' inside of which is the table columns 'StartDate' 'EventTitle' 'Fornames' 'Surname'
What I want to do is query the db and echo a list of the events by their startdate and title and then next to each one display the names that have booked onto each event.
When i run the following code it shows the StartDate, EventTitle and Forename but repeats this for every entry - I hope this makes sense.
$sql = "SELECT *
FROM Bookings";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array($result) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo '<table>';
echo '<tr>';
echo '<td>' . $row['StartDate'] . '</td>' . '<td>' . $row['EventTitle'] . '</td>'
.'<td>' . $row['Forenames'] . '</td>';
echo '</tr>';
echo '</table>';
}
Try:
$sql = "SELECT StartDate, EventTitle, GROUP_CONCAT(Fornames) as Attendees
FROM Bookings
GROUP BY StartDate, EventTitle";
$result = mysql_query($sql) or die (mysql_error());
echo '<table>';
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['StartDate'] . '</td>' . '<td>' . $row['EventTitle'] . '</td>'.'<td>' . $row['Attendees'] . '</td>';
echo '</tr>';
}
echo '</table>';
A couple of notes:
mysql_fetch_array() won't give you the field names in $row, but mysql_fetch_assoc() will.
The code you posted would skip the first row of results
<table> doesn't need to be put in for every row.

Categories