I have a 4 table merge going on and in the end I want it to be sorted by an "ORDER BY" according to some variable. Right now this is always returning the same order of entries.
Something like:
if(isset($_GET['filter'])){
$filter = $_GET['filter'];
#Example $filter = 'date' or team or game_num
$q = $db->prepare("SELECT g.game_num, s.date, t.team
FROM schedule n
LEFT JOIN g_lkp g
ON n.game_num = g.game_num
LEFT JOIN dates s
ON n.date = s.date
LEFT JOIN teams t
ON n.home_team_nbr = t.team
ORDER BY '$filter' ");
$q->execute();
$qR = $q->fetchAll(PDO::FETCH_ASSOC);
if ($q->rowCount() > 0) {
foreach ($qR as $row) {
echo '
//First check value of $filter
$q = $db->prepare("SELECT g.game_num game_num, s.date date, t.team team
FROM schedule n
LEFT JOIN g_lkp g
ON n.game_num = g.game_num
LEFT JOIN dates s
ON n.date = s.date
LEFT JOIN teams t
ON n.home_team_nbr = t.team
ORDER BY $filter ");
Related
I am wondering if it is possible to compare dates in all $row's from a while loop to today's date. Let me explain.
I have created this table shown in this image:
I have created this table through joining several tables, and actually have a while loop inside a while loop (which is how I got multiple games to show on one row, but only one wager amount per row.
What I want to do is compare the dates of each of the individual games to today's date, to list if the parlay is active or not (in this image, they would all not be active, but that's not the point).
I want to take today's date and if it is less than all the dates of the games, then Active == 'Future', or if today's date is in between a set of dates, then Active == 'In Progress', and if today's date is past all the dates, then Active == 'Past'. One part that makes this challenging is that I do not know how many individual games there will be, so I'm thinking something like this may work:
/* fetch row */
$firstrow = $result2->fetch_row();
$date1=date_create("$firstrow[0]");
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date1,$date2);
But I don't know how many rows to make to test my last date, perhaps something with a
$row_count = $result->num_rows;
$datelast=date_create("$lastrow[$row-count-1]");
Not sure how to then implement and use this if it is the correct logic!
So does anyone know how I can compare each of these date rows to today's current date?
This is my initial select query:
I've omitted some info from my image, to try to make the core logic of the problem easier, that's why there are some extra fields here
//Create parlay select query
$query = "SELECT
u.first_name AS 'User First Name',
u.last_name AS 'User Last Name',
b.betting_site_name AS 'Betting Site',
p.id AS 'Parlay ID',
p.wager AS 'Wager',
p.odds AS 'Odds',
p.success AS 'Success',
DATE_FORMAT(p.creationdate, '%d-%m-%Y') AS 'Date',
pg.parlayid AS 'PG Parlay ID',
SUM(p.wager * p.odds) AS Winnings
FROM parlays p
JOIN parlaygames pg ON pg.parlayid = p.id
JOIN bonuses b ON p.bettingsiteid = b.id
JOIN users u ON p.userid = u.id
WHERE userid=$id
GROUP BY p.id
ORDER BY p.id DESC
LIMIT 5";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
This is my table creation code (and second while loop to get the individual games):
<h2>Betting History</h2>
<table class="table table-striped userwinnings">
<tr>
<th>Parlay Information</th>
<th>Wager</th>
<th>Active</th>
</tr>
<?php
while($row = $result->fetch_assoc()) {
$output = '<tr>';
$output .= '<td><table><tr><th>Date</th><th>Game</th><th>Bet Info</th></tr>';
$parlayid = $row['Parlay ID'];
$query3 = "SELECT
pg.parlayid AS ParlayID,
g.date AS GameDate,
ht.name AS HomeTeam,
away.name AS AwayTeam,
pg.betinfo AS BetInfo
FROM parlaygames pg
JOIN parlays p ON pg.parlayid = p.id
JOIN games g ON pg.gameid = g.id
JOIN teams ht ON g.home_team = ht.id
JOIN teams away ON g.away_team = away.id
JOIN users u ON u.id = p.userid
WHERE p.id = $parlayid";
$result3 = $mysqli->query($query3) or die($mysqli->error.__LINE__);
while($row3 = $result3->fetch_assoc()) {
$gamescount = $result3->num_rows;
$output .= '<tr>';
$output .= '<td>'.$row3['GameDate'].'</td>';
$output .= '<td>'.$row3['HomeTeam'].' vs '.$row3['AwayTeam'].'</td>';
$output .= '<td>'.$row3['BetInfo'].'</td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= '</td>';
$output .= '<td>'.$row['Wager'].'</td>';
$output .= '<td>'.$row['Active'].'</td>';
$output .= '</tr>';
echo $output;
}
?>
</table>
Hi please replace these query
$query3 = "SELECT
pg.parlayid AS ParlayID,
g.date AS GameDate,
ht.name AS HomeTeam,
away.name AS AwayTeam,
pg.betinfo AS BetInfo
FROM parlaygames pg
JOIN parlays p ON pg.parlayid = p.id
JOIN games g ON pg.gameid = g.id
JOIN teams ht ON g.home_team = ht.id
JOIN teams away ON g.away_team = away.id
JOIN users u ON u.id = p.userid
WHERE p.id = $parlayid";
With these one
$query3 = "SELECT
pg.parlayid AS ParlayID,
g.date AS GameDate,
IF((NOW() between MIN(g.date) AND MAX(g.date)),'In Progress',IF((MAX(g.date) > NOW()),'Future','Past')) as Acctive,
ht.name AS HomeTeam,
away.name AS AwayTeam,
pg.betinfo AS BetInfo
FROM parlaygames pg
JOIN parlays p ON pg.parlayid = p.id
JOIN games g ON pg.gameid = g.id
JOIN teams ht ON g.home_team = ht.id
JOIN teams away ON g.away_team = away.id
JOIN users u ON u.id = p.userid
WHERE p.id = $parlayid group by pg.parlayid";
This is how I ended up solving this issue:
while($row3 = $result3->fetch_assoc()) {
$gamescount = $result3->num_rows;
/* seek to row no. 1 */
$result4->data_seek(0);
/* fetch row */
$firstrow = $result4->fetch_row();
$result4->data_seek($gamescount-1);
$lastrow = $result4->fetch_row();
$date1=date_create("$firstrow[1]");
$date3=date_create("$lastrow[1]");
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date3,$date2);
$firstdate = $date3->format('Y-m-d');
$lastdate = $date1->format('Y-m-d');
$today = $date2->format('Y-m-d');
if ($firstdate < $today && $today < $lastdate) {
$active = "In Progress";
} else if ($today > $lastdate) {
$active = "Past";
} else if ($today < $firstdate) {
$active = "Future";
}
I hope this logic can help others as well.
For some reason the member id field(auto inc.) in my huge query is returning null.I've tried every which way of selecting it... m.member_id AS member_id, etc.I cannot figure out why it is returning null when there is a value for that field in the table.
<?php
public function get_info($criteria = 0){
if(is_numeric($criteria)){
$where = "WHERE m.member_id = ".$criteria;
} else {
$where = "WHERE email_address = '".$criteria."'";
}
$query_member = "
SELECT
m.member_id AS member_id, m.display_name, m.email_address, m.group_id, m.status, m.activation_code, UNIX_TIMESTAMP(m.date_joined) AS date_joined,
m.gender, m.location, m.biography, m.mantra, m.birth_date, m.results_per_page, m.admin_emails, m.member_emails, m.last_active, m.avatar_id,
m.banner_id, m.signature, m.newsletter_subscription, m.recruiting_status, m.facebook_username, m.website, m.steam_username, m.xboxlive_gamertag, m.psn_id,
g.group_id, g.title, g.description,
a.attachment_id, a.file_name,
f.message_id, f.author_id, COUNT(f.message_id) AS forum_count,
b.attachment_id AS banner_id, b.file_name AS banner_file,
mr.request_id, mr.author_id, mr.recipient_id, mr.status, COUNT(mr.request_id) AS total_friends,
tm.team_member_id, tm.member_id, tm.team_id
FROM members AS m
LEFT JOIN member_groups AS g ON (m.group_id = g.group_id)
LEFT JOIN attachments AS a ON (m.avatar_id = a.attachment_id)
LEFT JOIN forum_messages AS f ON (m.member_id = f.author_id)
LEFT JOIN attachments AS b ON (m.banner_id = b.attachment_id)
LEFT JOIN member_requests AS mr ON (m.member_id = mr.author_id OR m.member_id = mr.recipient_id) AND mr.status = 1
LEFT JOIN team_members AS tm ON (m.member_id = tm.member_id) AND date_left = ''
".$where."
GROUP BY m.member_id
LIMIT 1";
//show_error($query_member);
if($query_member = $this->db->query($query_member)){
if($query_member->num_rows() > 0){
var_dump($query_member->row_array());
Because you select two fields with the same name. So MySQL will return result of last one. Add aliases:
SELECT m.member_id AS member_id_1, tm.member_id AS member_id_2 ...
Wondering if this is possible or not. In the statement below I am getting counts from different tables. This prints our an array of totals for each user_id.
Is there any way to combine these totals so it returns a single array with the totals? I am using subqueries as joins were taking a hit performance-wise so joining is not an option.
$stmt = $db->prepare("
SELECT
(SELECT COUNT(*) FROM log1 WHERE log1.user_id = users.user_id AS l1,
(SELECT COUNT(*) FROM log2 WHERE log2.user_id = users.user_id AS l2,
(SELECT COUNT(*) FROM log3 WHERE log3.user_id = users.user_id AS l3,
(SELECT COUNT(*) FROM log4 WHERE log4.user_id = users.user_id AS l4
FROM computers
INNER JOIN users
ON users.computer_id = computers.computer_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
");
$binding = array(
'cw_account_id' => $_SESSION['user']['account_id'],
'cw_status' => 1
);
$stmt->execute($binding);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
At the moment I am doing something like this with the return to get the result I want:
foreach($result as $key)
{
$new['l1'] = $new['l1'] + $key['l1'];
$new['l2'] = $new['l2'] + $key['l2'];
$new['l3'] = $new['l3'] + $key['l3'];
$new['l4'] = $new['l4'] + $key['l4'];
}
return $new;
Use SUM aggregate function:
$stmt = $db->prepare("
SELECT
SUM((SELECT COUNT(*) FROM log1 WHERE log1.user_id = users.user_id)) AS l1,
SUM((SELECT COUNT(*) FROM log2 WHERE log2.user_id = users.user_id)) AS l2,
SUM((SELECT COUNT(*) FROM log3 WHERE log3.user_id = users.user_id)) AS l3,
SUM((SELECT COUNT(*) FROM log4 WHERE log4.user_id = users.user_id)) AS l4
FROM computers
INNER JOIN users
ON users.computer_id = computers.computer_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
");
This query returns one row with total counts and your required result:
$new = $result[0];
Here is the code I'm currently using:
$result = mysql_query("
SELECT SUM(s.amount)
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC;
");
$data = mysql_fetch_array($result);
$pendingcommissions = $data['?????????'];
$this->assign("pendingamount", $pendingcommissions);
What I'm not sure about is what to enter for ????????? on the third line. I've tried all of these things and none of them have worked:
$pendingcommissions = $data['SUM(tblaffiliatespending.amount)'];
$pendingcommissions = $data['SUM'];
$pendingcommissions = $data['tblaffiliatespending.amount'];
$pendingcommissions = $data['tblaffiliatespending'];
$pendingcommissions = $data['amount'];
Any ideas on what this needs to be changed to?
You need to give the alias for the sum of amount SUM(s.amount) total_amountso when you execute query and fetch the results from it you will have the sum of your amount column on total_amount index in resultant array and you can access it by $data['total_amount'];, also note using aggregate functions without grouping then will result in a single row not per group
SELECT SUM(s.amount) total_amount
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC
so i have 5 tables in which it is interconnected with foreign keys
and here is a sample output of table 3
what i wanted to do in table number 3 is to extract the SubdeptID of user with userid of 10 but in this case it has 2 userid10 so its print both. what i want to print is only the one with latter TransferID. my select statement is this
$sql_exp = "SELECT a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC LIMIT 1";
my php code is
$rs = $conn->Execute($sql_exp);
if ($rs->EOF) {
echo "<tr><td>Please check for the Employee Name or the Department</td>";
} else {
while (!$rs->EOF){
echo "<tr><td>".$rs->Fields("Department")." / ".$rs->Fields("EmployeeName")."</td>";
$rs->movenext();
}
$rs->Close();
}
im having an error in "LIMIT" query.
MSSQL don't have LIMIT keyword.
Use TOP instead LIMIT.
$sql_exp = "SELECT TOP 1 a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC";