There are 4 receiving methods. 1) delivery 2) pickup 3) mobile_topup 4) bank_deposit
if there are 2 mobile topups there I need only one. But, if there are 2 delivery methods then I need both f them. To achieve this, I have created this query:
$clause = "SELECT id FROM beneficiary_payment_info
GROUP BY IF(ben_payment_method='mobile_topup', '', ben_payment_method)";
But, the problem is, it's group by with delivery methods as well. I don't want that.
Right now, I have 2 mobile_topup, 2 delivery and bank deposit in database.
I need 1 mobile_topup, 2 delivery and 1 bank deposit.
I was just wondering that is it possible in this query or I need to do it in php?
EDIT
temporary, in php I have this solution. But, it doesn't look reliable.
$mobile_count = array();
for($i=0; $i<count($data); $i++){
if($data[$i]['ben_payment_method']=='mobile_topup'){
//unset($data[$i]);
array_push($mobile_count, $i);
}
}
if(count($mobile_count) > 1){
for($i=0; $i<count($mobile_count)-1; $i++){
unset($data[$mobile_count[$i]]);
}
}
$clause = "SELECT id FROM beneficiary_payment_info
GROUP BY IF(ben_payment_method='mobile_topup', ben_payment_method, id,)";
Related
I have an array of admission and each student has multiple entry of payment.
Like Admission=[1,2,3,4,5,6] this is admission ids,
and Payment=[[1->1,5,6,2],[2->2,3],[3->4,7,8] this is admission id->payment_ids
foreach($admission as $a)
{
$payment=DB::table('payement')->where('admission_id',$a->admission_id)->get();
foreach($payment as $p)
{
// Here i wan if payment_date <= today_date
// 1. Add all payment
// 2. Else Go to the next admission id
}
}
It sounds like you're looking for something like this:
foreach($admission as $a)
{
$payment = DB::table('payment')->where('admission_id', $a->admission_id)->get();
$totalPayment = 0;
foreach($payment as $p)
{
if (\Carbon\Carbon::parse($p->payment_date)->lessThanOrEqualTo(\Carbon\Carbon::now())) {
$totalPayment += $p->amount;
}
}
}
Inside of the foreach loop on $payment the code above uses Carbon to compare the dates. It looks like you're using Laravel's Eloquent so you should have Carbon available in your application since it's a Laravel dependency. I use Carbon to compare the dates because it knows more about dates than I do.
Next, if it's true that payment_date is less than or equal to the current date then it will add it to the $totalPayment for that admission. Otherwise, it will move onto the next payment. This presumably makes using continue or break unnecessary unless I am misunderstanding something.
I'm not sure what you want to do with $totalPayment. Right now it will be calculated for each admission and then lost.
If you want to find the total payment across all admissions you should move $totalPayment = 0 to the line above foreach($admission as $a).
If you want to find the total payment for each admission you could set a totalPayment on each admission $a->total_payment = $totalPayment. I would then change $totalPayment = 0 to $a->total_payment = 0 and change $totalPayment += $p->amount to $a->totalPayment += $p->amount.
i this problem that i am sure are very simple to some people but atm i just cant wrap my head around it.. here goes i want to plus all data outputs from a certain row with the code i have now it just outputs for example 12 2 12 14 but i want to get 40 instead of the above just to state a example here is my code
$searchTimeScale = mysql_query("SELECT * FROM workhours WHERE case_project_id='$myCaseId'");
while($timeFeed = mysql_fetch_assoc($searchTimeScale)){
$workedHours = $timeFeed['worked_hours'];
$workedMinutes = $timeFeed['worked_minutes'];
echo $workedHours;
var_dump($workedMinutes);
}
You should use aggregate function SUM() along with GROUP BY
SELECT SUM(your_hour_field)
FROM workhours
WHERE case_project_id='$myCaseId'
GROUP BY case_project_id
You don't technically need GROUP BY here since you are only querying for a single case_project_id, but I am showing it in case you ever wanted to SUM up across a full record set with aggregations on a specific field or fields.
You can aggregate in your SQL query by selecting SUM(worked_hours*60+worked_minuts). That gives you the total number of minutes.
Just keep a counter going:
$total = 0;
while($timefeed = mysql_fetch_assoc(...))) {
$total += ($timeFeed['worked_hours'] * 60) + $timeFeed['worked_minutes'];
}
I have a table that holds user data in a single row and it looks like this
What I am trying to do is list them in order of their priority so the outcome looks like this
Everything (because its priority is 1)
Something (Because its priority is 2)
Anything (Because its priority is 3)
At present to achieve this I am using else if statement and it is working fine for example
if (Data1_Priority == '1'){
echo Data1
}elseif{
.....
}
I was wondering if it is possible to display the data in order of their priority using some loop rather than if else
I will really appreciate any assistance on how to achieve this.
Do you want to unpivot them and sort them?
select d.*
from (select data1 as data, data1priority as priority from table union all
select data2 as data, data2priority as priority from table union all
select data3 as data, data3priority as priority from table
) d
order by priority;
As a note: having similar column names that only differ by a number is a sign of poor database design. It is usually better to have fewer columns and more rows.
As mentioned by others, you should be doing this in the database (probably with a redesign), but for a php solution, you can create a temporary array indexed by priority, sort on the key, then iterate it:
$row = [
'data1'=>'something',
'data1_priority'=>2,
'data2'=>'anything',
'data2_priority'=>3,
'data3'=>'everything',
'data3_priority'=>1,
];
$temp=[];
for($i=1; $i<(count($row)/2)+1; $i++){
$temp[$row['data'.$i.'_priority']]=$row['data'.$i];
}
ksort($temp);
foreach($temp as $element)
echo $element . '<br>';
Live example: http://codepad.viper-7.com/O3D54e
for ($pri = 1; $pri<=3; $pri++)
{
for($d == 1; $d<=3; $d++)
{
if ($r["Data".$d."_Priority"] == $pri)
{
echo $r["Data".$d];
}
}
}
Untested, but a point in the right direction?
Working in Zend framework and using Doctrine.
I have 1 competition, concisting of several matches ( could be from as little as 4 up to as much as 50 ).
These matches usually have the same players in them.
I have the tables:
Competitions
Matches
Matchresults
Users
First step, get all matches belong to competition, this is easy, so no worries there.
Now it gets tricky:
Step two, get all results of said matches, still ok, i can get the data, but presenting this data is the challenging part.
Step three, get the names from the users table, where the users id is equal to the users id given in the matchresults table.
The end result should be a table looking something like this.
Name Match1 match2 match3 total points
firstname + lastname 50 60 61 171
firstname + lastname 52 56 66 174
I can get all the data correctly, but getting it all in the right field is a bit of a problem.
Could anyone point me in the right direction or maybe give me an example?
Cheers,
Mark
EDIT: clarification.
This is for a fishing competition.
Each match has an x amount of fisherman and these get points according to what they catch.
The calender year has been divided into competitions, 2 or 4, can be different each year.
These matches have a begin-date and an end-date and all matches that have been played in between the begin date and end date of a competition, belong to that competition.
Now the user of the app, wants to make an excell export, showing all the results in the manner i've shown above, the more matches, the more columns with points in them.
You want to make yourself a function which transforms a competition into a list of users along with their results. Something like:
public function transform($competition)
{
$users = array();
$matches = $competition->getMatches();
$matchCount = count($matches);
$matchNum = 0;
foreach($matches as $match)
{
$matchNum++;
foreach($match->getResults() as $result)
{
$user = $result->getUser();
$userId = $user->getId();
// Add new user if necessary
if (!isset($users[$userId]))
{
// Store the name
$users[$userId]['name'] = $user->getFirstName() . ' ' . $user->getLastName();
// Spot to store total points
$users[$userId]['total'] = null;
// Individual match results
$users[$userId]['matches'] = array();
// Spots for each match in case users skip a match
for($i = 1; $i <= $matchCount; $i++) $users[$userId]['matches']['Match' . $i] = null;
}
$points = $result->getPoints();
$users[$userId]['matches']['Match' . $matchNum] = $points;
$users[$userId]['total'] += $points;
}
}
// Use usort if you want to sort by names
// Done
var_dump($users);
return $users;
}
I have a database with the following table:
id value
-----------
1 yes
2 no
3 no
4 maybe
I'm using some simple php to log the choices entered on a poll website. The user selects a radio box and it is entered into the above table. However, I want to make this a little more flexible. I created a simple backend that allowed an admin user to add or delete poll choices. What would I do to show on the frontend the number of votes for each individual choice, when the number of choices is not constant? I know I could do this easily if the poll choices were static but since the backend user will be changing the choices, how could I dynamically display the results?
I am not really sure what you are asking. Is it COUNT you're looking for?
Don't worry about the choices or the number of choices, grab all the votes/choices and iterate through them and add them to an array indiscriminately: http://codepad.org/LWPyuTqj
$total = array();
$votes = array(1=>'yes',2=>'no',3=>'no',4=>'maybe');
foreach($votes as $vote) {
if (!isset($total[$vote]))
$total[$vote] = 1;
else
$total[$vote] += 1;
}
print_r($total);
I would recommend google graph API for this. It is really easy!
http://code.google.com/apis/chart/interactive/docs/gallery/piechart.html
Generate the code dynamically, using the first example on the link above. First select the values. This assuming there is a question ID so you can relate to the question. In this case id 1.
$result = mysql_query('SELECT value,COUNT(*) as num FROM choises WHERE question_id = 1 GROUP BY value');
Then with PHP loop through the data
$results = array();
while ($row = mysql_fetch_assoc($result)){
$results[$row['value']] = $num;
}
With this you can generate the graph:
echo 'data.addRows('.count($results).');';
$i = 0;
foreach ($results as $value => $num){
echo'
data.setValue('.$i.', 0, "'.$value.'");
data.setValue('.$i.', 1, '.$num.');
';
$i++;
}