dtxb75622 2012-07-18 00:33
浏览 40
已采纳

PHP-如果else语句不起作用?

$nav_next_id = $page_id + 1;

$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'"; {
$result = mysql_query($sql);
while ($next_page = mysql_fetch_array($result))
{
    if($page_id + 1 > 24) 
    {
    $goto_next_page = 'some_page.php';
    } 
    else
    {
    $goto_next_page = $next_page['page_title'];     
    }
}
}   

Basically a very straight forward next page button on simple site. Using the ID in the MYSQL table I can determine the next page from the current page.

All works well until I come to the last page. Which is page 24.

So i put an if in the loop saying if the page is > 24 go to some_page.php.

I am echoing $goto_next_page in my html.

However nothing is echoed from page 24, all other pages work fine. What wrong with this script?

---------UPDATE---------------- THIS IS HOW I GOT IT TO WORK ------------------

if($cur_page_id == 24) 
{
$goto_next_page = 'some_page.php';  
} 
else
{
$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'"; {
$result = mysql_query($sql);
while ($next_page = mysql_fetch_array($result))
    {
        $goto_next_page = $next_page['page_title'];
    }
}
}   
  • 写回答

4条回答 默认 最新

  • droxy80248 2012-07-18 00:48
    关注

    Perhaps you meant to use the pre-increment operator:

    if(++$page_id > 24)
    

    Effectively, this means:

    $page_id = $page_id + 1;
    
    if($page_id > 24) {
    

    This operator as well as its brother operator (the post-increment operator) both add 1 to the variable. However, the pre-increment operator adds one and then returns the new value. The post-increment operator, on the other hand, returns the value of the variable and then increments it by 1.

    The reason why your code doesn't work is that $page_id + 1 > 24 doesn't increment $page_id so it always remains the same. This makes sense because if you did:

    $a = $page_id + 1;
    

    You wouldn't expect $page_id to be incremented. You would expect $a to be 1 more than $page_id, but you'd expect $page_id to be the same.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)