Hi Guys,
I have a little problem which i know exactly why its happening but cant think of a way of getting round the problem, i have been learning PHP for just 3 days and i think im getting good at it and have nearly finished building my first PHP project (an Affiliate Network involving click tracking, lead tracking, cookies, subid tracking and more). I’m currently coding the Statistics page and below is some the code I’m using:
<?
$query = "SELECT
ProgramName AS 'name'
, COUNT(*) AS 'count'
, SUM(Payout) AS 'payout'
FROM
affleads
WHERE
AffID = '%s'
AND
Date = '$datenow'
AND
Status = 'Pending'
GROUP BY
ProgramName;
";
$query2 = "SELECT
ProgramName AS 'name2'
, COUNT(*) AS 'count2'
, SUM(Payout) AS 'payout2'
FROM
affleads
WHERE
AffID = '%s'
AND
Date = '$datenow'
AND
Status = 'Approved'
GROUP BY
ProgramName;
";
$result2 = mysql_query(sprintf(
$query2,
mysql_real_escape_string($affid)
));
while($row = mysql_fetch_assoc($result)){
?>
<tr class="campaignheader">
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row['name']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row['count']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row2['count2']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader">£</td>
<td bgcolor="#FFFFFF" class="campaignheader">£<? printf($row['payout']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader">£<? printf($row2['payout2']); ?></td>
</tr>
<?
}
mysql_close();
?>
Now here is the output from using the PHP While and the SQL from Query:
Now I’m trying to also add values into the table from Query2 and this is the code i have used:
$result = mysql_query(sprintf(
$query,
mysql_real_escape_string($affid)
));
$result2 = mysql_query(sprintf(
$query2,
mysql_real_escape_string($affid)
));
while($row2 = mysql_fetch_assoc($result2)){
while($row = mysql_fetch_assoc($result)){
<tr class="campaignheader">
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row['name']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row['count']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader"><? printf($row2['count2']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader">£</td>
<td bgcolor="#FFFFFF" class="campaignheader">£<? printf($row['payout']); ?></td>
<td bgcolor="#FFFFFF" class="campaignheader">£<? printf($row2['payout2']); ?></td>
</tr>
<?
}
}
mysql_close();
?>
and here is the output:
Now i bet your thinking, well whats the problem? The problem is i have 0 approved leads for Tesco Mobile in my database so there shouldnt be a value for Approved Leads for Tesco Mobile and there also shouldnt be a value for Approved £. Now what i think is happening here is because when the SQL is being ran there is no results for Tesco Mobile for Approved Leads so its using the data from Nufield Health because its on a while loop. Table picture below:
How would i go about doing what i need it to do and if there is no value then it will display ‘0’. Also the way i have done it i know is gonna cause a few problems so can someone help me out and code it in a different way please.
Any help would be great and thank you in advance.
Thanks!