【发布时间】:2016-03-31 03:40:23
【问题描述】:
我很难找出一个循环。
它们并不是我真正的强项。 ;)
它使用“lookup”类作为查找表,如下所示:(为简洁起见,省略了很多行)
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>";
}
?>
这最终将构建一个更新查询,将“heat_nbr”和“heat_position”分配给每个entry_id。
任务是从 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 ;
【问题讨论】:
-
这看起来很乱。
-
它肯定远未完成......
-
为什么会有第一个 Select 查询?它的目的是什么?看来您的第二个查询也是如此
-
你能提供你的SQL表的结构吗?
-
第一个是获取entry_id的函数。这绝对没有按预期工作。它根本没有做 second 的工作。