I have 3 Tables:
1.Personal - with the list fo the workers.
2.Codes - with the list of the shift.
3.Dienstplan - that like a working schedule
I have a following query
$query4 = $db->query("SELECT count(codes.lcfruh) AS front_lcfruh, name
FROM dienstplan
LEFT JOIN codes ON (dienstplan.schicht = codes.lcfruh)
LEFT JOIN personal ON personal.perso_id = dienstplan.perso_id
WHERE personal.status_sum = 'rezeption' AND dienstplan.kw = '$kw' AND dienstplan.schicht!='' AND personal.zeigen='ja' GROUP BY dienstplan.datum");
I want to have a count(codes.lcfruh) listed as in the inputfields listed as follows:
$names = array();
while ($result = $query4 ->fetch_object()) {
$names[] = $result->name;
echo '<p class="taglist1"><input name="" type="text" title="'.implode(', ', $names).'" class="zbroj'.$result->front_lcfruh.'" value="'.$result->front_lcfruh.'"></p>';
}
The result should be an array of 7 input fields with the count of count(codes.lcfruh) for every day in the week. I am getting the correct number of fields (7) and the count in the fields correctly. Now I wanted to list the names in the title="'.implode(', ', $names).'" of the fields that are having one of the codes in the codes table column "lcfruh". The problem is I am getting only one name listed for the first days, 2 names on the second day an so on.... And it is always the first name in the table.
Instead of just selecting the name, make a GROUP_CONCAT on it. As you have a GROUP BY, this gives you all names of that day separated by a comma.
SELECT count(codes.lcfruh) AS front_lcfruh, GROUP_CONCAT(name) AS name
Then in your echo just show the concated names like this:
echo '<p class="taglist1"><input name="" type="text" title="'.$result->name.'" class="zbroj'.$result->front_lcfruh.'" value="'.$result->front_lcfruh.'"></p>';
Related
I have two tables one having employee name, employee id and another table tblleaves having empid,Leave_Date, fromDate, toDate, Description.
If user choose one day leave it stores the date value to Leave_Date and if user choose multiple days it store value of from date and to date.
Now I want the monthly report of employees. In this page I want an employee name, Leave Days and Leave Dates. I tried codes but I got employee name repeatedly because they apply many leaves in that month i want to display employee name one time.
<?php
if(isset($_POST['apply'])){
$ym=$_POST['month'];
list($Year, $Month) = explode("-", "$ym", 2);
$sql = "SELECT distinct tblleaves.id as lid,tblemployees.FirstName,tblemployees.LastName,tblemployees.EmpId,
tblemployees.id,tblleaves.LeaveType,tblleaves.PostingDate,
tblleaves.Leave_Date from tblleaves join tblemployees on tblleaves.empid=tblemployees.id
WHERE YEAR(Leave_Date) = 2019 AND MONTH(Leave_Date) = 6";
echo $sql;
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr>
<td> <?php echo htmlentities($cnt);?></td>
<td><?php echo htmlentities($result->FirstName);?> <?php echo htmlentities($result->LastName);?></td>
<td><?php ?></td>
<td><?php $result->Leave_Date?></td>
<?php $cnt++;}}}?>
I want employee monthly leave report
employee name Leave Days Leave Dates
KrishnanR 3 12-06-2019, 13-06-2019, 14-06-2019
Really that sort of formatting should be done in the report itself by having a band that occurs once per emplyee and shows the name and then within band show all the records for that employee.
However you can get a dataset containing the example result you posted by using the MySQL function GROUP_CONCAT().
The following code will give you three columns, FirstName, LastName and leave_dates with one row per employee. The leave_date column will contain all their tblleaves.Leave_Date values, separated by a comma and a space (or whatever appears after the key word SEPARATOR.
You won't need the DISTINCT
SELECT
tblemployees.FirstName,
tblemployees.LastName,
count(tblleaves.empid) as Leave_Days,
GROUP_CONCAT( tblleaves.Leave_Date SEPARATOR ', ' ) AS leave_dates
FROM
tblleaves
JOIN tblemployees
ON tblleaves.empid = tblemployees.id
WHERE YEAR(Leave_Date) = 2019
AND MONTH(Leave_Date) = 6
GROUP BY tblemployees.EmpId
I have three tables: tbl_days, tbl_slots and tbl_bookings as follows.
Customers are supposed to book for a slot in a day.
Am using the following query:
$slots = $this->common->slots();
if ($slots != NULL) {
foreach ($slots as $C):
$start = $C->start;
$stop = $C->stop;
$slotspace= $start." to ".$stop;
$available='Available';
$booked ='Booked';
$sum_col[] = " IF(tbl_bookings.slot_id =". $C->id.",'".$booked."','".$available."') AS '" . $slotspace . "'";
endforeach; }
$sqlquery = $this->db->query("SELECT tbl_days.date_day,
" . implode(",", $sum_col) . "
FROM tbl_days
LEFT JOIN tbl_bookings ON tbl_bookings.date = tbl_days.date_day
LEFT JOIN tbl_slots ON tbl_slots.id = tbl_bookings.slot_id
LEFT JOIN tbl_fields ON tbl_fields.id = tbl_bookings.field_id
");
return $sqlquery->result();
However, the results display as:
Intended result should be:
Please note how booking information displays on multiple rows for the same date.
Dates to be from the date today (for the next 7 days from today)
Display slots for the day on one row.
Display the date regardless of if there is a booking or not, like in date 29th.
Kindly help me on how to go about this.
Please note that am using datatables.
So each entry gets displayed in a new row instead of everything that's connected to each other in 1 row, right? I had the same problem in the past and the solution is a second foreach. The second one should look at all the records and see if one is connected to another.
See my question: Each value is displayed in a new row HTML table
I have a MySQL query that shows a list of items after joining two tables:
SELECT contenidos.tituloContenido FROM contenidos
JOIN cursosContenidos ON cursosContenidos.contenidoID = contenidos.contenidoID
WHERE cursosContenidos.cursoID = ?;
There's no problem with this query, but, when I change it to show another field of the contenidos table, the results are duplicated.
So they get duplicated when doing:
SELECT contenidos.contenidoID, contenidos.tituloContenido FROM contenidos
JOIN cursosContenidos ON cursosContenidos.contenidoID = contenidos.contenidoID
WHERE cursosContenidos.cursoID = ?;
I've tried adding a GROUP BY cursosContenidos.contenidoID clause, and DISTINCT as well, but they keep appearing duplicated. Any idea on why?
To retrieve the results, I'm using PHP:
$mostrarContenidos = $conectar1->prepare("
SELECT contenidos.tituloContenido FROM contenidos
JOIN cursosContenidos ON cursosContenidos.contenidoID = contenidos.contenidoID
WHERE cursosContenidos.cursoID = ?;
");
$mostrarContenidos->bindParam(1, $cursoID);
$mostrarContenidos->execute();
$contenidos = $mostrarContenidos->fetch(PDO::FETCH_ASSOC);
if ($contenidos) {
echo '<h2>Contenidos del Curso</h2>';
foreach ($contenidos as $value) {
echo 'Id: '.$contenidos['contenidoID'].'<br>';
echo 'Title: '.$contenidos['tituloContenido'].'<br>';
}
} else {
echo 'error retrieving results';
}
The expected result is:
Id: 1
Title This is my super title
What I get:
Id: 1
Title This is my super title
Id: 1
Title This is my super title
Your SQL query is most likely not returning multiple rows - you are only outputting it twice. This is because your foreach statement loops over each column in the single row you've fetched, rather than each row of the result set.
To clarify a bit further, this line just fetches the first (and probably only) row:
$contenidos = $mostrarContenidos->fetch(PDO::FETCH_ASSOC);
The foreach loop goes through each column in the row, and outputs info about the whole row for each column.
If you want $contenidos to be an array of all rows, you could do this instead:
$contenidos = $mostrarContenidos->fetchAll(PDO::FETCH_ASSOC);
The problem is not in the query, but in the php.
In your foreach-loop you assign the result-array-elements to $value, but then you don't use $value.
You should use :
echo 'Id: '.$value['contenidoID'].'<br>';
echo 'Title: '.$value['tituloContenido'].'<br>';
This should do it: GROUP BY contenidos.contenidoID, contenidos.tituloContenido
Should work with distinct
SELECT distinct contenidos.contenidoID, contenidos.tituloContenido FROM contenidos
JOIN cursosContenidos ON cursosContenidos.contenidoID = contenidos.contenidoID
WHERE cursosContenidos.cursoID = ?;
i want to make reading toefl test. i have two tables named 'id_reading' and 'soal_reading'. id_reading table contain id and text. soal_reading contain id, question, option a b c d and answer. so i want to display data from that tables. but i want to display randomly all column in soal_reading tables except id column. i try but it wont random. please help.
<?php
include "conection.php";
$query = mysql_query("
SELECT id_reading.id
, text
, id_reading.text
, soal_reading.pertanyaan
, a
, b
, c
, d
, jawaban
from id_reading
, soal_reading
where id_reading.id = soal_reading.id
");
if ($query) {
while ($row = mysql_fetch_array($query)) {
echo "
<tr>
<td>".$row['id']."</td>
<td>".$row['text']."</td>";
$q = mysql_query("SELECT * from soal_reading order by rand()");
if ($q) {
while ($r = mysql_fetch_array($query)) {
echo "
<td>".$r['pertanyaan']."</td>
<td>".$r['a']."</td>
<td>".$r['b']."</td>
<td>".$r['c']."</td>
<td>".$r['d']."</td>
<td>".$r['jawaban']."</td>
<td>
Edit |
Delete
</td>
</tr>";
}
}
}
}
?>
Your query set in a random way questions but not fields in the question. So if you have 3 questions for example Q1, Q2,Q3 they are ordered in a random way, not the fields.
I suggest you to put your $r['a'] .... $r['d'] into an array and then extract a number from 0 to count($array) to get a possible answer and remove that element from the array. Do that until all possible answers are extracted. (count($array)==0).
In summary, your query provides you random order for your questions and in PHP you give a random order to your fields.
For some code, please comment.
Bye
I have 3 tables 1 is an item table, one is a note table and the other is a note image table.
When a user views item details, all notes are picked up for that item (there is a item_id field in the note table)
The notes can have multiple images attached to them these are stored in flat file but are referenced by the "note image" table.
Now when displaying item details I run a query to get all notes for a item... simple enough, then these results are looped through to output them onto the page.
Problem now arises after adding images to notes, how would you go about querying all notes for a item say
SELECT * FROM notes WHERE item = 1
then how would you loop though the result array getting all note images for a note say
SELECT * FROM note_img WHERE note_img_noteid = 27
Its hurting my head a little because I can't visualize how to get the results and output them in PHP.
---EDIT---
Think I may of got it,
SELECT
d.door_note_id,
d.door_note_doorid,
d.door_note_timestamp,
d.door_note_editedtime,
d.door_note_text,
u.user_name AS created_by,
e.user_name AS edited_by,
i.door_img_id AS img_id,
i.door_img_url AS img_url
FROM
user u,
door_note d
LEFT JOIN
user e
ON
user_id = d.door_note_editeduserid
LEFT JOIN
door_img i
ON
door_img_noteid = d.door_note_id
WHERE
d.door_note_doorid = 214
AND
u.user_id = d.door_note_userid
Then I use this:
foreach ($result->result() as $row){
if(!isset($my_items[$row->door_note_id])){ //the note id becaoms a key
//here you set up an array for all the note details
$my_items[$row->door_note_id] = array('door_note_id'=>$row->door_note_id,
'door_note_doorid'=>$row->door_note_doorid,
'door_note_timestamp'=>$row->door_note_timestamp,
'door_note_editedtime'=>$row->door_note_editedtime,
'door_note_text'=>$row->door_note_text,
'created_by'=>$row->created_by,
'edited_by'=>$row->edited_by,
'images'=>array());
}
//if the note has any images add them to the images array for that note.
if(isset($row->img_url)){
$my_items[$row->door_note_id]['images'][] = $row->img_url;
}
}
Its very hard to know when you haven't post your relationships in a table but taking some assumptions
$query = "SELECT items.id as item_id, items.name as item_name, notes.id as note_id,
notes.description as note_description, note_image.image as note_image from notes
LEFT JOIN notes ON items.id = notes.item_id
LEFT JOIN note_image ON notes.id = note_image.note_img_noteid";
//this wil fetch all you items with description, notes and images, because item can have multiple notes, your result wil have multiple entires of the item. so you have to index correctly to use in views
$result = $this->db->query($query)
$my_items = array();
foreach ($result->result() as $row){
if(!isset($my_items[$row->item_id])){ //you item it becaoms a key
//here you set up an array for all your items
$my_items[$row->item_id] = array('item_name'=>$row->item_name, 'notes'=>array());
}
//here you stroe all images fro a note
if(!isset($my_items[$row->item_id]['notes'][$row->note_id])){
$my_items[$row->item_id]['notes'][$row->note_id] = array('note_description'=>$row->note_description, 'images'=>array());
}
$my_items[$row->item_id]['notes'][$row->note_id]['images'][] = $row->note_image;
}