dongtanliefang8765 2015-07-14 08:22
浏览 36
已采纳

如何在php中的两列表中列出数据库中的选定项?

So here's what I basically want to do. I have display a list of members and each of them have a check box next to it.

Just like this:

[ ] Spongebob

[ ] Patrick

[ ] Squidward

[ ] Sandy

[ ] Mr. Krab

[ OK ]

Whenever I select a number of members, and click OK, it will lead to a print page. In that print page, I want the members to be listed in two columns like this:


Spongebob | Patrick |

Squidward | Sandy |

Mr. Krab |

So far, I've only done it to be in a single column.

Here is my current code:

if(isset($_POST['ok'])){
if(!empty($_POST['check_list'])) {

$check_list = $_POST['check_list']; 
foreach ($check_list as $selected){
$sql = mysql_query("SELECT * FROM addmember WHERE id = '".$selected."' ORDER BY id");

$limit = 2;
$count = 0;

echo "<table>";

while ($row = mysql_fetch_array($sql)){
    $name = $row ['name'];

    if ($count < $limit){

        if ($count == 0){
            echo "<tr>";
            }

            echo "<td>$name";
        }else{
            $count = 0;
            echo "</tr><tr><td>
            $name</td><tr>";
            }
            $count++;
        }
        echo "</tr></table>";
        }

        echo '<script>window.print();</script>';
        }
    else{

    echo "<script>alert('Please select at least one member.');</script>";
    echo "<script>window.history.go(-1)</script>";
    }
}

Any suggestion and advice is very appreciated.. Thank you very much.

  • 写回答

1条回答 默认 最新

  • dslfq06464 2015-07-15 07:38
    关注

    I think that your while is not necessary, because you only need one row. You have overcomplicated things a bit.

    You need to check

    • If $count is 0 - start row
    • If $count == $limit or is last item of all - end row

    And thats it. Between just echo <td> with $name.

    So try this:

    if(isset($_POST['ok'])){
      if(!empty($_POST['check_list'])) {
    
        $check_list = $_POST['check_list'];
    
        $itemsCount = count($check_list);
        $limit = 2;
        $count = $doneCount = 0;
    
        echo "<table>";
    
        foreach ($check_list as $selected){
          $sql = mysql_query("SELECT * FROM addmember WHERE id = '".$selected."' ORDER BY id LIMIT 1");
          $row = mysql_fetch_array($sql);
    
          $name = $row['name'];
    
          // Start new row
          if ($count == 0){
            echo "<tr>";
          }
    
          // Echo name
          echo "<td>".$name."</td>";
    
          // End row, increment counters
          if (++$count == $limit || $itemsCount == ++$doneCount ){
            $count = 0;
            echo "</tr>";
          }
    
        }
    
        echo "</tr></table>";
    
        echo '<script>window.print();</script>';
      }
      else{
        echo "<script>alert('Please select at least one member.');</script>";
        echo "<script>window.history.go(-1)</script>";
      }
    }
    

    ++$count increments by one and then returns it, so we compare and increment all in one. See PHP: Incrementing/Decrementing Operators.

    Don't use mysql_* functions anymore, cause they are deprecated. Instead use MySQLi or PDO.

    Futher reading:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么