【问题标题】:How do I make this only appear on the first page? And how do I separate two cells?我如何让它只出现在第一页上?以及如何分隔两个单元格?
【发布时间】:2011-07-19 11:40:02
【问题描述】:

这段代码几乎只是显示了一个包含 16 个单元格(4 列和 4 行)的表格。除了右上角,还有两个“保留”单元格。这段代码也分页。

我想用这段代码修复两件事。现在两个“保留单元格”是相同的。意思是两个单元格都说保留。我如何做到这一点,以便我可以更改两个保留单元格内的内容。抱歉,如果这有点难以理解。

另外,我希望两个保留单元格仅显示在第一页上。除了第一页,它只会回显一个包含 4x4 单元格的表格,就好像保留的单元格不存在一样。

谁能让这成为可能?

废话不多说,代码如下:

        $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 GalleryID ASC LIMIT $Page_Start , $Per_Page";
        $objQuery  = mysql_query($strSQL);
$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>'; **//How do I make it so there are two separate reserve slots,  
  rather than one <td> which controls both cells?** 
  } else {
    echo '<td><img src="https://s3.amazonaws.com/thumbnails/' . $objResult["Picture"] . '" />' .
            $objResult["GalleryName"] . '</td>'; }
  $cell++;
}
echo '</tr></table>';
    ?>

        <br>
view more:
        <?php
        if($Prev_Page)
        {
            echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'>prev</a> ";
        }
            {
                echo "|";
        }
        if($Page!=$Num_Pages)
        {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>next</a> ";
        }
        ?>


</body>
</html>
<?php
mysql_close($objConnect);
?>

【问题讨论】:

标签: php mysql pagination html-table cell


【解决方案1】:
if($cell == 2) {
    echo '<td>RESERVED</td>';
} elseif ($cell == 3) {
    echo '<td>The other cell</td>';
} else {
    echo '<td><img src="https://s3.amazonaws.com/thumbnails/' . $objResult["Picture"] . '" />' .
    $objResult["GalleryName"] . '</td>'; }
    $cell++;
}

编辑:这让你很难理解到底发生了什么,所以我重写了这个。这几乎就是您的代码应该看起来的样子。如果你不能从这里理解基本条件,你需要在自己的时间投入更多的工作来理解基本面,因为很多这些东西都是初级的。

$Page = max((int)$_GET["Page"], 1);
$Prev_Page = $Page - 1;
$Next_Page = $Page + 1;

$Page_Start = max(0, ($Per_Page * ($Page - 1) - 2));
if ($Page === 1) $Per_Page = $Per_Page - 2;
$Num_Pages = ceil($Num_Rows / $Per_Page);

$strSQL .= "ORDER BY GalleryID ASC LIMIT {$Page_Start},{$Per_Page}";
$objQuery = mysql_query($strSQL);

$cell = 0;
$total = mysql_num_rows($objQuery);
?>

<table border="1" cellpadding="2" cellspacing="1">
    <? for ($cell = 0; $cell < $total; ++$cell) : ?>
        <? if ($cell % 4 === 0) : ?>
            <tr>
        <? endif; ?>
        <td>
            <? if ($Page === 1 && $cell === 2) : ?>
                First reserved cell
                <? --$cell; ?>
            <? elseif ($Page === 1 && $cell === 3) : ?>
                Second reserved cell
                <? --$cell; ?>
            <? else: ?>
                <? $objResult = mysql_fetch_array($objQuery); ?>
                <img src="https://s3.amazonaws.com/thumbnails/<?=$objResult["Picture"]; ?>" />
                <?=$objResult["GalleryName"]; ?>
            <? endif; ?>
        </td>
        <? if ($cell % 4 === 3) : ?>
            </tr>
        <? endif; ?>
    <? endfor; ?>
</table>

<br>
view more:
<?php
    if($Prev_Page) {
        echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'>prev</a> ";
        if($Page != $Num_Pages) echo " | ";
    }

    if($Page != $Num_Pages) {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>next</a> ";
    }
?>


</body>
</html>
<?php
mysql_close($objConnect);
?>

【讨论】:

  • 谢谢!分页部分有什么想法吗?
  • 条件变为if ($cell == 2 &amp;&amp; $Page == 1) {} elseif ($cell == 3 &amp;&amp; $Page == 1) {
  • 嗯,我输入了你所说的内容,但随后我在我的网站上得到了分页字面意思是“查看更多:if($Prev_Page) { echo " prev "; } { echo "|" ; } if($Page!=$Num_Pages) { echo " next "; } ?>".上一个和下一个是链接,带我到 404 错误。
  • 请复制并粘贴您的代码。看起来您已经删除了“查看更多:”之后的开始
  • 嗯?查看更多内容后,我没有删除开头的
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2015-12-16
  • 2011-12-30
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 2017-10-02
相关资源
最近更新 更多