【问题标题】:Random gaps in results that are printed from a while loop?从while循环打印的结果中的随机间隙?
【发布时间】:2012-10-25 15:26:12
【问题描述】:

我创建了一个搜索表单,允许用户在包装数据库中搜索结果。我最近在将结果打印成三行时遇到了问题,因为所有结果都在下面打印一个,每个结果。我设法解决了这个问题,现在结果是三行,除了现在,有随机间隙,一两行只有一个结果,但结果将在下一行继续 3。我似乎无法确定为什么这是随机发生的。

它似乎没有任何模式,它取决于用户搜索的内容。

这是 PHP 代码:

       <?php
            $con = mysql_connect ("localhost", "horizon1", "");
                   mysql_select_db ("horizon1_delyn", $con);

            if (!$con)
                { 
                    die ("Could not connect: " . mysql_error());
                }

            $descrip = mysql_real_escape_string($_POST['descrip']); 
            $depth   = mysql_real_escape_string($_POST['depth']);
            $varWidth = mysql_real_escape_string($_POST['traywidth']);
            $varHeight= mysql_real_escape_string($_POST['trayheight']);
            $varRange = mysql_real_escape_string($_POST['trayrange']);
            $varType  = mysql_real_escape_string($_POST['traytype']);
            $varShape = mysql_real_escape_string($_POST['trayshape']);
            $varImage = mysql_real_escape_string($_POST['imagename']);

            if (isset($varHeight) && !empty($varHeight)) {
                    $low  = ($varHeight."00");
                    $high = ($varHeight."99");
                } else {
                    $low  = ("000");
                    $high = ("999");
                }

            if (isset($varWidth) && !empty($varWidth)) {
                    $min  = ($varWidth."00");
                    $max = ($varWidth."99");
                } else {
                    $min  = ("000");
                    $max = ("999");
                }   


            $sql = "SELECT * FROM range WHERE 
                        description LIKE '%".$descrip."%'  
                    AND trayrange   LIKE '%".$varRange."%' 
                    AND traytype    LIKE '%".$varType."%' 
                    AND trayshape   LIKE '%".$varShape."%'
                    AND traywidth   BETWEEN '".$min."'  AND '".$max."' 
                    AND trayheight  BETWEEN '".$low."' AND '".$high."' ";


                $r_query = mysql_query($sql);  

    $count = 0;
    while ($row = mysql_fetch_array($r_query)) 
    { 

    $t = $count%1;
    echo ($t==4) ?  '<div id="results">' : '<div id="results" style="float:left">';

    echo '<p class="image">
            <img src="   '. $row['imagename'] . '" width="150" length="80"> 
          </p>';
    echo '<div id="table">
          <br /><strong> Tool Code:  </strong> '. $row['toolcode'];
    echo '<br /><strong> Description:</strong> '. $row['description']; 
    echo '<br /><strong> Tray range: </strong> '. $row['trayrange']; 
    echo '<br /><strong> Tray type:  </strong> '. $row['traytype'];
    echo '<br /><strong> Tray size:  </strong> '. $row['traysize']; 
    echo '<br /><strong> Tray shape: </strong> '. $row['trayshape'] . '</div>' . '<br />';
    echo '<a href="http://www.delynpackaging.co.uk/contact.htm">Enquiry Page</a> <br />' . 
         '<a href="http://176.67.171.11/range-test/rangetest.php">Back to Search</a>';
    echo '</div>' . '</div>';

    $count++;
}

            if (mysql_num_rows($r_query) <= 0){
                echo 'No results match your search, please try again';
           }
    ?>

谁能明白为什么会发生这种情况?

【问题讨论】:

  • 仅供参考,具有相同 ID 的多个元素是非语义的。另外,$t 永远不能是= 4,因为$count%1 = 0 在任何时候
  • 提醒一句,id 应该只有 1 个 DOM 实例。看起来您正在输出一个 id="table" >1 次的 div。向其添加索引以使其成为唯一标识符,例如id="table1" 或将其更改为一个类,例如class="table"Read more here

标签: php html sql css


【解决方案1】:

这可能取决于盒子的宽度,当它们不适合行时它们会包裹起来。 还有 $t = $count%1;看起来不寻常,应该是 $t = $count%3;然后条件应为: ($count>0 && $t==0)

【讨论】:

  • 它们很合身,因为它们的尺寸都一样。因此,如果上面的三行可以容纳,那么为什么只有 1 或 2 行是没有意义的。我更改了代码,但不幸的是它没有任何区别。但是感谢您的帮助。
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 2021-10-12
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 2014-04-16
  • 1970-01-01
相关资源
最近更新 更多