简体   繁体   English

从数据库获取记录并插入特定行

[英]Get records from database and insert in specific row

I have a select query that retrieves all data from 2 tables using INNER JOIN . 我有一个选择查询,该查询使用INNER JOIN从2个表中检索所有数据。 One of the tables is called Rack_Monitor and the otherone is called Rack_Content . 其中一个表称为Rack_Monitor ,另一个表称为Rack_Content Rack_Monitor has a column named TotalUnits ( for example 48 ). Rack_Monitor有一个名为TotalUnits的列(例如48)。 Rack_Content has a column named Units ( for example 3 or 3;4 the delimiter is a ; if it contains multiple units. Rack_Content有一个名为Units的列(例如3或3; 4,如果分隔符包含多个单位,则定界符为;)。

Question: 题:

Now I need to build a table in Twig that loops 48 times (TotalUnits) and check if there is any content to fill. 现在,我需要在Twig中构建一个表,该表循环48次(TotalUnits),并检查是否有任何要填充的内容。 If not than the row must be empty. 如果不是,则该行必须为空。 The array will start at 0 and the content of that array has a unit of 3;4 so it has to be placed on the 3rd and 4th row of the table. 该数组将从0开始,并且该数组的内容的单位为3; 4,因此必须将其放在表的第三行和第四行。

Code looping 48 times: 代码循环48次:

<table class="table" id="diagnosis_list">
  <thead>
    <tr>
      <th>Unit</th>
    </tr>
  </thead>
  <tbody>
    {% set TotalUnits = RackContent[0].TotalUnits %}
    {% for i in 1.. TotalUnits %}
      <tr>
        <td class="priority" style="">{{ i }}</th>
      </tr>
    {% endfor %}
  </tbody>
</table>

This code is showing 48 rows of numbers for the table. 此代码显示该表的48行数字。

I'm stuck with the next code. 我陷入下一个代码。 This is what I've got so far. 这就是到目前为止。 Am I on the right track?: 我在正确的轨道上吗?:

                   <div class="col-xs-5 col-md-7 col-lg-7">
  <table class="table" id="diagnosis_list">
    <thead>
    <tr>
      <th>Name</th>
      <th>Intern IP</th>
      <th>Extern IP</th>
      <th>Backup</th>
      <th>&nbsp;</th>
    </tr>
    </thead>
    <tbody>
      {% for i in 0..TotalUnits %}

        {% if RackContent[i] is defined %}
          {% set UnitSplit = RackContent[i].Units|split(';') %}
          <tr>
            <td><h5>HostName</h5><small>Webserver</small></td>
            <td><h5>InternIP</h5><small>WebserverInternIP</small></td>
            <td><h5>IPMI IP</h5><small>WebserverIP</small></td>
            <td><h5>BackupserverIP</h5></td>
            <td><a class='btn btn-warning'>
                <i class="fa fa-edit text-white"></i>
                </a>
              <a class='btn btn-danger'>
                <i class="fa fa-trash-o text-white" onclick="Delete(2)"></i>
                </a>
            </td>
          </tr>
        {% else %}
          <tr>
            <td><h5>Empty</h5><small>Empty</small></td>
            <td><h5>Empty</h5><small>Empty</small></td>
            <td><h5>Empty</h5><small>Empty</small></td>
            <td><h5>Empty</h5></td>
            <td><a class='btn btn-warning'>
                <i class="fa fa-edit text-white"></i>
              </a>
              <a class='btn btn-danger'>
                <i class="fa fa-trash-o text-white" onclick="Delete(2)"></i>
              </a>
            </td>
          </tr>
        {% endif %}

      {% endfor %}

    </tbody>
  </table>
</div>

Why I've 2 tables set up is because it is a draggable table. 为什么要设置2个表是因为它是可拖动的表。 So the numbers are static and will not be able to move. 因此数字是静态的,将无法移动。 The other table with the content can be moved up and down rows. 带有内容的另一个表可以上下移动。

The code is not done yet but I just want to know if you guys will program it the same way or is there a more efficient way to do that? 代码尚未完成,但是我只想知道你们是否将以相同的方式对其进行编程,或者是否有更有效的方法来进行编程?

This is how i did it. 这就是我做到的。 It works fine now. 现在工作正常。

<table class="table table-condensed">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>Hardware</th>
                        <th>Name</th>
                        <th>Intern IP</th>
                        <th>Extern IP</th>
                        <th>Backup</th>
                        <th>Options</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for i in 1..(TotalUnits) %}
                        {% set ID = i  %}
                        {% set FoundIt = 0 %}
                        {% for R in RackContent %}
                            {% for U in R.Units %}
                                {% if loop.length == 2 %}
                                    {% if U == i and loop.index == 1 %}
                                        <tr class="success">
                                            <td>{{ ID }}</td>
                                            <td rowspan="2"><h5>{{ R.Name }}</h5></td>
                                            <td rowspan="2"><h5>{{ R.HostName }}</h5><small>{{ R.WebServer }}</small></td>
                                            <td rowspan="2"><h5>{{ R.InternIP }}</h5><small>{{ R.WebServerInternIP }}</small></td>
                                            <td rowspan="2"><h5>{{ R.IPMIIP }}</h5><small>{{ R.WebServerIP }}</small></td>
                                            <td rowspan="2"><h5>{{ R.BackupServer }}</h5></td>
                                            <td rowspan="2"><div class="dropdown">
                                                    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                                        Options
                                                        <span class="caret"></span>
                                                    </button>
                                                    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-eye text-blue"></i>Open</a></li>
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-edit text-orange"></i>Edit</a></li>
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash-o text-red"></i>Delete</a></li>
                                                    </ul>
                                                </div></td>
                                        </tr>
                                        {% set FoundIt = 1 %}
                                    {% elseif U == i and loop.index == 2%}
                                        <tr class="success">
                                            <td>{{ ID }}</td>
                                        </tr>
                                        {% set FoundIt = 1 %}
                                    {% endif %}
                                {% elseif loop.length == 1 %}
                                    {% if U == i and loop.index == 1 %}
                                        <tr class="success">
                                            <td>{{ ID }}</td>
                                            <td><h5>{{ R.Name }}</h5></td>
                                            <td><h5>{{ R.HostName }}</h5><small>{{ R.WebServer }}</small></td>
                                            <td><h5>{{ R.InternIP }}</h5><small>{{ R.WebServerInternIP }}</small></td>
                                            <td><h5>{{ R.IPMIIP }}</h5><small>{{ R.WebServerIP }}</small></td>
                                            <td><h5>{{ R.BackupServer }}</h5></td>
                                            <td><div class="dropdown">
                                                    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                                        Options
                                                        <span class="caret"></span>
                                                    </button>
                                                    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-eye text-blue"></i>Open</a></li>
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-edit text-orange"></i>Edit</a></li>
                                                        <li><a href="#" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash-o text-red"></i>Delete</a></li>
                                                    </ul>
                                                </div></td>
                                        </tr>
                                        {% set FoundIt = 1 %}
                                    {% endif %}
                                {% endif %}
                            {% endfor %}
                        {% endfor %}
                        {% if FoundIt == 0 %}
                            <tr class="active">
                                <td>{{ ID }}</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                    </tbody>
                </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM