【问题标题】:How do I not display certain parts of the table from my database?如何不显示数据库中表的某些部分?
【发布时间】:2011-07-19 09:41:58
【问题描述】:

http://i.stack.imgur.com/5OTgG.png

上图描述了我的问题,并提供了视觉效果。 如您所见,我只想保留或删除右上角的两个空格。我怎样才能做到这一点?

这是我当前的代码:

<html>
<head>
</head>
<body>
    <?php
        $objConnect = mysql_connect("localhost","root","") or die(mysql_error());
        $objDB = mysql_select_db("dbname");
        $strSQL = "SELECT * FROM album";
        if (!isset($_GET['Page']))  $_GET['Page']='0';
        $objQuery = mysql_query($strSQL);
        $Num_Rows = mysql_num_rows($objQuery);

        $Per_Page = 12;   // Per Page
        $Page = $_GET["Page"];
        if(!$_GET["Page"])
        {
            $Page=1;
        }

        $Prev_Page = $Page-1;
        $Next_Page = $Page+1;

        $Page_Start = (($Per_Page*$Page)-$Per_Page);
        if($Num_Rows<=$Per_Page)
        {
            $Num_Pages =1;
        }
        else if(($Num_Rows % $Per_Page)==0)
        {
            $Num_Pages =($Num_Rows/$Per_Page) ;
        }
        else
        {
            $Num_Pages =($Num_Rows/$Per_Page)+1;
            $Num_Pages = (int)$Num_Pages;
        }

        $strSQL .=" order  by albumID ASC LIMIT $Page_Start , $Per_Page";
        $objQuery  = mysql_query($strSQL);


        echo"<table border=\"0\"  cellspacing=\"1\" cellpadding=\"1\"><tr>";
        $intRows = 0;
        while($objResult = mysql_fetch_array($objQuery))
        {
            echo "<td>"; 
            $intRows++;
    ?>
            <center>
                <img src="https://s3.amazonaws.com/thumbnails/<?=$objResult["Picture"];?>" height="190" width="190" /></a><br>
                <?=$objResult["albumName"];?>
                <br>
            </center>
    <?php
            echo"</td>";
            if(($intRows)%4==0)
            {
                echo"</tr>";
            }
        }
        echo"</tr></table>";
    ?>

        <br>
        <?php
        //DELETED PAGINATION CODE for the sake of simplicity in StackOverflow
                ?>
</body>
</html>
<?php
mysql_close($objConnect);
?>

【问题讨论】:

  • 看起来你从来没有打开过&lt;tr&gt;,除非你创建了表。
  • 您希望在这些插槽中放入什么?是不是您没有从数据库中获取的另一个图片/专辑名称?
  • 是的,或者只是一些无关的东西的空格。还没决定。
  • 顺便说一句,您正在不必要地敲打您的数据库。代替执行“SELECT * FROM album”,然后执行 mysql_num_rows(..) 您可以执行“SELECT count(*) FROM album”,它只返回表中的行数并且非常有效。这也将消除像现在这样查询专辑表两次的需要。
  • 另外,在 DB 查询中使用来自 $_GET 的值存在安全风险。我建议仅使用 $_GET 将参数解析为其他变量,同时确保它们具有有效数据。例如,对于只能具有数值的变量,请执行以下操作: $PageNum = ($_GET['page'] ? intval($_GET['page'] : 0);

标签: php mysql database pagination html-table


【解决方案1】:

在您的循环中,您需要放入一些条件语句来打破默认行为。

while($objResult = mysql_fetch_array($objQuery))
{
    if(($intRows)%4==0)
    {
        echo"<tr>"; // You forgot this.
    }

    echo "<td>";
    $intRows++;
    if ($intRows == 3 || $intRows == 4) : 
?>
    <!-- special cells -->
<?
    else:
?>
    <center>
        <img src="https://s3.amazonaws.com/thumbnails/<?=$objResult["Picture"];?>" height="190" width="190" /></a><br>
        <?=$objResult["albumName"];?>
        <br>
    </center>
<?php
    endif;

    echo"</td>";
    if(($intRows)%4==0)
    {
        echo"</tr>";
    }
}
// You also need this part:
echo ($intRows %4 > 0 ? "</tr>" : "") . "</table>";

【讨论】:

  • 嗯,对不起,兄弟,由于某种原因它无法正常工作。不过感谢您的努力。
  • 我非常想知道这怎么行。这是一个非常简单的条件。你能复制你的代码吗?也许是一个错字的变量或什么。
【解决方案2】:

您的 $intRows 变量似乎在计算单元格,而不是行数。所以你可以简单地通过它们的数字来识别右上角的单元格,它们应该是 2 和 3。将这个添加到你的循环中:

$cell = 0;
echo '<table border="1" cellpadding="2" cellspacing="1"><tr>';
while($objResult = mysql_fetch_array($objQuery))
{
  if($cell % 4 == 0) {
    echo '</tr><tr>';
  }

  if($cell == 2 || $cell == 3) {
    echo '<td>RESERVED</td>';
  } else {
    echo '<td>'.$objResult["albumName"].'</td>';
  }

  $cell++;
}
echo '</tr></table>';

【讨论】:

  • 它似乎不起作用:/。或者,也许我将您的代码放在错误的位置。你能具体说明我应该在哪里实现吗?
  • 它应该进入你迭代数据库记录的while循环中。您还必须确保在正确的位置增加 $intRows。尝试用简化版本替换循环的内容以找出问题所在。
  • 我编辑了我的回复以使其更详细。看看是否效果更好。
  • 是的,我认为我应该在哪里实现代码,但我得到的只是一个显示我数据库中前两个图像的页面。而已。此外,您在我原来的帖子上的 cmets 非常有见地。不幸的是,我是一个新手程序员。当我用“SELECT count(*) FROM album”替换我的代码时,我得到了错误。那么 &_GET 的替代方法是什么?你能做一个新的回答来详细说明你到目前为止所说的一切吗?编辑:你7分钟前的帖子,我现在试试。
  • 很好,它'有点工作'。您的代码中有一些问题(它没有显示图像),但我可以解决这个问题。你能详细说明计数和 $_GET 吗?
猜你喜欢
  • 1970-01-01
  • 2010-09-17
  • 2016-02-01
  • 2020-06-27
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多