在我的数据库中,我有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();
}
现在,页面看起来像这样(使用数据库的值):
我提供的代码仅适用于车轴! (火车下面的4个圆圈)!
现在,我想要的是什么:
现在,我只想问一下轴台的价值。但它只包含3个轴而不是4个。这是因为我想知道每个轴之间的距离。所以我总是少需要1个 我通过创建一个额外的div来创建圆(轴)并且位置在左边来解决这个问题。
我想拥有的是:
显示axle_nr
表中的bogie
(因此显示4)。
得到distance
axle
= axle_nr
然后你总是保持1空。因为{4}表中不存在轴4。
所以我想做一个检查:如果轴不存在那么距离= 0.我不想在数据库中插入它,但只是这样我不再需要无用的轴div而且轴保持在左边
为什么我要这个?
这样我可以检查转向架号码是否相同,所以我可以给它们每个颜色等等。另外我不需要useless_axle div!
修改
简单说明:
我想显示axle
表中的Axle_nr
。 (所以它显示4个圆圈)
然而!我需要bogie
表中的Distance
来制作火车人物
如您所见,axle
表的轴比axle
表少1个
所以我希望“不存在”轴的值为0.我想要它0因为它会出现在火车的开头。 (就像现在没用的车轴一样)
代码编辑:
现在我有了这个:
bogie
并且:
<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>
它显示了12个轴而不是4个!
编辑:
现在它向我展示了4根车轴,这是正确的。 但是我也需要正确的距离。代码我有: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();
}
现在,它告诉我每个轴的边距为10%。这是正确的(如果你只有第一个轴)。它需要像10-15-10-15左右。我该怎么做?
修改
现在我有了以下查询:
<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>
我在这里叫它:
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();
}
图片编辑:
答案 0 :(得分:4)
在我看来,这是解决原始问题的一种相当复杂的方法。你是害羞的一个轴,并需要该轴在你的数据库中。您说所有值都是通过数据库中的触发器添加的。如果是这种情况,为什么不添加一个距离为&#39; 0&#39;与火车的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&#39;”。从技术上讲,你有一个距离为&#39; 0&#39;从火车前面开始,为什么不在你的数据库中跟踪它。
<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>
采用这种方法既可以简化并解决您的问题。
答案 1 :(得分: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行。