简体   繁体   English

使用PHP和MySQL以两个“序列”显示数据

[英]Display data in two 'sequences' using PHP and MySQL

I'm thinking how to (because I don't have any code yet) display data from mysql database (using PHP) like this.. 我正在考虑如何(因为我还没有任何代码)显示来自mysql数据库(使用PHP)的数据。

For example, my table tb_services : 例如,我的表tb_services

+--------------+----------------------+---------------------------+-----------------
|      id      |        title         |          body             |     image      |
+--------------+----------------------+---------------------------+----------------+
|       1      |      service 1       |         body 1            |    img1.jpg    |
|       2      |      service 2       |         body 2            |    img2.jpg    |
|       3      |      service 3       |         body 3            |    img3.jpg    |
|       4      |      service 4       |         body 4            |    img4.jpg    |
+--------------+----------------------+---------------------------+----------------+

and the PHP code could be like this: 并且PHP代码可能是这样的:

<?php

    mysql_connect("localhost", "root" , "");
    mysql_select_db("db_services");

    $sql = mysql_query("select * from tb_services");

    $data = array();

    while ($reg = mysql_fetch_assoc($sql){
        $data[] = $reg;
    }

?>

The result I need should be like this: 我需要的结果应该是这样的:

在此输入图像描述

(Maybe not like that style) (也许不喜欢那种风格)

Thank your for answers. 谢谢你的回答。

Can you try this, 你能试试吗

$i=1;
while ($reg = mysql_fetch_assoc($sql){
    $data[] = $reg;

  if($i%2)==1){
      echo "First Layout";
  }else{
      echo "Second Layout";
  }

 $i++;
}

Keeping it simple... 保持简单......

$odd = true;
while ($reg = mysql_fetch_assoc($sql){
    $data[] = $reg;

    if($odd){
        $odd=false;
        //echo left layout
    }
    else{
        $odd=true;
        //echo right layout
    }
}

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

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