dtdt0454 2014-12-19 20:36
浏览 32
已采纳

尝试使用php回显出不在mysql表中的每个int

I am pretty new at this whole programming stuff. I need to echo out any integer from the mysql database that is missing (in chronological order). So this $row["statement"] has integers starting at 114 and goes to 44714. There are missing numbers in this list, and I need to know which ones. The first number that is missing is 118. So it goes 114, 115, 116, 117, 120, ...

I want to echo 118, 119, and any other missing from this $row["statement"].

What I have here just prints until one is missing, so "114 115 116 117".

<?php 
  $link = mysqli_connect("localhost", "root", "password", "DB")or die("Error " . mysqli_error($link));

  $query = "SELECT statement FROM sales" or die ("Error in the consult.." . mysqli_error($link));
  $result = $link->query($query);
  $count = 114;

  while($row = mysqli_fetch_array($result)) {   
    while ($row["statement"] == $count) {
       echo $count . "<br>";
       $count++;
    }
  }
 ?>

I can't think how to do the reverse. If I were to set the second while conditional to check if !==, this would just print out the entire list, including the missing and not missing. i.e. 114, 115, 116, 117, 118, 119, 120...

Is nesting these whiles bad? Should it be a do while? If statement? I don't get the logic. Please learn me real good.

  • 写回答

2条回答 默认 最新

  • dsjmrpym220113739 2014-12-19 21:01
    关注

    You can make it easy on yourself if make an array and then diff the array -

    $count = 114;
    $currentNumbers = array();
    while($row = mysqli_fetch_array($result)) {
        array_push($currentNumbers, $row['statement'];
    }
    $tempArray = range($count,max($number));                                           
    $missingNumbers = array_diff($tempArray,$CurrentNumbers);
    foreach($missingNumber AS $value) {
        echo $value . '<br />';
    }
    

    range() array_diff() EXAMPLE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站