Mit*_*tch 19 css php mysql database loops
在我的数据库中,我有3个表:
train_information:
+----------+-----------------+------------------+
| train_id | number_of_axles | number_of_bogies |
+----------+-----------------+------------------+
| 1 | 4 | 2 |
+----------+-----------------+------------------+
Run Code Online (Sandbox Code Playgroud)
轴:
+---------+----------+------+----------+
| axle_id | train_id | axle | distance |
+---------+----------+------+----------+
| 1 | 1 | 1 | 2500 |
| 2 | 1 | 2 | 5000 |
| 3 | 1 | 3 | 2500 |
+---------+----------+------+----------+
Run Code Online (Sandbox Code Playgroud)
转向架:
+----------+----------+---------+----------+
| bogie_id | train_id | axle_nr | bogie_nr |
+----------+----------+---------+----------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 3 | 1 | 3 | 2 |
| 4 | 1 | 4 | 2 |
+----------+----------+---------+----------+
Run Code Online (Sandbox Code Playgroud)
当某些内容插入到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>
Run Code Online (Sandbox Code Playgroud)
和:
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();
}
Run Code Online (Sandbox Code Playgroud)
现在,页面看起来像这样(使用DB的值):
我提供的代码仅适用于车轴!(火车下面的4个圆圈)!
现在,我想要的:
现在,我只想问一下轴台的价值.但它只包含3个轴而不是4个.这是因为我想知道每个轴之间的距离.所以我总是少需要1个.
我通过创建一个额外的div来创建圆(轴)并且位置在左边来解决这个问题.
我想拥有的是:axle_nr
从bogie
表中显示(所以它显示4).得到distance
where axle
= axle_nr
.
然后你总是保持1空.因为轴4不存在于axle
表中.所以我想做一个检查:如果轴不存在那么距离= 0.我不想在数据库中插入它,但只是这样我不再需要无用的轴div而且轴保持在左边.
我为什么要这个?
这样我可以检查转向架号码是否相同,所以我可以给他们每个其他颜色等.此外,我不需要useless_axle div!
编辑:
简单说明:
我想Axle_nr
从bogie
表中显示出来.(所以它显示4个圆圈)但是!我将需要Distance
从axle
为了使列车数字表.
你可以看到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>
Run Code Online (Sandbox Code Playgroud)
和:
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();
}
Run Code Online (Sandbox Code Playgroud)
它显示了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>
Run Code Online (Sandbox Code Playgroud)
现在,它告诉我每个轴都有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();
}
Run Code Online (Sandbox Code Playgroud)
我在这称呼它:
<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>
Run Code Online (Sandbox Code Playgroud)
图片编辑:
在我看来,这是解决原始问题的一种相当复杂的方法。您缺少一个轴,并且需要将该轴存在于您的数据库中。您说所有值都是通过数据库中的触发器添加的。如果是这样的话,为什么不添加一个与火车 id 距离为“0”的值。这不仅会给你轴,还会给你渲染的 div。
如果您的表在生成后看起来像这样(如果索引方向错误,请原谅我。我正在努力理解您的数据库布局):
+---------+----------+------+----------+
| axle_id | train_id | axle | distance |
+---------+----------+------+----------+
| 0 | 1 | 0 | 0 |
| 1 | 1 | 1 | 2500 |
| 2 | 1 | 2 | 5000 |
| 3 | 1 | 3 | 2500 |
+---------+----------+------+----------+
Run Code Online (Sandbox Code Playgroud)
然后,以下代码将生成所有圆圈,包括边距(或您之前所说的距离)为“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>
Run Code Online (Sandbox Code Playgroud)
采用这种方法既可以简化又可以解决您的问题。