0

一个客户需要以按字母顺序排列的网格显示产品。像这样显示。将从数据库中查询产品

A  H  O  U
B  I  P  V
C  J  Q  W
D  K  R  X 
E  L  S  Y 

行可以增加,但列不会增加。最大列数为 4。有人可以给出一个想法。

提前致谢。

4

3 回答 3

0

如果您有一系列想要显示的结果,这非常简单。只需计算每个最后一列中的行数(在这种情况下,要显示的结果总数除以 4 然后四舍五入)。然后,这将成为您的偏移量,以在给定当前列中项目的索引的情况下获取要进入下一列的项目的索引。

像这样的东西会起作用:

<?php
    $rows= array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X');

    $numberOfTableRows= ceil(count($rows)/4.0);
?>
    <table>
<?php   
    for($ndx=0;$ndx<$numberOfTableRows;$ndx++)
    {
?>
    <tr>
        <td><?php echo $rows[$ndx]; ?></td>
        <td><?php echo (isset($rows[$ndx+$numberOfTableRows]) ? $rows[$ndx+$numberOfTableRows] : ''); ?></td>
        <td><?php echo (isset($rows[$ndx+($numberOfTableRows*2)]) ? $rows[$ndx+($numberOfTableRows*2)] : ''); ?></td>
        <td><?php echo (isset($rows[$ndx+($numberOfTableRows*3)]) ? $rows[$ndx+($numberOfTableRows*3)] : ''); ?></td>
    </tr>   
<?php   
    }
?>
    </table>

编辑或使用来自 SQL 的数据:

$result = mysql_query("SELECT user FROM tbl_user ORDER BY tbl_user.user ASC");

// Build the $rows array from the MySQL results
while ($row = mysql_fetch_assoc($result)) {
    $rows[]= $row;  
}

mysql_free_result($result);

$numberOfTableRows= ceil(count($rows)/4.0);
?>
    <table>
<?php   
    for($ndx=0;$ndx<$numberOfTableRows;$ndx++)
    {
?>
    <tr>
        <td><?php echo $rows[$ndx]['user']; ?></td>
        <td><?php echo (isset($rows[$ndx+$numberOfTableRows]) ? $rows[$ndx+$numberOfTableRows]['user'] : ''); ?></td>
        <td><?php echo (isset($rows[$ndx+($numberOfTableRows*2)]) ? $rows[$ndx+($numberOfTableRows*2)]['user'] : ''); ?></td>
        <td><?php echo (isset($rows[$ndx+($numberOfTableRows*3)]) ? $rows[$ndx+($numberOfTableRows*3)]['user'] : ''); ?></td>
    </tr>   
<?php   
    }
?>
    </table>

所以我所做的更改是:我们现在循环遍历结果mysql_query以构建我们的结果数组,并且在输出部分我们现在引用我们想要显示的数据库字段(因为每个$row现在都是一个数组而不是单个值)。

注意我已经使用mysql_*了您在评论中使用的内容,但实际上我们不应该再使用它们,因为它们已被弃用 -mysqli_*改为使用(或 PDO)。

EDIT根据要求切换以将输出更改为ULs。

如果我们写入ULs 会更容易一些,因为我们可以按列而不是按行构建 HTML。因此,使用表格来布局我们得到的 4 列:

<?php
    /** $row generation code as before **/
    $numberOfTableRows= ceil(count($rows)/4.0);
?>
    <table>
    <tr>
<?php
    for($col=0;$col<4;$col++) {
?>
        <td style="vertical-align: top">
        <ul>
<?php
        for($ndx=0;$ndx<$numberOfTableRows;$ndx++)
        {
            if (isset($rows[$ndx+($numberOfTableRows*$col)])) {
?>

        <li><a href="?id=<?php echo $rows[$ndx+($numberOfTableRows*$col)]['user_id'];?>"><?php echo $rows[$ndx+($numberOfTableRows*$col)]['user'];?></li>
<?php
            }
        }
?>
        </ul>
        </td>
<?php   
    }
?>
    </tr>   
    </table>
于 2013-10-31T12:46:59.153 回答
0

试试这个:

<?php
    $rows = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X');

    $numberOfTableRows = ceil(count($rows)/4.0);
?>
    <table>
<?php   
    for($ndx=0;$ndx<$numberOfTableRows;$ndx++)
    {
?>
    <tr>
        <td><?php echo $rows[$ndx]; ?></td>
        <td><?php echo $rows[$ndx+$numberOfTableRows]; ?></td>
        <td><?php echo $rows[$ndx+($numberOfTableRows*2)]; ?></td>
        <td><?php echo $rows[$ndx+($numberOfTableRows*3)]; ?></td>
    </tr>   
<?php   
    }
?>
    </table>
  • 谢谢
于 2013-10-31T13:03:45.593 回答
0

假设数据在按字母顺序排序的一维数组中,这将根据需要将其拆分为行(它正在工作):

<?php
# generate alphabet for demo
# warning: php5.5 generator
function az1() { for($a = 65; $a < 91; $a++) { yield chr($a); } }

$a = [];

foreach(az1() as $s) { $a[] = $s; }

# spit data into columns
$size = round(sizeof($a) / 4);


$b = []; 

for($i = 0; $i < 4; $i++) {
  $b[] = array_slice($a, $i * $size, $size); 
}

# transform it to rows
$c = [];
for($i  = 0; $i < $size; $i++) {
  $c[$i] = [];
  for($j = 0; $j < 4; $j++) {
    if(isset($b[$j][$i]))
      $c[$i][$j] = $b[$j][$i];
    else
      $c[$i][$j] = '';
  }
}

print_r($c);

然后,您可以使用常规foreach生成表行或您希望它包含的内容。

编辑

假设有一些products表包含这样的列:

id | category_id | title | content | image | price | qty 

并且您想按字母顺序显示它们(对于您商店中的某个类别或部分):

SELECT * FROM products WHERE category_id = 5 ORDER BY title ASC

它会给你按字母顺序排序的产品。然后你可以将它提供给上面的代码;

再一次,用代码:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=store', 'youruser', 'yourpasswd');

$pdo->query('SET NAMES UTF8'); # here we say NO to latin1

if(isset($_GET['id'])) {
  $category_id = (int) $_GET['id'];


  $prepared = $pdo->prepared('SELECT * FROM products WHERE category_id = :category_ud ORDER BY title ASC');
  $prepared->execute([':category_id' => $category_id]);

  # don't use this in production with large data sets
  $a = $prepared->fetchAll(PDO::FETCH_ASSOC);

  # spit data into columns
  $size = round(sizeof($a) / 4);

  $b = []; 

  for($i = 0; $i < 4; $i++) {
    $b[] = array_slice($a, $i * $size, $size); 
  }

  # transform it to rows
  $c = [];
  for($i  = 0; $i < $size; $i++) {
    $c[$i] = [];
    for($j = 0; $j < 4; $j++) {
      if(isset($b[$j][$i]))
        $c[$i][$j] = $b[$j][$i];
      else
        $c[$i][$j] = '';
    }
  }

  # send $c to output code here
}

如何输出(假设上面的代码将行生成为$c数组):

<? foreach($c as $row): ?>
<tr>
  <? foreach($row as $product): ?>
    <td>
       .. product information
    </td>
  <? endforeach ?>
</tr>
<? endforeach ?>
于 2013-10-31T13:07:53.307 回答