Reputation: 459
I have a query that gives me the number of occurrences using a count, see below:
$query = "SELECT COUNT(a.market_id) AS winners, a.winner,
a.twitter_pubstatus, a.market, a.racetime, a.racecourse, b.course, b.horse,
b.type, b.racetime FROM results a INNER JOIN bets b ON a.racecourse = b.course
WHERE a.twitter_pubstatus = 0 AND a.market = '$win' AND
b.type = '$userwin' AND a.winner = b.horse;";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$checkWinners = $row['winners'];
echo $checkWinners;
This for example will return 11. I now have an IF statement:
<?php if ($checkWinners > 1) { ?>
DO THIS
<? } else { ?>
DO THIS INSTEAD
<? } ?>
Now it runs through this once and that's fine. I want it to loop through so, 11 then goes through the first bit then need it will be 10 and go through until it hits 0 and then stop.
How to do this? While Loop? Help Appreciated
Upvotes: 0
Views: 140
Reputation: 8077
An even simpler way is to loop through normally, and have MySQL order them as you need, to ensure they are in the right order.
$query = "SELECT COUNT(a.market_id) AS winners, a.winner,
a.twitter_pubstatus, a.market, a.racetime, a.racecourse, b.course, b.horse,
b.type, b.racetime FROM results AS a INNER JOIN bets AS b ON a.racecourse = b.course
WHERE a.twitter_pubstatus = 0 AND a.market = '$win' AND
b.type = '$userwin' AND a.winner = b.horse
ORDER BY winners DESC;"; //<--- ORDER BY
$result = mysql_query($query) or die(mysql_error());
if( mysql_num_rows( $result ) ) { //Check if there's any results
while ( $row = mysql_fetch_array($result) ) { //Iterate results until empty
$checkWinners = $row['winners'];
//Tweet winners code here
echo $checkWinners . '<br/>';
}
//Update database to reflect the updated tweets
$query = "UPDATE results AS a INNER JOIN bets AS b ON a.racecourse = b.course
WHERE a.twitter_pubstatus = 0 AND a.market = '$win' AND
b.type = '$userwin' AND a.winner = b.horse
SET a.twitter_pubstatus = 1;" //Update all of them at once
mysql_query($query) or die(mysql_error()); //Send query, unless fail
} else {
//If no results from initial query
echo "No Winners! :(";
}
Also, you should consider using mysqli over mysql, as mysql extension for PHP has been deprecated. See: Why shouldn't I use mysql_* functions in PHP?.
Upvotes: 0
Reputation: 32517
Yes you can do this with a while loop. The basic logic is this:
$c = 10;
while ($c > 0) {
// do something
$c--;
}
But if I had to take a guess, I think maybe what you really want is this:
$query = "SELECT COUNT(a.market_id) AS winners, a.winner,
a.twitter_pubstatus, a.market, a.racetime, a.racecourse, b.course, b.horse,
b.type, b.racetime FROM results a INNER JOIN bets b ON a.racecourse = b.course
WHERE a.twitter_pubstatus = 0 AND a.market = '$win' AND
b.type = '$userwin' AND a.winner = b.horse;";
$result = mysql_query($query) or die(mysql_error());
while ( $row = mysql_fetch_array($result) ) {
$checkWinners = $row['winners'];
echo $checkWinners . '<br/>';
}
This loops through all the rows returned from the query.
Upvotes: 1