2015-12-24 36 views
1

我有點難以搞清楚一個循環。。 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 ; 
+1

這看起來很凌亂。 – CodeGodie

+0

它肯定遠沒有完成... – indymx

+0

爲什麼你有第一個選擇查詢?它的目的是什麼?看起來你的第二個查詢也是一樣的 – CodeGodie

回答

1

今天早上我知道這個循環是我的問題,知道我接近我所需要的。稍微按摩我的主循環中的$偏移量,並按照我的需要工作。 ;)

再次感謝那些提供了幫助。以下是可用的代碼。

<?php 
session_start(); 
require_once "../db.class.php"; 
require_once "../functions.class.php"; 

$f = new functions(); 
$event_id = $_POST['id']; 
header("Content-Type:application/json; Charset=utf-8"); 


// -- Function Name : getLookup 
// -- Params : $value 
// -- Purpose : get count of riders and return distribution of riders and heats 
function getLookup($value) 
{ 
    global $db; 
    $stmt = $db->prepare("SELECT * FROM tbl_lookup WHERE rider_count = :value"); 
    $stmt->bindValue(':value', $value); 
    $stmt->execute(); 
    return $r = $stmt->fetchAll(PDO::FETCH_ASSOC); 
} 
// -- Function Name : getEntries 
// get entries for the event 
// -- Purpose : get entries to determine race orders 
function getEntries($class_id, $limit, $offset) 
{ 
    global $db; 
    global $event_id; 
    $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', $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']; 
} 
// -- Function Name : getEntriesPerClass 
// get count of entries per class 
// -- Purpose : get entries per class to feed the lookup function 
function getEntriesPerClass() 
{ 
    global $db; 
    $getentriesperclass = $db->prepare("SELECT tbl_event_entries.class_id, COUNT(tbl_event_entries.entry_id) AS riders, tbl_moto_order.moto_nbr 
           FROM tbl_event_entries 
           JOIN tbl_moto_order ON tbl_event_entries.event_id = tbl_moto_order.event_id AND tbl_event_entries.class_id = tbl_moto_order.class_id 
           WHERE tbl_event_entries.event_id = :event_id 
           GROUP BY tbl_event_entries.class_id 
           ORDER BY moto_nbr;"); 
    $getentriesperclass->bindValue(':event_id', $_POST['id']); 
    $getentriesperclass->execute(); 
    $counts = array(); 
    while ($r = $getentriesperclass->fetch(PDO::FETCH_ASSOC)) $counts[$r['class_id']] = $r['riders']; 
    return $counts; 
} 

$counts = getEntriesPerClass(); 
$race = 1; //set to add value to race_nbr field, increments 1 time for each total heat race 

foreach ($counts as $class => $rider_count) 
{ 
    $list = getLookup($rider_count); // lookup to get heat race counts and line ups for each heat. 
    // update event classes table with counts so we don't have to loop like crazy to build the printouts 
    $assign_class_counts = $db->prepare("UPDATE tbl_event_classes SET nbr_of_heats = :nbr_of_heats, nbr_of_riders_in_heats = :nbr_of_riders_in_heats WHERE event_id = :event_id AND class_id = :class_id"); 
    $assign_class_counts->bindValue(':nbr_of_heats', $list[0]['heat_count']); 
    $assign_class_counts->bindValue(':nbr_of_riders_in_heats', $list[0]['rider_count']); 
    $assign_class_counts->bindValue(':event_id', $_POST['id']); 
    $assign_class_counts->bindValue(':class_id', $class); 
    $assign_class_counts->execute(); 

    $offset = 0; // set offset to feet the main query to give us the position in each heat for a rider. Used in the getEntries query. 
    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++) 
      { 
       $assignheats = $db->prepare("UPDATE tbl_event_entries SET heat_nbr = :heat_nbr, heat_position = :heat_position, race_nbr = :race_nbr WHERE entry_id = :entry_id"); 
       $assignheats->bindValue(':heat_nbr', $i); 
       $assignheats->bindValue(':heat_position', $rih); 
       $assignheats->bindValue(':race_nbr', $race); 
       $assignheats->bindValue(':entry_id', getEntries($class, $list[0]['riders_in_heat_'.$i], $offset)); 
       $assignheats->execute(); 
       $offset++; 
      } 
      $race++; 
     } 
    } 
} 

echo json_encode(array('status' => true)); 
1

盡我所能去清理東西,可能不完美,但應該是正確方向的推動。

移動lookup類數據庫表

在我看來,像你這樣要好得多這裏供應的lookup類的意圖移動到數據庫表。試想一下,這樣的事情:

CREATE TABLE IF NOT EXISTS `tbl_lookup` (
`lookup_id` int(11) NOT NULL AUTO_INCREMENT, 
`rider_count` int NOT NULL, 
`heat_count` int NOT NULL, 
`riders_in_heat_1` int NOT NULL, 
`riders_in_heat_2` int, 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

然後從lookup類插入您的所有數據。

使用JOIN的& GROUP BY在你的第一個SQL查詢

您需要確定在給定的事件和類車手的數量。您可以加入tbl_event_classestbl_event_entries,然後GROUP BY tbl_event_entries.event_id以獲得這些計數。

SELECT tec.class_id, tec.event_id, COUNT(tee.event_id) AS entries_per_class 
FROM tbl_event_classes tec 
JOIN tbl_event_entries tee ON tee.event_id = tec.event_id 
WHERE tec.event_id = :event_id 
GROUP BY tee.event_id 

清理PHP

現在你的PHP代碼應該是有點更容易跟蹤。一個主要的查詢,以獲得事件和類與每個類每個事件車手數量。然後,當你循環結果集時,確定每個熱量的騎手人數。

這是一個有點粗糙,但我敢肯定,你可以得到它從這裏磨合了。

function getEntriesPerClass($event_id) { 
    global $db; 

    $stmt = $db->prepare(
     'SELECT tec.class_id, tec.event_id, COUNT(tee.event_id) AS entries_per_class ' . 
     'FROM tbl_event_classes tec ' . 
     'JOIN tbl_event_entries tee ON tee.event_id = tec.event_id ' . 
     'WHERE tec.event_id = :event_id ' . 
     'GROUP BY tee.event_id'); 

    $stmt->bindValue(':event_id', $event_id); 
    $stmt->execute(); 

    return $stmt->fetchAll(PDO::FETCH_ASSOC); 
} 

function getRidersInHeats($class_id, $event_id, $riders_per_class) 
{ 
    global $db; 

    $stmt = $db->prepare(
     'SELECT tl.riders_in_heat_1, tl.riders_in_heat_2 ' . 
     'FROM tbl_lookup ' . 
     'WHERE class_id = :class_id AND event_id = :event_id AND rider_count = :entries'); 

    $stmt->bindValue(':class_id', $class_id); 
    $stmt->bindValue(':event_id', $event_id); 
    $stmt->bindValue(':rider_count', $riders_per_class); 

    $stmt->execute(); 

    return $stmt->fetchAll(PDO::FETCH_ASSOC); 
} 

$entriesPerClass = getEntriesPerClass($_GET['event_id']); 
foreach($entriesPerClass as $entry) { 
    $riders = getRidersInHeats($entry['class_id'], $entry['event_id'], $entry['entries_per_class']); 

    echo 
     "class  : " . $row['class_id']  . "; " . 
     "ridercount: " . $riders['rider_count'] . "; " . 
     "heats  : " . $riders['heat_count'] . "<br/>"; 

    echo "Heats, consisting of :<br>\n<ul>"; 
    echo "<li>Heat 1: " . $riders['riders_in_heat_1'] . "</li>"; 

    $ridersInHeat2 = $riders['riders_in_heat_2']; 
    if($ridersInHeat2 > 0) { 
     echo "<li>Heat 2: " . $riders['riders_in_heat_2'] . "</li>"; 
    } 

    echo "</ul>"; 
} 
+0

我欣賞的努力,但這裏有這麼多的錯誤,我不能作出任何它的正面或反面。 – indymx

+0

嗨,這個代碼很簡單; 2個查詢和一個循環...把它分解成幾塊。首先獲取該連接查詢,然後使用mysql客戶端運行它,看看它是如何工作的,然後將其移動到php中。之後,獲取查找表和相應的功能。之後的循環應該很容易。如果你想聊天,我明天就會到。 – quickshiftin

+0

它的工作現在..見上面我張貼終於做出了該死的東西的工作代碼..這是所有關於我怎麼想通的偏移和在那裏我增加了。 – indymx