I have a simple booking page. Already booked places belong to class "notAvailable". I want to use ajax to query username who booked certain slot. How can I achieve that?
$mydb = mysqli_connect('localhost', 'root', '', 'registration');
$myquery = "SELECT day, time FROM reserve";
$result = mysqli_query($mydb, $myquery);
$data_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$data_array[$row['day']][] = $row['time'];
}
$arr2=array('Monday','Tuesday','Wednesday','Thursday','Friday');
foreach($arr2 as $v)
{
if(array_key_exists($v,$data_array))
{
$arr4[$v]=$data_array[$v];
}
}
$times = ['08:00', '09:00','10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00'];
?>
I used fetch_assoc funtion to get all booked slots from database just in once
And here I need ajax query to get username for each booked cell when mouse is on the cell.
<TABLE class="myTimetable">
<THEAD>
<TR>
<TH></TH>
<TH>Monday</TH>
<TH>Tuesday</TH>
<TH>Wednesday</TH>
<TH>Thursday</TH>
<TH>Friday</TH>
</TR>
</THEAD>
<TBODY>
<?php
foreach($times as $time)
{?>
<tr>
<?php
$nome=$nome+1;
echo $nome;
echo "<td>$time</td>";
foreach($arr4 as $aa => $bb)
{
if(in_array($time , $bb))
{
echo "<td class='notAvailable'></td>";
}
else{
echo "<td class='available'>mine</td>";
}
}
?>
</tr>
<?php
}
?>
</TBODY>
</TABLE>
I see no querys for the who reserved the time slot, but to show the data you can do something like this
<?php
$byHow = "for now unknown";
echo '<td class="notAvailable" data-by-who="'.$byWho.'">timeslot</td>';
in javascript
$('.notAvailable').on('mouseover',function(){
alert($(this).data('byWho'));
});
Related
I have a table which displays data fetched from the database. I have done this in PHP say $data_array();
What I want is to find the record with same Email address and highlight those rows with Red. Meaning, if an email address is repeated in the table twice or thrice, all the 3 rows will get Red highlighted.
I have done this way:
$exist = array();
foreach($data_array as $da){
if(in_array($da['Email'], $exist)){
$row_color = "#f00"; // Red
}else{
$row_color = "#fff"; // White
}
echo "<tr style='color:".$row_color.";'>";
echo "<td>".$da['Email']."</td>";
echo "</tr>";
$exist[] = $da['Email'];
}
The above works and displays the second and the third row highlighted but not the first one. Obviously my code has nothing which will highlight the first record which has repeated value.
How can I do this? Can be done in PHP or Jquery also. Any help??
in jQuery, you can use filter() and use includes() to check if <td>'s text is in the array.
var emails = ['email1#bla.com', 'select#bla.com'];
$('table td').filter(function() {
return emails.includes($(this).text().trim());
}).css('backgroundColor', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
</tr>
<tr>
<td>email1#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
<tr>
<td>doNotSelect#bla.com</td>
</tr>
<tr>
<td>email1#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
<tr>
<td>doNotSelectToo#bla.com</td>
</tr>
<tr>
<td>someOtherEmail#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
</table>
You can separate the duplicate-check from the rendering of the HTML and do it beforehand, so that you know whether an email is duplicated when you are rendering your HTML.
Example
<?php
$data_array = [
[ 'Email' => 'test#example.com' ],
[ 'Email' => 'test2#example.com' ],
[ 'Email' => 'test#example.com' ],
[ 'Email' => 'test#example.com' ],
];
$email_count = [];
// Count emails.
foreach($data_array as $entry) {
$email = $entry['Email'];
if (isset($email_count[$email])) {
$email_count[$email]++;
} else {
$email_count[$email] = 1;
}
}
// Render table.
foreach($data_array as $entry) {
if($email_count[$entry['Email']] > 1){
$row_color = "#f00"; // Red
}else{
$row_color = "#fff"; // White
}
echo "<tr style='color:".$row_color.";'>";
echo "<td>".$entry['Email']."</td>";
echo "</tr>" . PHP_EOL;
}
Result
<tr style='color:#f00;'><td>test#example.com</td></tr>
<tr style='color:#fff;'><td>test2#example.com</td></tr>
<tr style='color:#f00;'><td>test#example.com</td></tr>
<tr style='color:#f00;'><td>test#example.com</td></tr>
Im fetching data from a table to another table that will be shown in my dashboard page for a user after loggin in.
But there is a problem with the indexes, i got this error:
Here is the line error:
Here is my code:
My view file ("usuario"):
<thead>
<th>id</th>
<th>User</th>
<th>Subject</th>
<th>Grade</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false) {
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['User']."</td>
<td>".$record['name']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My controller file ("login"):
public function home(){
$data['record']=$this->m_login->getDetails();
$this->load->view('usuario',$data);
}
My model file ("m_login"):
public function getDetails()
{
$st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata['id'])
->get()->result_array();
return $st[0];
}
You have the variable $records on view but not on controller
Change
$data['record'] = $this->m_login->getDetails();
To
// add this array() just in case no results found
$data['records'] = array();
$data['records'] = $this->m_login->getDetails();
$this->load->view('usuario', $data);
Another way is on controller
$results = $this->m_login->getDetails();
$data['records'] = array();
if ($results) {
foreach ($results as $result) {
$data['records'][] = array(
'id' => $result['id'],
'User' => $result['User'],
'name' => $result['name'],
'grade' => $result['grade'],
'date' => $result['date']
);
}
}
$this->load->view('usuario',$data);
View
<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>No Results Found</td>
</tr>
<?php } ?>
Wrong array index in your controller.
Change this
$data['record']=$this->m_login->getDetails();
to
$data['records']=$this->m_login->getDetails();
result_array() return returns result with array so you need to it like this -
if (count($record) > 0 && $record != false) {
foreach($record as $rec){
echo "<tr>
<td>".$rec['id']."</td>
<td>".$rec['User']."</td>
<td>".$rec['name']."</td>
<td>".$rec['grade']."</td>
<td>".$rec['date']."</td>
</tr>";
}
}
Please check this from above code and comment if you have any problem
this is my controller
function search_keyword()
{
$cari = $this->input->GET('cari');
$data['dapat'] = $this->order_test_m->search($cari);
$this->load->view('admin/a',$data);
}
this is my model
function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
this is my view
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Product Code</th>
<th>Payment Type</th>
<th>Delivery Date</th>
<th>Total Price</th>
<th style="text-align: center;padding-right: 15px;">Action</th>
</tr>
<?php if($dapat !== NULL) { ?>
<?php foreach ($dapat as $row => $test) {
?>
<tr>
<td><?= $test->user_order_id?></td>
<td><?= $test->sender_name?></td>
<td><?= $test->user_product_order_id?></td>
<td><?= $test->payment_type ?></td>
<td><?= $test->time.$test->date ?></td>
<td><?= $test->delivery_price?></td>
</tr>
<?php }
}else{
echo "<td colspan='3'>no customer for the result!</td>";
}
?>
</table>
guys i need help here
im new in codeigniter.
i need to make a search but the search result are needing 2 table from my database. time, date and user_product_order_id are from the uhd_user_product_order, and user_order_id, sender_name, payment_type, and user_order_reference(this the search key) are from uhd_user_order
in the view i can view it from my table uhd_user_order, but i cant view the time, date and the user_product_order_id
can you help me how to join the 2 table so i can see the best result from the search
Use this code on your model
public function search($cari){
$this->db->select('*');
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t2.user_order_id = t1.user_order_id"); # confirm user_order_id in both table
$this->db->where('user_order_reference',$cari);
$query = $this->db->get();
return $query->result();
}
function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
become
function search($cari)
{
$this->db->join("uhd_user_product_order" , "uhd_user_order.user_order_id = uhd_user_product_order.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
I have an array such:
$prices = array();
Along with a MySQL query and fetch:
$query = "SELECT $columns FROM Prices WHERE `key` LIKE '$rows' LIKE '$AirportPU' AND rate LIKE '$rate'";
if($results = $db->query($query))
{
if($results->num_rows)
{
while($row = $results->fetch_object())
{
$prices[] = $row;
}
$results->free();
}
I've printed out the table using the following code: (I have removed some table columns)
<?php
if(!count($prices)) {
echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
} else {
?>
<table>
<thead>
<tr>
<th>Location</th>
<th>City</th>
</tr>
</thead>
<tbody>
<?php
foreach ($prices as $p) {
?>
<tr>
<td> <?php echo $p->Location; ?> </td>
<td> £<?php echo $p->City; ?> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } ?>
I can return data from the MySQL query, and print this to the table. However, sometimes not all the columns need be printed as they have no values. I would like to hide these columns.
I have tried checking the array using:
print (isset($prices["City"])) ? "Exists</br>" : "Doesn't Exist</br>" ;
But that always returns "Doesn't Exist"
if (array_key_exists("City",$prices))
{
echo "Element exists!";
}
else
{
echo "Element does not exist!";
}
That also returns false.
You need to check it inside the foreach loop which you are executing.
print (isset($p->City)) ? "Exists</br>" : "Doesn't Exist</br>" ;
Check with
if(empty($price['City'] || $price['City'] == "")){
//Hide column
}
What I want:- I want to calculate the total working hours of my employees based on the working hours of multiple days.
My problem the total working hours ($total_hours) is not working.
My Model
class Attendence extends AppModel {
function add($data){
if (!empty($data)) {
$this->create();
if($this->save($data)) {
return true ;
}
}
}
function fetchdata() {
return $this->find('all', array('conditions' => array('Attendence.date' > '2014-04-01',
'AND' => array('Attendences.date' < '2014-04-21'),
)));
}
}
My Controller
class EmployeesController extends AppController {
public $uses = array('Employee', 'Attendence', 'InsertDate');
public function add()
{
if($this->Employee->add($this->request->data)==true){
$this->redirect(array('action'=>'index'));
}
}
public function index(){
$this->set('employees',$this->Employee->Fetch());
$this->set('attendence',$this->Attendence->fetchdata());
$this->set('dates',$this->InsertDate->fetchdate());
}
}
My View
<div class="index">
<table>
<thead>
<th>Num</th>
<th>Employee</th>
<th>Salary/Hour</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total Hour</th>
<th>Total Salary</th>
</thead>
<?php
$id = 0;
foreach($employees as $e):?>
<? $id++ ?>
<tr>
<td><?php echo $e{'Employee'}{'id'} ?></td>
<td><?php echo $e['Employee']['firstname'], $e['Employee']['lastname'] ?></td>
<td style="text-align:center"><?php echo $e['Employee']['salary'] ?></td>'
<?php foreach($dates as $d):?>
<td><?php echo $d['InsertDate']['start_date'] ?></td>
<td><?php echo $d['InsertDate']['end_date'] ?></td>
<?php
$total_hours = 0;
foreach ($attendence as $et){
$ts1 = strtotime($et['Attendence']['in_time']);
$ts2 = strtotime($et['Attendence']['out_time']);
$diff = abs($ts1 - $ts2) / 3600;
$total_hours += number_format($diff,2);
}
//Total hours
echo '<td style="text-align:center">'.$total_hours.'</td>';
//Total Salary
echo '<td style="text-align:center">'.$total_hours*$e['Employee']['salary'].'</td>';
?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
</table>
</div>
A programmer friend of mine gave me a solution. But I dont know how to implement in CAKEPHP
I am updating the solution also
Look at here :
$total_hours = 0;
foreach ($attendence as $et){
$ts1 = strtotime($et['Attendence']['in_time']);
$ts2 = strtotime($et['Attendence']['out_time']);
$diff = abs($ts1 - $ts2) / 3600;
$total_hours += number_format($diff,2);
}
The code shows you that there is an array. the array contains (attendance id and in_time and out_time in each point).
The Important thing is you should check how you fill this array.
In the above foreach that you generate the table by $employees array , you have the (employee_id) wich name (id) here
So you should write a new query in your view!!!In the middle of your first foreach and before second foreach
before this line :
$total_hours = 0
You have to write a query and fetch data from DB like this :
//SELECT * FROM attendences
WHERE attendences.date > '2014-04-23' AND attendences.date < '2014-04-30'
AND id=$e['Employee']['id'] // is your employee_id in your first array.
So when you fetched data , You have a new array named "$attendence"
Then , your second foreach(which calculates the salary and total hours) should work correctly