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条)

报告相同问题?

悬赏问题

  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少