I'm still in the process of learning SQL queries. I've tried searching around online, but I don't even know what terms I should be searching for, so I'm looking for someone to point me in the right direction. I have two tables -- appointments and logins. I'm trying to use the concat function to get the first and last name for both "apptFor" and "addedBy." Do I need to have separate queries? It seems like it should be able to get done in a single query.
<table border="1">
<tbody>
<tr>
<td width="140" align="center"><strong>Appt Date/Time</strong></td>
<td width="100" align="center"><strong>Appt For</strong></td>
<td width="300" align="center"><strong>Appointment</strong></td>
<td width="100" align="center"><strong>Added By</strong></td>
<td width="100" align="center"><strong>Date Added</strong></td>
</tr>
<?php
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy,a.added,concat(u.firstName,' ',u.lastName) as appAddedBy FROM appointments a ";
$query .=" INNER JOIN logins u on a.addedBy=u.userid WHERE a.userID='$uid' order by appDate asc";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['appDate']; ?></td>
<td><?php echo $row['apptFor']; ?></td>
<td><?php echo $row['appNote']; ?></td>
<td><?php echo $row['appAddedBy']; ?></td>
<td><?php echo $row['added']; ?></td>
</tr>
<?php }} ?>
</tbody>
</table>
If you need tow name you should join two time the logins
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy
,a.added,concat(u1.firstName,' ',u1.lastName) as appAddedBy
,concat(u2.firstName,' ',u2.lastName) as appApptFor
FROM appointments a ";
$query .=" INNER JOIN logins u1 on a.addedBy=u1.userid";
$query .=" INNER JOIN logins u2 on a.apptFor=u2.userid
WHERE a.userID='$uid' ";
order by appDate asc";
$result = mysqli_query($con, $query);
Related
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')";
I have a page that gives me info from database.
Date - notes - account_type
In account_type I have 3 types of account PS: A - B - C.
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
I want when I print the values it came out with different urls for each account_type.
Like: accounts.php?type=A , B or C
This is what i have tried
<?php
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
if ($showRowGeneral['account_type'] == 'b')
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
}
?>
<tr><td colspan='3'><a href='?type=A'>A</a>, <a href='?type=B'>B</a> or <a href='?type=C'>C</a></td></tr>
but how to achieve this?
Thanks advanced.
Have you tried to use $_GET ?
Read this for more info https://www.w3schools.com/php/php_forms.asp
In your code you want to use it like so,
if ($showRowGeneral['account_type'] == $_GET['type'])
If you want to have a link for each line :
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?echo $showRowGeneral['account_type'];?></td>
<td><?echo $showRowGeneral['riyal'];?></td>
<td><?echo $showRowGeneral['dollars'];?></td>
<td>your text</td>
</tr>
<?
}
I have two Table ownership_profile and socity_unit.
Query For table1: select * from ownership_profile where SID='$id'
Query For Table2: select * from socity_unit where socity_id='$sid'
I have to join with one query, but i don't have idea how to do it.
This is my Php code but gives error:
<!-----------------Table For User Names-------------------------------------->
<table border="1" align="center">
<tr>
<th>Unit No</th>
<th>Member Name</th>
<th>Wing</th>
<th>Unit</th>
</tr>
<?php
if(isset($_GET['submit']))
{
$sql = "select * from ownership_profile o inner join society_unit s on o.sid = s.society_id where o.sid = '$sid' ";
$result = mysql_query($sql);
$i=1;
while($row=mysql_fetch_array($result)){
?>
<?php
$name = $row['NAME'];
$unitid = $row['UNIT_ID'];
$sid = $row['SID'];
$wings = $row['wings'];
$unit_no = $row['unit_no'];
{
?>
<!--User Submit Result-->
<tr>
<td><?php echo $unitid; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $wings; ?></td>
<td><?php echo $unit_no; ?></td>
</tr>
<?php }?>
<?php
//echo "<br>";
$i++;
}
}
?>
inner join is what you want.
select *
from ownership_profile o
inner join society_unit s
on o.sid = s.society_id
where o.sid = '$sid'
This assumes that 'sid' and 'society_id' are the relationship identifiers.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
select * from ownership_profile o inner join socity_unit s on o.SID = s.socity_id where o.SID = '$SOCIETY_ID'
This query working fine but...... its generate dublicate entry from table
I am done with a CMS school system which i created from scratch for practice in php. My question is for example I have Accounting 101, Computer Science 101, however there must multiple times for Accounting 101. For example: Ticket 1035, 1036 are both Accounting 101 and they should appear in the same table, but in my code it shows them in different classes. Here is my code.
if(isset($_GET['id']))
{
$category = $_GET['id'];
$sql = "SELECT * FROM classes WHERE category_id = " . $category;
$query2 = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query2))
{
?>
<center><h3><?php echo $row->class_name . '-' . $row->units; ?> </h3></center>
<table border ="0" wdith="100%">
<tr>
<td>
<strong>Description: </strong>
<?php echo $row->class_description; ?>
</tr>
</td>
</table>
<br/>
<table border="1" width="44%">
<tr>
<td width="60"><b>Ticket</b> </td>
<td width="123"><b>Days</b></td>
<td width="120"><b>Hours</b></td>
<td width="64"><b>Room</b></td>
<td><b>Instructor</b></td>
</tr>
<tr>
<td width="60"> <?php echo $row->ticket; ?> </td>
<td width="123"><?php echo $row->days; ?></td>
<td width="120"><?php echo $row->start_hours . $row->time_format . '-' . $row->end_hours . $row->time_format2 ; ?> </td>
<td width="64"> <?php echo $row->room_number; ?></td>
<td><?php echo $row->instructor_name; ?></td>
</tr>
}//end while
}//end if
Its showing Accounting 101 with different tickets in different tables, but it should be all in 1 table. Thanks.
You need a double loop if you're trying to get records inside of records. For example:
while ($row1 = mysql_fetch_object($query1))
{
echo $row1->ParentName;
$query2 = 'select * from `mytable` where `myForeignKey` = ' . $row1->ParentId;
while ($row2 = mysql_fetch_object($query2))
{
echo $row2->ChildName;
}
}
You could also do a left join. Let me know if you need a sample of that.
Edit:
The left join would be done like this:
$sql = "select * from `classes` as a where category_id = '{$category}' left join `tickets` as b on a.id = b.class_id"
Ref. http://www.w3schools.com/sql/sql_join_left.asp
$row[0] is user_id when i click on user_id i need to know how to get the value i clicked from the html table using php so i can use this id to select its data into another form so i can edit it
$select = "SELECT U.id, U.first_name, U.last_name, U.email_address, U.password, U.gender, U.day, U.month, U.year, COUNT(O.user_id) FROM users U LEFT JOIN orders O ON U.id = O.user_id GROUP BY U.id ORDER BY id $order LIMIT $offset , $rowsPerPage";
$result=mysql_query($select) or die(mysql_error());
while($row=mysql_fetch_array($result)){ ?>
<table>
<tr>
<td><?php echo "<a href= 'editUser.html'> $row[0]</a>"; ?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><?php echo $row[4];?></td>
<td><?php echo $row[5];?></td>
<td><?php echo $row[6];?></td>
<td><?php echo $row[7];?></td>
<td><?php echo $row[8];?></td>
<td><?php echo $row[9];}
</tr>
</table>
Pass as query string and will be available in $_GET
<td><?php echo "<a href= 'editUser.html?id=".$row[0]."'> $row[0]</a>"; ?></td>
You can get in next page like this
echo $_GET['id'];
You can't "get" id from HTML table.
You can only send it using a hyperlink
$select = "SELECT U.id, U.first_name, U.last_name, U.email_address, U.password, U.gender, U.day, U.month, U.year, COUNT(O.user_id) FROM users U LEFT JOIN orders O ON U.id = O.user_id GROUP BY U.id ORDER BY id $order LIMIT $offset , $rowsPerPage";
$result=mysql_query($select) or die(mysql_error());
while($row=mysql_fetch_array($result)){
$data[] = $row;
}
?>
<table>
<tr>
<? foreach($data as $row) { ?>
<td><?php echo $row['id']?></td>
<? } ?>
</tr>
</table>
you can use Ajax request to send data from table to server.