2015-05-28 33 views
19

以我數據庫我有3個表:顯示值

train_information:

+----------+-----------------+------------------+ 
| train_id | number_of_axles | number_of_bogies | 
+----------+-----------------+------------------+ 
|  1 |    4 |    2 | 
+----------+-----------------+------------------+ 

橋:

+---------+----------+------+----------+ 
| axle_id | train_id | axle | distance | 
+---------+----------+------+----------+ 
|  1 |  1 | 1 |  2500 | 
|  2 |  1 | 2 |  5000 | 
|  3 |  1 | 3 |  2500 | 
+---------+----------+------+----------+ 

轉向架:

+----------+----------+---------+----------+ 
| bogie_id | train_id | axle_nr | bogie_nr | 
+----------+----------+---------+----------+ 
|  1 |  1 |  1 |  1 | 
|  2 |  1 |  2 |  1 | 
|  3 |  1 |  3 |  2 | 
|  4 |  1 |  4 |  2 | 
+----------+----------+---------+----------+ 

當在train_information表中插入內容時,觸發器也會插入其他2個表(距離& bogie_nr稍後得到更新,但在本例中所有內容都已填充)。

現在我根據distance & axle的值創建一個火車模型。 現在它看起來是這樣的:

<div id="axles"> 
        <!--This is the last (useless) axle, wich always is 0--> 
        <div id="useless_circle"></div> 
        <!--Here we create the axles and style them with the distances--> 
        <?php 
         $show_axle = $database->axles($_GET['train_id']); 
         $total_distance = 0; 
         foreach($show_axle as $number_ofaxles){ 
          $total_distance += $number_ofaxles['distance']; ?> 
          <div id="axle" name="test" style="margin-left:<?= $total_distance/25000*100;?>%"> 
           <?= "<div id='circle'>" . $number_ofaxles['axle'] . "</div>";?> 
          </div> 
        <?php } ?> 
       </div> 

和:

function axles($id){ 
     $sql = "SELECT * FROM axle WHERE train_id = :id2"; 
     $sth = $this->pdo->prepare($sql); 
     $sth->bindParam(":id2", $id, PDO::PARAM_STR); 
     $sth->execute(); 
     return $sth->fetchAll(); 
    } 

現在,頁面看起來像這樣(與DB值):

How the page looks.

我提供的代碼僅用於車軸! (火車下面的四個圓圈)!現在

,我想要什麼:

現在,我只問了橋臺的價值。但它只包含3個軸而不是4個。這是因爲我想知道每個軸之間的距離。所以我總是需要少一點。
我解決了這個問題,通過創建1個額外的div來創建圓(軸),並將位置放在左邊。

我想要的是這樣的: 顯示axle_nrbogie表(因此它顯示4)。 獲取distance其中axle = axle_nr
然後你總是保持1空。因爲軸4不存在於axle表中。 所以我想做一個檢查:如果軸不存在,那麼距離= 0。我不想在數據庫中插入這個,但只是這樣我就不再需要無用的軸div了,軸保持在剩下。

爲什麼我要這個?
這種方式我可以檢查極小的轉向架數字是相同的,所以我可以給他們每個其他顏色等我也不需要useless_axle div!

編輯:

簡單的解釋:

我想表明從bogieAxle_nr。(所以它顯示4個圓圈) 但是!我將需要axle表中的Distance以製作火車圖。
您可以看到axle表的軸比表bogie表少了1個軸。
所以我希望「不存在」軸的值爲0.我希望它是0,因爲它會在列車的開始時出現。 (就像沒用橋現在)

CODE編輯:

現在我有這樣的:

  <div id="axles"> 
       <?php 
       $testingggg = $database->axleees(); 
       foreach ($testingggg as $lol){ ?> 
        <div id="axle"> 
          <div id="circle" name="<?= $lol['axle'] ?>"><?= $lol['axle'] ?></div> 
        </div> 
       <?php } ?> 
      </div> 

和:

function axleees() { 
     $sql = "SELECT ti.axle_nr, ti.train_id, ti.bogie_nr, uti.axle_id, uti.train_id, uti.axle, uti.distance 
       FROM bogie as ti 
       JOIN axle as uti 
       ON ti.train_id = uti.train_id 
       WHERE ti.train_id = :train_id"; 
     $sth = $this->pdo->prepare($sql); 
     $sth->bindParam(":train_id", $_GET["train_id"], PDO::PARAM_INT); 
     $sth->execute(); 
     return $sth->fetchAll(); 
    } 

而且它讓我看到12車軸而不是4!

編輯:

它顯示了我4軸現在至極是正確的。 但是我也需要正確的距離。代碼我有:

<div id="axles"> 
    <?php 
     $total_distance = 0; 
     foreach ($testingggg as $lol){ 
      $total_distance += $lol['distance']; 
    ?> 
      <div id="axle" style="margin-left:<?= $total_distance/25000*100;?>%"> 
        <div id="circle" name="<?= $lol['axle'] ?>"><?= $lol['axle_nr'] ?></div> 
      </div> 
     <?php } ?> 
    </div> 

現在,它表明我的每個軸有10%的餘量。這是正確的(如果你只有第一軸)。它需要像10-15-10-15左右的東西。我該怎麼做呢?

編輯:

現在我有如下因素查詢:

function axleees() { 
     $sql = "SELECT ti.axle_nr, ti.train_id, ti.bogie_nr, uti.axle_id, uti.train_id, uti.axle, uti.distance 
        FROM bogie as ti 
        JOIN axle as uti 
        ON ti.train_id = uti.train_id 
        WHERE ti.train_id = :train_id 
        GROUP BY uti.axle_id"; 
     $sth = $this->pdo->prepare($sql); 
     $sth->bindParam(":train_id", $_GET["train_id"], PDO::PARAM_INT); 
     $sth->execute(); 
     return $sth->fetchAll(); 
    } 

恩我在這裏把它稱爲:

<div id="axles"> 
      <?php 
       $total_distance = 0; 
       foreach ($testingggg as $lol){ 
       $total_distance += $lol['distance']; 
       $margin = $total_distance/25000*100; 
      ?> 
      <div id="axle" style="margin-left:<?= $margin; ?>%"> 
        <div id="circle" name="<?= $lol['axle'] ?>"><?= $lol['axle_nr'] ?></div> 
      </div> 
     <?php } ?> 
      </div> 

圖片編輯:

train example

+2

我還沒有得到你的問題。我會建議簡單解釋一下。所以,人們可以幫助你。 – Ironic

+0

編輯@ 404 – Mitch

回答

4

在我看來,這個是解決原始問題的一種相當複雜的方式。你很害羞一個軸,並且需要該軸在你的數據庫中。你說所有的值都是通過數據庫中的觸發器添加的。如果是這種情況,爲什麼不添加一個距離爲'0'的值與火車的id。這不僅會給你軸,還會給你渲染的div。

如果您的桌子看起來像這樣一代人(請原諒我,如果索引關閉了錯誤的方向。我奮力剛一接觸,瞭解你的數據庫佈局):

+---------+----------+------+----------+ 
| axle_id | train_id | axle | distance | 
+---------+----------+------+----------+ 
|  0 |  1 | 0 |  0 | 
|  1 |  1 | 1 |  2500 | 
|  2 |  1 | 2 |  5000 | 
|  3 |  1 | 3 |  2500 | 
+---------+----------+------+----------+ 

那麼下面生成對子級各界,包括「0」,有一個保證金的一個(或距離你如前所述)。從技術上講,你在火車前面有一個距離爲'0'的車軸,爲什麼不在你的數據庫中跟蹤它。

<div id="axles"> 
    <!--Here we create the axles and style them with the distances--> 
    <?php 
    $show_axle = $database->axles($_GET['train_id']); 
    $total_distance = 0; 
    foreach($show_axle as $number_ofaxles){ 
     // Because the first value is 0, the first run will be against the left edge. 
     $total_distance += $number_ofaxles['distance']; ?> 
     <div id="axle" name="test" style="margin-left:<?=$total_distance/25000*100;?>%"> 
      <?= "<div id='circle'>" . $number_ofaxles['axle'] . "</div>";?> 
     </div> 
    <?php } ?> 
</div> 

採取這種方法既簡化和解決您的問題。

+0

是的,儘管PO聲明他不想將軸存儲在數據庫中,但我也認爲您的答案是繼續進行的方式。如果火車有4根車軸,則在那裏放4行,並簡化計算! XD – Chococroc

3

變化

$sql = "SELECT ti.axle_nr, ti.train_id, ti.bogie_nr, uti.axle_id, uti.train_id, uti.axle, uti.distance 
      FROM bogie as ti 
      JOIN axle as uti 
      ON ti.train_id = uti.train_id 
      WHERE ti.train_id = :train_id"; 

$sql = "SELECT ti.axle_nr, ti.train_id, ti.bogie_nr, uti.axle_id, uti.train_id, uti.axle, uti.distance 
        FROM bogie as ti 
        LEFT JOIN axle as uti 
        ON ti.train_id = uti.train_id AND uti.axle_id = ti.axle_nr 
        WHERE ti.train_id = :train_id"; 

或運行測試下一個SQL:

SELECT 
     b.*, 
     a.* 
FROM bogie AS b 
LEFT JOIN axle AS a ON a.train_id = b.train_id AND a.axle_id = b.axle_nr 
WHERE b.train_id = 1 

返回4行,而不是12

+0

這幫了我,艱難得到了1個小問題哈哈查看編輯@Danila Ganchar – Mitch

+0

示例已經更新。檢查答案。 –

+0

我其實不想加5,因爲你可以看到我的軸表有距離。我從這些中得出一筆款項。所以2500/25000 * 100 = 10,所以第一根軸的餘量爲10%。我需要和其他人一起做這件事。現在的問題是:它只對第一根軸進行。所以結果是10%,10%,10%,10%。而實際上應該是10%,20%,10%因爲我只有3根車軸。 – Mitch