here are the following codes:
controller.php
function getchart() {
$prac = $this->input->post('prac_name');
$datee = $this->input->post('datee');
$this->load->model('appoint');
$results['appoint'] = $this->appoint->getappoint($prac , $datee);
$this->load->view('ajax/getappchart' , $results);
}
model.php
function getappoint($prac , $datee) {
$this->db->select('rdv.id as rdvid, startTime, endTime, day, firstname, lastname');
$this->db->from('rdv');
$this->db->join('contact', 'contact.id = rdv.contact_id');
$this->db->where('people_id',$practicien);
$this->db->where('DATE(day)', $datee);
$this->db->order_by('TIME(startTime)', 'ASC');
$query = $this->db->get();
//print_r($this->db->last_query());
return $query;
}
view.php
if ($appoint->num_rows() > 0) {
foreach($appoint->result() as $sub_row)
{
// display output.
}
} else {
echo 'No Appointments on Above Date.';
}
?>
what i need is, there are appointments with sametime and day(mostly 2 same).
if there is morethan 2, i need to set two different class style for both the appointment.
how can i achieve this ?
Thanks.
Final answer: done it with the help of #minhaz-ahmed
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//REST VIEW CODE
}
}
else {
echo 'No Appointments on Above Date.';
}
You can do like this
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
}
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//YOUR REST VIEW
}
} else {
echo 'No Appointments on Above Date.';
}
What do you mean by class style? Anyway, the code below assumes you will put the results in a datagrid.
$prev_date = "";
$prev_time = "";
$results = $query->result();
foreach($results as $appointment) {
if ($prev_date != $appointment->day) // Assuming "day" is tables date
# Display output here
if ($prev_time != $appointment->startTime) // Logically, display should sort appointments by their starting time
# Display output here
# Display appointment rows here
$prev_date = $appointment->day;
$prev_time = $appointment->startTime;
}
if (empty($results)) {
# Display "no appointments found" output here
}
Related
I am working with mysql and Rest Api in php,I want to use "orderby" according to condition (if "filterId"==3 or 4). In other words:
If I pass filterId='3' then query should be like SELECT ......ORDER by p.sp ASC/DESC
If I pass filterId='4' then query should be like SELECT ......ORDER by m.merchantName ASC/DESC
If I pass both parameters,filterId='3' and filterId='4' then query should be like SELECT ......ORDER by m.merchantName,p.sp ASC/DESC
If not pass filterId='3' or filterId='4' then orderby not apply/append
How can I do this? Here is my current code:
if(isset($_GET["Filter"]))
{
$filter=trim($_GET['Filter']);
$data=json_decode($filter);
// echo "<pre>";print_R($data);
foreach($data as $dt)
{
foreach($dt as $d)
{
//echo $d->filter_id;
if($d->filter_id=="1")
{
}
if($d->filter_id=="3")
{
}
}
}
Try this.
$query = "Select * from .... ORDER BY";
if(isset($_GET["Filter"]))
{
$filter=trim($_GET['Filter']);
$data=json_decode($filter);
// echo "<pre>";print_R($data);
foreach($data as $dt)
{
foreach($dt as $d)
{
//echo $d->filter_id;
if($d->filter_id=="1")
{
$query .='ORDER BY p.asp';
}
else if($d->filter_id=="3")
{
$query .='ORDER BY m.merchantName';
}
else
{
break;
}
}
}
For your third condition, you might have to use in_array_all() as below:
function in_array_all($lookfor, $mainarray) {
return empty(array_diff($lookfor, $mainarray));
}
//Try this
$query = 'select * from ...';
$order_by_clause = '';
if(isset($_GET["Filter"]))
{
$filter=trim($_GET['Filter']);
$data=json_decode($filter);
foreach($data as $dt)
{
foreach($dt as $d)
{
//echo $d->filter_id;
if($d->filter_id=="3")
{
$order_by_clause = 'ORDER BY p.sp ASC';
}
else if($d->filter_id=="4")
{
$order_by_clause = 'ORDER BY m.merchantName';
}
else if(($d->filter_id=="3") && if($d->filter_id=="4"))
{
$order_by_clause = 'ORDER BY m.merchantName,p.sp';
}
else if(!(($d->filter_id!="3") || if($d->filter_id!="4"))
{
$order_by_clause = '';
}
}
}
}
$query .= $order_by_clause;
This is my Codeigniter project, I've made a bookshop function that counts the amount of brought and sold for the book in each entry and minus the sold price and gives me the amount that I currently have for each book.
Type "0" = The amount that I received
Else it's the amount that I spent
What I want to do now is I want to fetch the total amount that I received between date1 and date2 which should look something like "get_book_amount" for date1 -(minus) "get_book_amount" from date2.
The equation is like this:
On Date1 total amount that I received is 100, on Date2 it's 300. So the total amount that increased during that time is "200" which is the number I want to get.
How can I achieve this?
public function bookshop($date1,$date2)
{
$total_books = 0;
$books = '';
$this->db->select("*");
$this->db->from('books_s');
$this->db->where(['books_s.type' => 'Fantasy']);
$this->db->where(['books_s.stock' => 'Yes']);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$count_books = $query->result();
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
$getamount = $this->get_book_amount($single_book->id,$date1,$date2);
if($getamount > 0)
{
$amt = $getamount;
}
else
{
$amt = -($getamount);
}
$total_books = $total_books+$amt;
$books .= '<tr><td><h4>'.$single_book->name.'</h4></td>
<td style="text-align:right" ><h4>'.$amt.'</h4></td></tr>';
}
$books .= '<tr"><td ><h4><i>Total Current Assets</i></h4></td><td style="text-align:right;" ><h4><i>'.$total_books.'</i></h4></td></tr>';
}
}
}
//USED TO COUNT SINGLE BOOK AMOUNT
public function get_book_amount($warehouse_id,$date1,$date2)
{
$count_total_amount = 0;
$this->db->select("bookentry.id as transaction_id,bookentry.date,bookentry.naration,book_stock_entry.*");
$this->db->from('book_stock_entry');
$this->db->join('bookentry', 'bookentry.id = book_stock_entry.parent_id');
$this->db->where('book_stock_entry.warehouse', $warehouse_id);
$this->db->where('bookentry.date >=', $date1);
$this->db->where('bookentry.date <=', $date2);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$count_books = $query->result();
$count_total_amount = 0;
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
if($single_book->type == 0)
{
$count_total_amount = $count_total_amount + $single_book->getamount;
}
else
{
$count_total_amount = $count_total_amount - $single_book->getamount;
}
}
}
}
if($count_total_amount == 0)
{
$count_total_amount = NULL;
}
else
{
$count_total_amount = number_format($count_total_amount,'3','.','');
}
return $count_total_amount;
}
I think it's best if you do two separete querys and then calculate.
Create a Model Method call "get_book_amount_until_date"
First call the method with your date 1 and save it in a var.
Second call the method with your date 2 and save it in another var.
Now do:
Total = Results_date_2 - Results_date_1
I need two print the same rows which retrieved from the db, in two different locations in same php file.
I know it is better to have a function. It tried, It doesn't work properly.
I am using the below code print the said rows/
$get_g = "SELECT * FROM profile_groups";
$get_gr = mysqli_query($condb ,$get_g);
if(mysqli_num_rows($get_gr) > 0)
{
while($groups = mysqli_fetch_array($get_gr))
{
echo "<option value='".$groups['profile_gid']."'>".$groups['profile_gname']."</option>";
}
}
else
{
echo '<option value="">Empty - No Groups!!</option>';
}
I need to print exactly the same code twice in two different location in a php file.
I think it is not a good idea to retrieve data twice from the server by pasting the above code twice.
Is there any way to recall or reprint the retrieved data in second place which I need to print.
Edit : Or else, if someone can help me to convert this to a function?
I converted this into a function. It prints only first row.
Edit 2 : Following is my function
unction getGroup($dbconn)
{
$get_g = "SELECT * FROM profile_groups";
$get_gr = mysqli_query($dbconn ,$get_g);
if(mysqli_num_rows($get_gr) > 0)
{
while($groups = mysqli_fetch_array($get_gr))
{
$groupData = "<option value='".$groups['profile_gid']."'>".$groups['profile_gname']."</option>";
}
}
else
{
echo '<option value="">Empty - No Groups!!</option>';
}
return $groupData;
You can store the records coming from the DB in array and use a custom function to render the element
$get_g = "SELECT * FROM profile_groups";
$get_gr = mysqli_query($condb ,$get_g);
$options = []; //store in an array
if(mysqli_num_rows($get_gr) > 0)
{
while($groups = mysqli_fetch_array($get_gr))
{
$options[$groups['profile_gid']] = $groups['profile_gname'];
}
}
Now you can use the $options array many times in your page
echo renderElement($options);
function renderElement($ops){
$html = '';
foreach($ops as $k => $v){
$html .= "<option value={$k}>{$v}</option>";
}
return $html;
}
If the data is same for both places, put the entire string into variable, then echo it on those two places.
instead of
echo "here\n";
echo "there\n";
do
$output = "here\n";
$output .= "there\n";
then somewhere
echo $output
on two places....
Values are being stored in groups array, hence you can use a foreach loop elsewhere to get values from the array:
$groups = array();
$get_g = "SELECT * FROM profile_groups";
$get_gr = mysqli_query($condb ,$get_g);
if(mysqli_num_rows($get_gr) > 0)
{
while($groups = mysqli_fetch_array($get_gr))
{
echo "<option value='".$groups['profile_gid']."'>".$groups['profile_gname']."</option>";
}
}
else
{
echo '<option value="">Empty - No Groups!!</option>';
}
// use here
foreach($groups as $group)
{
echo $group['profile_gid'] . " ". $group['profile_gname'] . "<br/>";
}
class ProfileGroups
{
public $profile_groups_options;
public static function get_profile_groups_options($condb) {
$get_g = "SELECT * FROM profile_groups";
if( isset( $this->profile_groups_options ) && $this->profile_groups_options != '') {
return $this->profile_groups_options;
}
$get_gr = mysqli_query($condb ,$get_g);
if(mysqli_num_rows($get_gr) > 0)
{
while($groups = mysqli_fetch_array($get_gr))
{
$this->profile_groups_options .= "<option value='".$groups['profile_gid']."'>".$groups['profile_gname']."</option>";
}
}
else
{
$this->profile_groups_options .= '<option value="">Empty - No Groups!!</option>';
}
return $this->profile_groups_options;
}
}
ProfileGroups::get_profile_groups_options($condb);
There are three tables; venues, venues_parameters and calendars tables. On a venue table all venues added and venue details added on venues_parameters table. On a calendar table, a booked (1st half, 2nd half, full day with flag 1, 2 & 3) venue detailed store with date and other information.
What i want that if i am searching with today date, date it is check on the calendar table that the searchable date are already booked or not, if date booked with 1st half or 2nd half venue will available on search else not available.
My controller code :
public function index(Request $request, $event_type = '', $capacity = '', $venue_type = '', $catering = '')
{
//dd($request->datepicker); return date (format : 25/12/2017)
$venueQuery = Venue::whereStatus('Active');
dd($venueQuery);
if($request->occasion) {
$event_type = $request->occasion;
}
if($request->datepicker) {
$selectedDate = $request->datepicker;
} else {
$selectedDate = date('m/d/Y');
}
if($request->capacity) {
$capacity = $request->capacity;
}
if($request->venue_type_id) {
$venue_type = $request->venue_type_id;
}
if($request->catering) {
$catering = $request->catering;
}
if($event_type != '') {
$venueQuery->whereHas('occasions', function ($query) use ($event_type) {
$query->where('occasions.id', $event_type);
});
}
if($capacity != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($capacity) {
$query->where('venue_parameters.max_capacity', $capacity);
});
}
if($venue_type != '') {
$venueQuery->whereHas('venueType', function ($query) use ($venue_type) {
$query->where('venue_types.id', $venue_type);
});
}
if($catering != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($catering) {
if($catering == "indoor") {
$catering = "1";
$query->where('venue_parameters.is_indoor', $catering);
} if($catering == "outdoor") {
$catering = "1";
$query->where('venue_parameters.is_outdoor', $catering);
}
});
}
$venues = $venueQuery->get();
$venues_count = count($venues);
return view('front.venue.index', compact('venues','event_type','venues_count','selectedDate'));
}
Calendar table for the reference :
I am in the process of turning my standard PHP project into something built on Symfony2. One part I have is this function
function viewAvailability(){
$db = Database::get();
$active = "Active";
// Fetch all the active alert IDs.
$idListSql = $db->prepare("SELECT DISTINCT id
FROM availability_alert
WHERE alert_status = :active");
$idListSql->bindParam(':active', $active);
$idListSql->execute();
$alerts = array();
// Go through each ID.
while ($idListRow = $idListSql->fetch(PDO::FETCH_ASSOC)) {
$alertId = (int)$idListRow["id"];
// Create the first dimension of the array, using the alert ID as the key.
$alerts[$alertId] = array();
// Fetch all the availability values for this alert.
$availabilitySql = $db->prepare("
SELECT availability, last_updated, class_letter, alert_pseudo, flight_number
FROM availability_alert_availability
WHERE availability_alert_id = {$alertId}
ORDER by class_letter, last_updated");
$availabilitySql->execute();
// Go through each availability result.
while ($aRow = $availabilitySql->fetch(PDO::FETCH_ASSOC)) {
// Fetch the date and hour for this availability row as a string.
$dateString = new DateTime($aRow["last_updated"]);
$dateString = $dateString->format('d M Y H:00');
// Create the second dimension of the array, using the alert pseudo as the key.
if (empty($alerts[$alertId][$aRow["alert_pseudo"]])) {
$alerts[$alertId][$aRow["alert_pseudo"]] = array();
}
// Create the third dimension of the array, using the flight number as the key.
if (empty($alerts[$alertId][$aRow["alert_pseudo"]][$aRow["flight_number"]])) {
$alerts[$alertId][$aRow["alert_pseudo"]][$aRow["flight_number"]] = array();
}
// Create the fourth dimension of the array, using the date string as the key.
if (empty($alerts[$alertId][$aRow["alert_pseudo"]][$aRow["flight_number"]][$dateString])) {
$alerts[$alertId][$aRow["alert_pseudo"]][$aRow["flight_number"]][$dateString] = array();
}
// Create the fifth dimension of the array, using the class letter as a key, and the
// availability value as the value.
$alerts[$alertId][$aRow["alert_pseudo"]][$aRow["flight_number"]][$dateString][$aRow["class_letter"]] = $aRow["availability"];
}
}
}
This is only part of the function, I then go onto a lot of loops to output the data in a table. Anyways, I have moved my database calls above into my Entities custom repository (using DQL instead).
I then do most of the above working in my controller
public function availabilityAction()
{
$em = $this->getDoctrine()->getEntityManager();
$alerts = $em->getRepository('NickAlertBundle:AvailabilityAlert')->getActiveAlertIds();
$alertsArray = array();
if (!$alerts) {
throw $this->createNotFoundException('Unable to find Availability.');
}
foreach($alerts as $alert){
$alertId = (int)$alert['id'];
$alertsArray[$alertId] = array();
$allAvailability = $em->getRepository('NickAlertBundle:AvailabilityAlert')->getAlertAvailability($alertId);
foreach($allAvailability as $alertAvailability)
{
$dateString = $alertAvailability['lastUpdated'];
$dateString = $dateString->format('d M Y H:00');
if (empty($alerts[$alertId][$alertAvailability['alertPseudo']])) {
$alertsArray[$alertId][$alertAvailability['alertPseudo']] = array();
}
if (empty($alerts[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']])) {
$alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']] = array();
}
if (empty($alerts[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString])) {
$alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString] = array();
}
$alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString][$alertAvailability['classLetter']] = $alertAvailability['availability'];
}
}
return $this->render('NickAlertBundle:Page:availability.html.twig', array(
'alertsArray' => $alertsArray,
));
}
Now it is a lot neater, but I feel there is too much going on in my controller (not sure if this is good or not). The other problem is that I am then passing this filled up array I have to my view. Now I have a complex table layout to create from this array, and I really dont think it should be the job of the view to do this. As an example, this is part of the code I would need to convert within the view
if (!empty($pseudos)) {
foreach ($pseudos as $pseudo => $flights) {
foreach ($flights as $flight => $dates) {
$firstDate = array_pop(array_keys($dates));
echo '<div class="availability_table_container">';
echo "<table class='availability_table'>";
echo "<tr>";
if ($i == 0) {
echo "<th></th>";
}
echo "<th class='pseudo-header'>{$flight}</th>";
echo "</tr>";
echo "<tr>";
foreach (array_keys($dates[$firstDate]) as $classLetter) {
if ($j == 0) {
echo "<th></th>";
}
$j++;
echo "<th class='class-header'>{$classLetter}</th>";
}
echo "</tr>";
foreach ($dates as $date => $classes) {
echo "<tr>";
if ($i == 0) {
echo "<td class='time_row'>{$date}</td>";
}
foreach ($classes as $classLetter => $availability) {
if ($availability >= 0) {
$className = $availability > 0 ? "green" : "red";
}
if ($availability == "." || $availability == "-") {
$className = "purple";
}
echo "<td class='number_row {$className}'>{$availability}</td>";
}
echo "</tr>";
}
$i++;
echo "</table>";
echo "</div>";
}
}
}
So what's the best way to handle this code? I think I am right in not allowing my view to handle the above code, but then where should I do it?
Any advice on the design is appreciated.