Hey I have a set of results returned from the database, they are numbers whi开发者_运维技巧ch represent 1 - 18 which are the drivers_id, it could return any number of those dependant on how many drivers retired from a race. I am then matching them up to the users drivers and if they match then to -10 points from the users points.
If they do not match then to +5 points. If the query returns no set at all then i know that the users four drivers can be +5 points each, but if it returns 5 rows, then how can I loop through those and match them up? and if there is no match add +5 points to that drivers variable, but it has to be after all the result set has been compared as that driver might be a match to the next row in $row. Thanks a lot for any help provided!.
p.s each driver_id in the result set is unique, so once a comparison is true then that driver_id will not appear again.
$results = mysql_query("SELECT drivers_id FROM results_retired WHERE drivers_id IN ('$driver1', '$driver2', '$driver3', '$driver4') AND track_id = '$track'") or die ("Failed to update" . mysql_error());
$retireddrivers = mysql_fetch_array($results)
if(in_array($driver1, $retireddrivers)){
$driveronepoints -= $driver_points_system["retired"];
}
else {
$driveronepoints += $driver_points_system["completion"];
}
if(in_array($driver2, $retireddrivers)) {
$drivertwopoints -= $driver_points_system["retired"];
}
else {
$drivertwopoints += $driver_points_system["completion"];
}
if(in_array($driver3, $retireddrivers)) {
$driverthreepoints -= $driver_points_system["retired"];
}
else {
$driverthreepoints += $driver_points_system["completion"];
}
if(in_array($driver4, $retireddrivers)) {
$driverfourpoints -= $driver_points_system["retired"];
}
else {
$driverfourpoints += $driver_points_system["completion"];
}
I have made some changes now to the code, is the in_array function ok to use in this instance??
thanks
It looks like you're missing the concept of rows vs columns.
mysql_fetch_*
will return you one row -- that is, one of the retired drivers' IDs -- with each call. You'll need to collect them into an array before you can use in_array
.
while ($retired_driver = mysql_fetch_array($results))
{
$retireddrivers[] = $retired_driver['driver_id'];
}
Also, if you can manage it, i'd consider putting drivers 1-4 into an array as well. Then you can do like
// Ideally, $driver[1-4]* won't exist at all -- you'd pull the info from the DB,
// or from the form, and stuff it directly into the array.
// This code right here is just to make existing code work
$drivers = array(
array('id' => $driver1, 'points' => &$driver1points),
array('id' => $driver2, 'points' => &$driver2points),
array('id' => $driver3, 'points' => &$driver3points),
array('id' => $driver4, 'points' => &$driver4points),
);
foreach ($drivers as $driver)
{
if (in_array($driver['id'], $retireddrivers))
{
$driver['points'] -= $driver_points_system['retired'];
}
else
{
$driver['points'] += $driver_points_system['completion'];
}
}
considerably reducing duplication of code.
精彩评论