【发布时间】:2015-08-10 18:12:15
【问题描述】:
在我的数据库中,我有 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, which 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 的值):
我提供的代码仅适用于车轴! (火车下面的 4 个圆圈)!
现在,我想要什么:
现在,我只是询问轴表的价值。但它只包含 3 个轴而不是 4 个。这是因为我想知道每个轴之间的距离。所以我总是需要少 1 个。
我通过制作 1 个额外的 div 来解决这个问题,该 div 创建了圆圈(轴)并且位置在左侧。
我想要的是这样的:
显示bogie 表中的axle_nr(因此显示为 4)。
获取distance,其中axle = axle_nr。
然后你总是保持 1 为空。因为轴 4. 在 axle 表中不存在。
所以我想检查一下:如果轴不存在,那么距离 = 0。我不想将它插入数据库中,但是这样我就不再需要无用的轴 div 并且轴保持在左侧.
我为什么要这个?
这样我可以检查哪些转向架编号是相同的,所以我可以给它们另一种颜色等。而且我不需要 useless_axle div!
编辑:
简单解释:
我想显示bogie 表中的Axle_nr。 (所以它显示4个圆圈)
然而!我需要axle 表中的Distance 才能制作火车图。
如您所见,axle 表的轴比 bogie 表少 1 个。
所以我希望“不存在”轴的值为 0。我希望它为 0,因为这样它就会出现在火车的起点上。 (就像现在没用的车轴)
代码编辑:
现在我得到了这个:
<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>
图片编辑:
【问题讨论】:
-
我仍然没有得到你的问题。我建议简单解释一下。所以,人们可以帮助你。
标签: php mysql css database loops