CSS / PHP / MySQL - 将结果显示为3列


CSS / PHP / MySQL - Display results into 3 columns

我正在尝试将查询结果显示为返回的每一行的三列。

设置是 3 个div,全部漂浮在一个大包装器内。很简单,是吗?

#wrapper {width: 650px;}
#left    {width: 200px; float: left;}
#middle  {width: 200px; float: left;}
#right   {width: 200px; float: left;}
Results displayed like:
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT

直到现在,我才有了这个 mysql 关联查询,我想在每一列中显示结果。

简单来说,我有Column A需要在leftdiv中。

Columns B through Y middle div.

Column Zright区。

这是我的查询:

while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
           if ($col == "Column A") {
              //How to get this in left div?
              }
           if ($col != "Column A" && $col != "Column Z") {
              //How to get this in middle div?
              }
           if ($col == "Column Z") {
              //How to get this in right div?
              }
        }
}

我真的不知道从哪里开始。

始终尝试将视图 (HTML) 与实际代码分开。您可能想阅读一些有关 MVC 架构的信息。乍一看似乎很难理解,但相信我 - 值得学习如何使用它。

<?php 
$column = array(
    'A' => array(), 
    'B' => array(), 
    'Z' => array()
);
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
           if ($col == "Column A") {
              $column['A'][] = $row;
              }
           if ($col != "Column A" && $col != "Column Z") {
              $column['B'][] = $row;
              }
           if ($col == "Column Z") {
              $column['Z'][] = $row;
              }
        }
}
?>
<style type="text/css">
div {width:200px;float:left}
</style>
<!-- 1st column -->
<div><?php foreach ($column['A'] AS $row) { ?>...<?php } ?></div>
<!-- 2nd column -->
<div>..</div>
<!-- 3rd column -->
<div>..</div>

干杯!

你在这里有很多选择。但正如我所做的那样,这是制作 3 或 1 个数组,然后在我的模板中以 html 迭代它们,你到达了那里。

像这样:

$col_left  = array();
$col_mid   = array();
$col_right = array();
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
           if ($col == "Column A") {
                  $col_left[] = $val;
              }
           if ($col != "Column A" && $col != "Column Z") {
                  $col_mid[] = $val;
              }
           if ($col == "Column Z") {
                  $col_right[] = $val;
              }
        }
}

然后只是在 html 中循环这些和你完成。还有更多的选择,但这将是从我的头顶开始的。

然后在 html 中:

<div id="left">
<?php 
   foreach($col_left as $result){
      echo $result.'<br/>';
   }
?>
</div>

类似的东西,ofc您可以在那里添加空检查等。

我会稍微自主地做这件事,考虑到您当前的标识符。我刚刚将所有内容转储到一个数组中,并在底部用换行符连接它。我的例子可能有点太笼统了,但它肯定会完成工作,并且完全可以进行调整:)

<?
$columns = array(
    'left'      => array(),
    'middle'    => array(),
    'right'     => array()
);
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
        if ($col == "Column A") {
            $columns['left'][] = $val;
        } elseif ($col == "Column Z") {
            $columns['right'][] = $val;
        } else {
            $columns['middle'][] = $val;
        }
    }
}
?>
<div id="wrapper">
    <? foreach($columns as $column => $value_array) { ?>
    <div id="<?= $column ?>">
        <?= join("<br />", $value_array) ?>
    </div>
    <? } ?>
</div>

我做了这个确切的事情,但我把结果放到一个表格中。 我将模函数与辅助计数器变量一起使用。

if (($artisttablecount % 3) == 0){
        echo "</tr><tr>";
      }

我不确定如何使用div 做到这一点,但 mod 功能可能会有所帮助。 完整代码如下。

  //QUERY
$artisttable = mysql_query("SELECT DISTINCT * FROM records GROUP BY artist ORDER BY artist ASC");
//HELPER VAR FOR PRINTING IN 3 ROWS
$artisttablecount=0;
//LOOP THROUGH RESULTS
while($row = mysql_fetch_array($artisttable)){
//MAKES TABLE 3 COLUMNS
  if (($artisttablecount % 3) == 0){
    echo "</tr><tr>";
  }
  $current=$row['Artist'];      
  $artcount = mysql_num_rows(mysql_query("SELECT * FROM records WHERE artist = '$current'"));
  $spaceless = str_replace(" ", "%20", $row['Artist']); 
  echo "<td><a href=inventory.php?dec=0&format=0&art='" . $spaceless . "'>" .  $row['Artist'] . "</a> ($artcount)</td>";    
  $artisttablecount++;
}
?>