如何从数据库中显示表格的某些部分?

时间:2011-03-21 02:51:26

标签: php mysql database pagination html-table

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);
?>

2 个答案:

答案 0 :(得分:0)

您的$ 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>';

答案 1 :(得分:0)

在你的循环中,你需要输入一些条件语句来摆脱默认行为。

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>";