I have a simple ticket sale registration system with 6 types of tickets and 3 different "physical" sales locations. Each order is inserted as a row in the database and stores the amount of each type of ticket along with the sale location, total cost and a datetime timestamp.
I want to display the total amount of each ticket type, that was sold within a given time frame along with the total cost of those tickets. I also want to filter the results based on sale location.
This is my db query:
"SELECT SUM(ticketType1), SUM(ticketType2), SUM(ticketType3),
SUM(ticketType4), SUM(ticketType5), SUM(ticketType6),
SUM(cost), saleLocation
FROM `orders`
WHERE time BETWEEN '2018-07-10 07:00:01'
AND '2018-07-11 07:00:00'"
Then I display it in a HTML table row:
<?php
while ($row = $result->fetch_assoc()) {
if($row["saleLocation"] == 'location1') { ?>
<div class="cell">
<?php echo $row["SUM(ticketType1)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType2)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType3)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType4)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType5)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType6)"] ?>
</div>
<div class="cell">
<?php echo number_format($row["SUM(cost)"], 0, "", "."); ?>
</div>
<?php } //end if
} // end while
?>
It works, but I'm trying to use an IF statement inside the WHILE loop to filter the result based on location, but the IF statement doesn't appear to have any effect and instead it displays the results from all 3 locations.
I know I can easily modify the query to achieve this, but I would rather not do that in this specific case.
I'm guessing that the problem still lies within the query, though?
Some help would be greatly appreciated.
Related
Basically, I am trying to display ratings (using kk star rating plugin) for different past bookings in my plugin. I am using do_shortcode() for different bookings with different ids. This is being echoed in a partial file.
<?php
/* GIVE RATINGS IF NOT ALREADY GIVEN */
if ($ratings_possible) {
echo do_shortcode("[kkstarratings id=" . $booking->id . "]");
?>
Here $booking->id is giving a unique booking id number based on which I am trying to get unique rating for each booking.
Issue
Currently, main file runs loop three times and this snippet does display stars perfectly, but of those three star-bar, last one is actually working. Last one accepts the ratings and updates the average, but the first two are not working. We can hover over all there stars-bar and click, but the only last one of it is actually working.
I have also used kk_star_ratings() method but same result.
More details
The following is a part of dashboard.php code. It is displaying every appointment/booking of a customer. Here $customer->future_bookings is displaying upcoming bookings (where obviously $ratings_possible is set to false). However, on $customer->past_bookings I have set $ratings_possible to true. Followed by the partial file that is responsible for each booking detail i.e. include('_booking_tile.php');
dashboard.php
<div class="customer-bookings-tiles">
<?php
foreach($customer->future_bookings as $booking){
$editable_booking = true;
$ratings_possible = false;
include('_booking_tile.php');
} ?>
</div>
<?php } ?>
<?php
if($customer->past_bookings){ ?>
<div class="latepoint-section-heading-w">
<h5 class="latepoint-section-heading"><?php _e('Past '.$appointterm, 'latepoint'); ?></h5>
<div class="heading-extra"><?php printf( __('%d '.$appointterm, 'latepoint'), count($customer->past_bookings)); ?></div>
</div>
<div class="customer-bookings-tiles">
<?php
foreach($customer->past_bookings as $booking){
$editable_booking = false;
$ratings_possible = true;
include('_booking_tile.php');
} ?>
</div>
Now, on the _booking_tile.php, it contains details of each booking like agent, time, status etc. Through $booking object we are getting the variables of a particular booking and their values and everything. Here is some part of code:
_booking_tile.php
<div class="customer-booking-info-row">
<span class="booking-info-label"><?php _e($agentterm, 'latepoint'); ?></span>
<span class="booking-info-value"><?php echo $booking->agent->full_name; ?></span></div>
<div class="customer-booking-info-row">
<span class="booking-info-label"><?php _e('Status', 'latepoint'); ?></span>
<span class="booking-info-value status-<?php echo $booking->status; ?>"><?php echo $booking->nice_status; ?></span></div>
</div>
<?php if ($editable_booking) { ?>
<div class="customer-booking-buttons">
<a href="<?php echo $booking->ical_download_link; ?>" target="_blank" class="latepoint-btn latepoint-btn-primary latepoint-btn-link">
<i class="latepoint-icon latepoint-icon-ui-83"></i>
<span><?php _e('Add to Calendar', 'latepoint'); ?></span>
</a>
<?php /* <i class="latepoint-icon latepoint-icon-ui-46"></i><span><?php _e('Edit', 'latepoint'); ?></span> */ ?>
<a href="#" class="latepoint-btn latepoint-btn-danger latepoint-request-booking-cancellation latepoint-btn-link" data-route="<?php echo OsRouterHelper::build_route_name('bookings', 'request_cancellation'); ?>">
<i class="latepoint-icon latepoint-icon-ui-24"></i>
<span><?php _e('Cancel', 'latepoint'); ?></span>
</a>
</div>
<?php } ?>
<?php /* GIVE RATINGS IF NOT ALREADY GIVEN */
if ($ratings_possible) {
echo do_shortcode("[kkstarratings id=" . $booking->id . "]");
?>
<?php } ?>
Resulting HTML code is perfectly fine, every rating-star (kk star plugin) has perfect code it has unique IDs meaning it should work fine. Moreover, as I said before when I insert three shortcodes with different IDs, it works fine.
That stars are displaying perfectly, but only the third one is actually working. When I click third one it records my rating and displays the average. While first two are not.
Stars work when I insert their shortcodes manually through WordPress. So it means that multiple shortcodes with IDs is possible.
Author provides kk_star_rating() function that can be used in the code, but it gives same result.
Only the last star is working others are just not recording anything. All three have same resulting HTML code except for the IDs which will be unique.
I will be happy to answer more of your questions.
Here's the screenshot, Third one is giving me the result when I click. But I can only hover over the first two and click multiple times with no result.
Screenshot
It was some sort of problem with the plugin. It was not compatible with newest version of WordPress. Used a different plugin and it worked fine. Calling multiple shortcodes through a loop worked perfectly.
Thanks to everyone who tried to help.
I am trying to make a webpage that will add a card filled with data from a database if there is a row of data there. I have a <div class> that formats the card. Is there a way to programmatically add the <div class> so each <div class> is a row of data?
This is the PHP I have, it does read all the rows properly:
//SQL SELECT statement
$result = $conn->prepare("SELECT userid, pName, pDesc, dDate FROM test");
$result->execute();
// assign returned array elements to variables
for($i=0; $row=$result->fetch(); $i++){
$pName = $row['pName'];
$pDesc = $row['pDesc'];
$dDate = $row['dDate'];
}
Here is the HTML, it currently only displays the last row of data:
<h1>Project Dashboard</h1>
<div class="project-container">
<label>Project Owner:</label>
<span><?php echo $pName; ?></span><br>
<label>Project Description:</label>
<span><?php echo $pDesc; ?> </span><br>
<label>Project Due Date:</label>
<span><?php echo $dDate; ?> </span><br>
<div class="progress-bar">
<div id="myBar" class="container purple" style="height:24px;width:25%">
</div>
</div>
</div>
As mentioned in the comments, the PHP values are overwritten upon each iteration of the for loop, so only the last row of values will be displayed. For example, $pName can only hold one value at a time. So after the loop, $pName is defined as the last row's pName value.
I suggest using a while loop to output your HTML rows. Below, I'm assuming each .project-container is a row. I'm also using PDO.
<?php
while ($row=$result->fetch(PDO::FETCH_ASSOC)) {
?><div class="project-container">
<label>Project Owner:</label>
<span><?=$row['pName']?></span><br>
<label>Project Description:</label>
<span><?=$row['pDesc']?></span><br>
<label>Project Due Date:</label>
<span><?=$row['dDate']?></span>
</div><?php
}
Alternatively, you could fetch all the rows into a single PHP array and then loop through the array.
$rows=$result->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
// output row's HTML, just like above
}
See PDO::fetchAll vs. PDO::fetch in a loop
for performance considerations.
Anyone knows how to do create multiple cards looping through a PHP array?
For example, I have 5 friend with 5 description corrresponding each friend (from a mysqli table) saved in $friendList.
So I want, for each row, to create and show a card with the friend as a header and its description as the content of the card.
This would be the loop
while ($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)){
// $row['friend'];
// $row['description'];
}
but then, I do not know how to create the cards with the variables obtained:
Assuming that you have $friendList variable already defined and it is a mysqli_result object, here's how you can do it:
<?php while($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)): ?>
<div class="w3-card-4 test">
<img src="img_avatar3.png" alt="Avatar">
<div class="w3-container">
<h4><b><?php echo $row["friend"] ?></b></h4>
<p><?php echo $row["description"] ?></p>
</div>
</div>
<?php endwhile; ?>
Feel free to ask any questions :-)
I have a php function to retrieve the three most recent entries to a table that holds news highlights. Here is my function:
function getHighlights(){
return $this->query("
SELECT *
FROM `highlights`
ORDER BY `inserted` DESC
LIMIT 3
");
}
These highlights are then placed on my homepage via a foreach loop. Here is my code:
<?php foreach($highlights as $a){ ?>
<div class="col-md-6 highlight">
<div class="highlightItem">
<img class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
<?php } ?>
I'd like for my homepage to ALWAYS be in this format:
News Highlight Format. However, the only reason it's in that format right now is because the images have predefined sizes that fit nicely together.
I want to be able to reference the most recent news highlight and set that image to always display as 300 x 210. I also want to reference the next two highlights and set them to always display as 300 x 100.
What's the best course of action for this?
<?php foreach($highlights as $index => $a){ ?>
if($index == 0){
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="210" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}else{
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="100" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}
<?php } ?>
try to use that code
I need to add a total to the categories of an online classifieds website. These totals will dynamically change just as the categories can so it needs to update each time the page loads. I believe i need to use a getTotal command but don't know how or where to input it. Thanks in advance for any help.
The code currently looks like this:
<div class="submenu">
<?php
foreach($catList as $row){
?>
<div class="submenutxt"><?php echo $row->category_name;?> </div>
<?php
}
?>
</div>
Total number of rows in $catList:
<?php
echo "$total rows: <br />";
foreach($catList as $row) { ?>
echo (count($row))." items in this category";
...