I've been to this site several times before for help which I've managed to solve problems encountered in the past but this time I think I'm going to actually have to ask my own question.
I am working on a system in which employees of a small company can put in their appointments they had every week of the month, including the appointments, number of sales and amount of money made from them. I already calculated things like the success percentage and everything.
Now here lies the problem:
I need to make it so that the week numbers are automatically calculated rather than just put in 5 weeks a month.(Currently it works like: January 1-5 February 5 - 10 and so forth) And the numbers need to be linked to a page where the user can edit the information of that week.
I already know how to make a page like this too, the only thing I really need help with is finding out how to best put the week numbers in properly. (Security not being really an issue because it runs on an internal web server.)
Here's the table segment of my calendar.php :
<table id='myTable' width="600" border="1" cellspacing="3">
<tr>
<th align="left">
Prev </th>
<th colspan='3' width="200"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></th>
<th align="right"> Next </th></tr>
<?php
echo "<tr align='center'>";
//Rather than a loop, need to get the amount of weeks in each month.
$startingMonthWeekNum = ((($cMonth-1)*5)+1);
$endingMonthWeekNum = ($cMonth*5+1);
echo "<tr><th>Week #</th><th>Afspraken</th><th>Sales</th><th>Omzet</th><th>Conversie %</th></tr>";
for($i = $startingMonthWeekNum; $i < $endingMonthWeekNum; $i++)
{
//Reset variables
$appointments = null;
$sales = null;
$revenue = null;
$conversie = null;
$result = mysqli_query($con, "SELECT * FROM cms_appointments where date_week = $i and date_year = $cYear and id_user = $userId group by date_week");
$sumAppointments = 0;
$sumSales = 0;
$sumRevenue = 0;
while($row = mysqli_fetch_array($result))
{
$weekRow = $row['date_week'];
$appointments = $row['appointments'];
$sales = $row['sales'];
$revenue = $row['revenue'];
$conversie = $appointments > 0 ? ($sales / $appointments)*100 : 0;
}
$sumAppointments += $appointments;
$sumSales += $sales;
$sumRevenue += $revenue;
echo "<tr align='center'>";
echo "<td>$i</td><td>$appointments</td><td>$sales</td><td>$revenue</td><td>$conversie</td>";
echo "</tr>";
}
echo "<tr align='center'><td>Total</td><td>$sumAppointments</td><td>$sumSales</td><td>$sumRevenue</td><td>".($sumAppointments > 0 ? ($sumSales / $sumAppointments)*100 : 0)."</td></tr>";
echo "</tr> </table>";
mysqli_close($con);
?>
I have a small database behind this with the table cms_appointments having all the information in them which are used in this code. The appointments, sales and revenue.
This is the first time I posted a question and if it can be improved in some way, I will try my best to make it easier as this site helped me out greatly in the past, I want to leave this as a well explained first question.
Related
I am creating a page for someone where it will display all orders in the database, limited to 10 per page.
It works to that end, but now the issue is when I try to get a max number of pages using the below code:
$msql = "SELECT * FROM orders";
$mresult = $db->query($msql);
if(is_object($mresult) && $mresult->num_rows > 0)
{
if($mresult->num_rows > 10)
{
$maxpage = $mresult->num_rows / 10 + 1;
}
else
{
$maxpage = 1;
}
}
It won't set the $maxpage variable to anything other than 1 unless there are 20 or more orders in the database.
Right now, I have 11 test orders in the database, so when it divides by 10, it should set max pages to 1, + 1 additional page since there are more than 10 orders.
I have scoured the web looking for a solution, and have came up empty handed. Any thoughts or suggestions?
EDIT:
Added code at bottom of the table the actual orders are displayed for further troubleshooting:
<tr>
<td colspan="2" align="left"><<<< Prev</td>
<td align="center">Page <?php if(isset($_GET['page'])) { echo $_GET['page'] . ' of ' . $maxpage; } else { echo '1 of 1'; } ?></td>
<td colspan="2" align="right">Next >>>></td>
</tr>
Your code is running fine, the problem is your statement, the statement where you are dividing by 10, should be within parenthesis, because that needs to be computed first, then add 1 to it, in your code, addition is being done which equals 11, and then 11/11 = 1;
you probably should wrap it within floor function as well.
if($mresult->num_rows > 10)
{
$maxpage = (floor($mresult->num_rows / 10)) + 1;
}
else
{
$maxpage = 1;
}
Please use below code, I have tested.
$query = "select * from orders";
$history_res = mysql_query($query);
//count records
$totrecords = mysql_num_rows($history_res);
$totpages = ceil($totrecords / 10);
return $totpages;
Implementing the code provided by #Talha, and changing my navigation controls to the following fixed the issue:
<tr>
<td colspan="2" align="left"><<<< Prev</td>
<td align="center">Page <?php echo $page . ' of ' . $maxpage; ?></td>
<td colspan="2" align="right">Next >>>></td>
</tr>
What I want is show the total clicks per Category id of the items only for 20 items order by the highest total clicks per day.
Now I am using hard code and the result have to looks like this
So, if the item id and total clicks already fill up the
20columns (for item id and total clicks) + 2 for the tittle so means
22columns
It has to move to next row.
Because I am referring to my db, so I am using loop to create the table, and when I doing that way, I am getting this result....
The result will keep showing until the end in the left side. Thats very hard to read for report purposes. I wanted the result looks like the first figure that I've uploaded.
Here is what I am doing now:
include "Con.php";
//get the value from Get Method
$CatidValue = $_GET['CatIds'];
//(The date format would looks like yyyy-mm-dd)
$DateFrom = $_GET['DateFrom'];
$DateTo = $_GET['DateTo'];
//select the CatID
$SqlGet= "Select CatId from try where CatId = $CatidValue";
$_SqlGet = mysqli_query($connection,$SqlGet);
$TakeResultGet = mysqli_fetch_array($_SqlGet);
$CatIdTittle = $TakeResultGet ['CatId'];
echo"
For Category Id : $CatIdTittle
<br>
";
//For Loop purpose and break the value
$explodeValueFrom = explode("-",$DateFrom);
$explodeValueTo = explode("-",$DateTo);
$DateValueFrom = $explodeValueFrom[0].$explodeValueFrom[1].$explodeValueFrom[2];
$DateValueTo = $explodeValueTo[0].$explodeValueTo[1].$explodeValueTo[2];
//Loop through the date
for($Loop=$DateValueFrom; $Loop <= $DateValueTo; $Loop++){
$YearLoop= substr($Loop, 0,4);
$MonthLoop =substr($Loop, 4,2);
$DayLoop = substr($Loop, 6,2);
$DateTittleValue = $YearLoop."-".$MonthLoop."-".$DayLoop;
$trValue = "<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";
echo"
<table class='tg'>
<tr>
$trValue
</tr>
";
echo"
<table class='tg'>
<tr>
<td class='tg-yw4l'>Items Id</td>
<td class='tg-yw4l'>Total Clicks</td>
</tr>
";
//to get the item id and total clicks
$SqlSelect = "select `Item Id`,`Total Clicks`,Day from try where CatId = $CatidValue and Day = '$DateTittleValue' ORDER BY `try`.`Total Clicks` DESC limit 20";
$_SqlSelect = mysqli_query($connection,$SqlSelect);
foreach ($_SqlSelect as $ResultSelect) {
$Day = $ResultSelect['Day'];
$ItemId = $ResultSelect['Item Id'];
$TotalClicks = $ResultSelect['Total Clicks'];
echo"
<tr>
<td class='tg-yw4l'>$ItemId</td>
<td class='tg-yw4l'>$TotalClicks</td>
</tr>
";
}
}
?>
you need to add td dynamically within a table for each day report ( data should be grouped by date).
You just need to float each table with the float:left css property and some percentage based on the number of tables you want on the same row. In this case, 33% will do. Take a look at this codepen: http://codepen.io/anon/pen/EgXqPk
The magic happens on:
.tg {
width: 33%;
float:left;
}
you can try i hope this perfect for you to draw a table using PHP Loop.
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
so, what i wanted to do is show the total clicks per Category id of the items only for 20 items order by the highest total clicks per day.
now i am using hard code and the result have to looks like this
so, if the item id and total clicks already fill up the
20columns (for item id and total clicks) + 2 for the tittle so means
22columns
it has to move to next row.
because i am reffering to my db, so i am using loop to create the table, and when i doing that way, i am getting this result....
the result will keep showing until the end in the left side. thats very hard to read for report purposes. so i wanted the result looks like the first figure that i've uploaded.
here is what i am doing now
include "Con.php";
//get the value from Get Method
$CatidValue = $_GET['CatIds'];
//(The date format would looks like yyyy-mm-dd)
$DateFrom = $_GET['DateFrom'];
$DateTo = $_GET['DateTo'];
//select the CatID
$SqlGet= "Select CatId from try where CatId = $CatidValue";
$_SqlGet = mysqli_query($connection,$SqlGet);
$TakeResultGet = mysqli_fetch_array($_SqlGet);
$CatIdTittle = $TakeResultGet ['CatId'];
echo"
For Category Id : $CatIdTittle
<br>
";
//For Loop purpose and break the value
$explodeValueFrom = explode("-",$DateFrom);
$explodeValueTo = explode("-",$DateTo);
$DateValueFrom = $explodeValueFrom[0].$explodeValueFrom[1].$explodeValueFrom[2];
$DateValueTo = $explodeValueTo[0].$explodeValueTo[1].$explodeValueTo[2];
//Loop through the date
for($Loop=$DateValueFrom; $Loop <= $DateValueTo; $Loop++){
$YearLoop= substr($Loop, 0,4);
$MonthLoop =substr($Loop, 4,2);
$DayLoop = substr($Loop, 6,2);
$DateTittleValue = $YearLoop."-".$MonthLoop."-".$DayLoop;
$trValue = "<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";
echo"
<table class='tg'>
<tr>
$trValue
</tr>
";
echo"
<table class='tg'>
<tr>
<td class='tg-yw4l'>Items Id</td>
<td class='tg-yw4l'>Total Clicks</td>
</tr>
";
//to get the item id and total clicks
$SqlSelect = "select `Item Id`,`Total Clicks`,Day from try where CatId = $CatidValue and Day = '$DateTittleValue' ORDER BY `try`.`Total Clicks` DESC limit 20";
$_SqlSelect = mysqli_query($connection,$SqlSelect);
foreach ($_SqlSelect as $ResultSelect) {
$Day = $ResultSelect['Day'];
$ItemId = $ResultSelect['Item Id'];
$TotalClicks = $ResultSelect['Total Clicks'];
echo"
<tr>
<td class='tg-yw4l'>$ItemId</td>
<td class='tg-yw4l'>$TotalClicks</td>
</tr>
";
}
}
?>
You dont need to declare your trValueTitle in the loop. Plus you need to concatenate it in your echo, try this :
$trValueTittle ="1"."<th class='tg-amwm' colspan='2'>$DateTittleValue</th>" ;
echo"<table class='tg'>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
echo"<tr>". $trValueTittle ."</tr>";
}
I don't know what you want to do with the "1", but I think this is what you want to do:
echo "<table class='tg'>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
$trValueTittle ="<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";
echo"
<tr>
$trValueTittle
</tr>";
}
echo "</table>";
$trValueTittle ="1"."<th class='tg-amwm' colspan='2'>$DateTittleValue</th>" ;
echo"<table class='tg'>
<thead>
<tr>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
echo $trValueTittle;
}
echo"</tr>
</thead>
</table>";
#Mantello, good answer, but i think it's better to keep out the "tr" from the loop, cause usually you don't want to place your tableheads (th) in different rows.
#Rax your Code won't work. You'll recive an error. You can't output a Variable the way you did in your echo.
echo"<tr> '.$trValueTittle.' </tr>";
would be fine. But there's also no need to define your variable inside the for loop. This way you slow up your script.
So I have this code setup to display food entries by expiration dates on a table, from top to bottom, earliest to latest. If they are within three days of the current date, the text will be yellow,if it is anytime after that, the text will be red.
Anyway, I recently was testing and realized that if two entries have the same date, the way I have my code setup, it would pull show only one of the entries however number of times that expiration date is on the table (the very first entry with that date added to be exact).
So I setup a for loop uses some queries to check if there were more than one entries and then displayed each one subsequently. Problem is, I get to the final index for the loop, and the Name associated with that date will not show up. Heres the Code:
<?php
$sql1=mysql_query("SELECT email from loggedin WHERE session_id='$userid'");
$sess=mysql_fetch_array($sql1);
$newValue=$sess['email'];
$sqlLength = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue'");
?>
<div class="table-responsive">
<h2 class="sub-header">Expiration Dates</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Food Name</th>
<th>Brand Name</th>
<th>Food Group</th>
<th>Location</th>
<th>Expiration Date</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($test = mysql_fetch_array($sqlLength)){
$dates[$i] = $test['ExpirationDate'];
$i++;
}
$length=count($dates);
for($x=0;$x<$length;$x++){
$year[$x]=substr($dates[$x],6,4);
$month[$x] = substr($dates[$x],3,2);
$day[$x] = substr($dates[$x],0,2);
}
for($x=0;$x<$length-1;$x++){
for($y=$x+1;$y<$length;$y++){
if($year[$x] > $year[$y]){
$temp=$year[$x];
$year[$x]= $year[$y];
$year[$y]=$temp;
$temp=$month[$x];
$month[$x]= $month[$y];
$month[$y]=$temp;
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
elseif($year[$x] == $year[$y]){
if($month[$x] > $month[$y]){
$temp=$month[$x];
$month[$x]= $month[$y];
$month[$y]=$temp;
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
elseif($month[$x] == $month[$y]){
if($day[$x] > $day[$y]){
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
}
}
}
}
for($x=0;$x<$length;$x++){
$orderedDates[$x]= "$day[$x]-$month[$x]-$year[$x]";
}
$currentYear=date("Y");
$currentMonth=date("m");
$currentDay=date("d");
$multiple="false";
for($x=0;$x<$length;$x++){
$class='';
if($multiple=="false"){
$sql2 = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
$rows = mysql_fetch_array($sql2);
if(count($rows) > 7){
$y=1;
$multiple="true";
$count=count($rows)/7;
}
}
else{
$rows = mysql_fetch_array($sql2);
$y++;
//$x++;
}
$year=substr($rows['ExpirationDate'],6,4);
$month=substr($rows['ExpirationDate'],3,2);
$day=substr($rows['ExpirationDate'],0,2);
if($year==$currentYear){
if($month==$currentMonth){
if(($day - $currentDay) <= 3){
$class='expBad';
}
}
}
if($currentYear > $year){
$class='expGross';
}
elseif($currentYear == $year){
if($currentMonth > $month){
$class = 'expGross';
}
elseif($currentMonth == $month){
if($currentDay > $day){
$class='expGross';
}
}
}
?>
<tr>
<td><b><?php echo $rows['Name'] ?></td>
<td><b><?php echo $rows['Brand'] ?></td>
<td><b><?php echo $rows['Type'] ?></td>
<td><b><?php echo $rows['Container'] ?></td>
<td><b><?php echo "<span class=\"$class\">".$rows['ExpirationDate']."</span>" ?></td>
</tr>
<?php
if($y == $count){
$multiple="false";
}
}
/*
$er=mysql_query("SELECT Name FROM food WHERE ExpirationDate='$orderedDates[3]'");
$der = mysql_fetch_array($er);
$mer=$der['Name'];
echo $mer;
*/
?>
</tbody>
</table>
Some info to know, my food table has 7 columns.
orderedDates gives me a correctly ordered array (I know there's a better way to do that, but I was just starting php when I wrote that)
There are 4 food entries.
Here's the problem, when I get to $x = 3, for some reason it does not display any of the column values for that entry.
Here are what the entries should look like in the table.
what s Grains Pantry 12-02-2012
why s Grains Pantry 12-02-2012
who s Grains Pantry 08-04-2014
where s Grains Pantry 10-04-2014
I only get the first 3 entries and then the 4th row is empty.
the other two columns are id and OwnerEmail
It turns out the count() function was returning back a number other than what I thought.
Which is I thought $count would equal 1 for the value that had a unique expiration date.
But instead it was 2, which caused it to run the mysql_fetch_array function one more time than I wanted,making it point at a null value;
instead I used the following code to get the number of food items with same dates:
$sql2 = mysql_query("SELECT Name FROM food WHERE OwnerEmail='$emailname' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
$count2=0;
while($rows2 = mysql_fetch_array($sql2)){
$count2++;
}
I have these two objects:
$userinfo->pilotid;
$departures->total;
I'm trying to get $departures->total for specific pilotid in $userinfo = $userinfo->pilotid.
However, I'm not sure how can I link them so it echoes A for B. I have something like this but it does not display anything.
<?php echo $pilotid->$departures->total; ?>
Additionally, the first object is called like this:
$pilotid = Auth::$userinfo->pilotid;
This is the structure of the table where the objects are gathered from, using a query.
Stemming from the data provided by the OP, I am assuming, that $departures has a 1:n relationship with $userinfo, $userinfo being the 1 containing the pilotid.
So, in oder to find out how many departures that pilot had in total, there's two possible ways, one by using a subquery, which would mean something like this:
SELECT (SELECT COUNT(*) FROM `departures` WHERE `pilot_id` = ID) as total, * FROM pilots;
In this case, your total would be in the total column of your $userinfo query.
The second attempt makes use of actual PHP. In this scenario, you do the counting yourself.
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$row->departures = array();
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$userinfo[$row->pilotid]->departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
If this isn't what you're looking for, I will be needing more information from you, however like this you will be able to get the departures of the pilots either by making use of the total variable in the $userinfo object, or by simply calling count on the departures array.
Another variant, which keeps the actual departures and the pilots apart would look like this:
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
$departures = array();
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
I hope you will find these suggestions useful.
Edit:
After a few additional information from the OP, I suggest changing the query used to access the information in question.
This is the original code by the OP
$dep_query = "SELECT COUNT(pilotid) as total, depicao, pilotid FROM phpvms_pireps GROUP
BY depicao, pilotid ORDER BY total DESC LIMIT 5";
$fav_deps = DB::get_results($dep_query);
foreach($fav_deps as $departure)
{
$dep_airport = OperationsData::getAirportinfo($departure->depicao);
$pilotid = Auth::$userinfo->pilotid;
?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $pilotid->{$departures->total}; ?></td>
</tr>
<?php
}
?>
First thing we'll change is the query used to get the departures. Why fetch all the information, if we actually only want the one of the pilot in question?
$pilotid = $userinfo->pilotid; //As per Chat discussion
$dep_query = "SELECT COUNT(depicao) as total, depicao FROM phpvms_pireps WHERE pilotid = $pilotid GROUP BY depicao ORDER BY total DESC LIMIT 5";
This query will return the Top 5 of the departures from the different airports, which have been run by the pilot in question. As for the rest:
$fav_deps = DB::get_results($dep_query);
if(is_array($fav_deps)) { //For the general use
foreach($fav_deps as $departure) {
$dep_airport = OperationsData::getAirportinfo($departure->depicao); ?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $departure->total; ?></td> //Here is the actually changed Layout code
</tr>
<?php
}
} else echo "This pilot didn't have any departures yet.";
?>
With these alterations, your code should output the desired result. It is completely untested though. However it should give you the right idea.
I think what you need is this (note the curly brackets):
<?php echo $pilotid->{$departures->total}; ?>
Unless I'm misunderstanding the question...