以我數據庫我有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值):
我提供的代碼僅用於車軸! (火車下面的四個圓圈)!現在
,我想要什麼:
現在,我只問了橋臺的價值。但它只包含3個軸而不是4個。這是因爲我想知道每個軸之間的距離。所以我總是需要少一點。
我解決了這個問題,通過創建1個額外的div來創建圓(軸),並將位置放在左邊。
我想要的是這樣的: 顯示axle_nr
從bogie
表(因此它顯示4)。 獲取distance
其中axle
= axle_nr
。
然後你總是保持1空。因爲軸4不存在於axle
表中。 所以我想做一個檢查:如果軸不存在,那麼距離= 0。我不想在數據庫中插入這個,但只是這樣我就不再需要無用的軸div了,軸保持在剩下。
爲什麼我要這個?
這種方式我可以檢查極小的轉向架數字是相同的,所以我可以給他們每個其他顏色等我也不需要useless_axle div!
編輯:
簡單的解釋:
我想表明從bogie
表Axle_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>
圖片編輯:
我還沒有得到你的問題。我會建議簡單解釋一下。所以,人們可以幫助你。 – Ironic
編輯@ 404 – Mitch