我有點難以搞清楚一個循環。。 mysql&php
他們並不是我的強項。 ;)
它採用了類 「查找」 查找表看起來像這樣:(留下了很多行爲簡潔起見)
class lookup {
protected $lookup = array(
array('rider_count' => '1', 'heat_count' => '1', 'riders_in_heat_1' => '1'),
array('rider_count' => '2', 'heat_count' => '1', 'riders_in_heat_1' => '2'),
array('rider_count' => '3', 'heat_count' => '1', 'riders_in_heat_1' => '3'),
array('rider_count' => '4', 'heat_count' => '1', 'riders_in_heat_1' => '4'),
array('rider_count' => '5', 'heat_count' => '1', 'riders_in_heat_1' => '5'),
array('rider_count' => '6', 'heat_count' => '1', 'riders_in_heat_1' => '6'),
array('rider_count' => '7', 'heat_count' => '1', 'riders_in_heat_1' => '7'),
array('rider_count' => '8', 'heat_count' => '2', 'riders_in_heat_1' => '4', 'riders_in_heat_2' => '4'),
array('rider_count' => '9', 'heat_count' => '2', 'riders_in_heat_1' => '5', 'riders_in_heat_2' => '4'),
array('rider_count' => '10', 'heat_count' => '2', 'riders_in_heat_1' => '5', 'riders_in_heat_2' => '5'),
array('rider_count' => '11', 'heat_count' => '2', 'riders_in_heat_1' => '6', 'riders_in_heat_2' => '5'),
array('rider_count' => '12', 'heat_count' => '2', 'riders_in_heat_1' => '6', 'riders_in_heat_2' => '6'),
array('rider_count' => '13', 'heat_count' => '2', 'riders_in_heat_1' => '7', 'riders_in_heat_2' => '6'),
array('rider_count' => '14', 'heat_count' => '2', 'riders_in_heat_1' => '7', 'riders_in_heat_2' => '7')
);
public function select ($field, $value)
{
$list = array();
foreach ($this->lookup as $count)
{
if ($count[$field] == $value)
{
$list[] = $count;
}
}
return $list;
}
}
$classes = new lookup();
我的PHP的:
<?php
// get entries for the event
function getEntries($class_id, $limit, $offset)
{
global $db;
$getentries = $db->prepare("SELECT entry_id FROM tbl_event_entries WHERE event_id = :event_id AND class_id = :class_id LIMIT :offset, :limit");
$getentries->bindValue(':event_id', $_GET['event_id']);
$getentries->bindValue(':class_id', $class_id);
$getentries->bindValue(':limit', $limit);
$getentries->bindValue(':offset', $offset);
$getentries->execute();
while ($r = $getentries->fetch(PDO::FETCH_ASSOC)) return $r['entry_id'];
}
// get count of entries per class
// get classes for the event
$geteventclasses = $db->prepare("SELECT class_id FROM tbl_event_classes WHERE event_id = :event_id");
$geteventclasses->bindValue(':event_id', $_GET['event_id']);
$geteventclasses->execute();
while ($r = $geteventclasses->fetch(PDO::FETCH_ASSOC))
{
$getentriesperclass = $db->prepare("SELECT entry_id FROM tbl_event_entries WHERE class_id = :class_id AND event_id = :event_id");
$getentriesperclass->bindValue(':class_id', $r['class_id']);
$getentriesperclass->bindValue(':event_id', $_GET['event_id']);
$getentriesperclass->execute();
$r2count = $getentriesperclass->rowCount();
$counts[$r['class_id']] = $r2count;
}
foreach ($counts as $class => $rider_count)
{
$list = $classes->select('rider_count', $rider_count);
echo "class: ". $class ."; ridercount: " . $list[0]['rider_count'] ."; heats: ". $list[0]['heat_count'] ." heats, consisting of :<br>\n";
for ($i = 1; $i <= $list[0]['heat_count']; $i++)
{
if ($list[0]['heat_count'] > 0)
{
for ($rih = 1; $rih <= $list[0]['riders_in_heat_'.$i]; $rih++)
{
$offset = 1;
echo "<li>Heat ". $i ." : ". getEntries($class, $list[0]['riders_in_heat_'.$i], $offset) ." </li>";
}
$offset = $offset + $list[0]['riders_in_heat_'.$i];
}
}
echo "</ul>";
}
?>
這最終會建立一個更新查詢,爲每個entry_id分配「heat_nbr」和「heat_position」。
我們的任務是從class_id中獲取rider_count並將其分解,以便每場比賽最多隻有7名車手,並且均勻分配每名車手的熱量。
查找是我們如何確定分佈如何發生。這部分看起來很完美。我只是堅持如何讓每個騎手分配到一個位置。
我已經嘗試了幾種不同的方法,這和我得到的答案一樣。
在正確的方向微調將不勝感激!
見我至今這裏的輸出:
http://home.garyeterry.com/midam/createheats.php?event_id=113
感謝
表結構:
CREATE TABLE IF NOT EXISTS `tbl_event_entries` (
`entry_id` int(11) NOT NULL AUTO_INCREMENT,
`event_id` int(1) DEFAULT NULL,
`racer_id` int(4) DEFAULT NULL,
`class_id` int(1) DEFAULT NULL,
`racing_nbr` varchar(4) DEFAULT NULL,
`machine_cc` int(2) DEFAULT NULL,
`brand_id` int(1) DEFAULT NULL,
`overall_finish` int(1) DEFAULT NULL,
`xtra_int1` varchar(10) DEFAULT NULL,
`heat_nbr` int(1) DEFAULT NULL,
`heat_position` int(1) DEFAULT NULL,
`heat_row` int(1) DEFAULT NULL,
`heat_finish` int(1) DEFAULT NULL,
PRIMARY KEY (`entry_id`),
UNIQUE KEY `entry_id` (`entry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=165 ;
這看起來很凌亂。 – CodeGodie
它肯定遠沒有完成... – indymx
爲什麼你有第一個選擇查詢?它的目的是什麼?看起來你的第二個查詢也是一樣的 – CodeGodie