Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单击“给变量另一个值”_Php_Html_Pagination_Onclick - Fatal编程技术网

Php 单击“给变量另一个值”

Php 单击“给变量另一个值”,php,html,pagination,onclick,Php,Html,Pagination,Onclick,我正在尝试实现页面分页功能。我有自定义的帖子类型,我已经循环并收集了这个页面的所有帖子。我显示第一篇文章并隐藏其余的文章,我有“下一篇/上一篇”按钮,可以显示下一篇文章并隐藏当前文章。这似乎是工作,但我无法查看所有的职位-只有其中的2个 这是我的密码: <?php $currProj = 1; $countposts = 3; ?> <?php if ($countposts

我正在尝试实现页面分页功能。我有自定义的帖子类型,我已经循环并收集了这个页面的所有帖子。我显示第一篇文章并隐藏其余的文章,我有“下一篇/上一篇”按钮,可以显示下一篇文章并隐藏当前文章。这似乎是工作,但我无法查看所有的职位-只有其中的2个

这是我的密码:

<?php
            $currProj = 1;
            $countposts = 3;
            ?>
                <?php if ($countposts>1) { 
                    $prevID = $currProj - 1;

                    if ($prevID <= 0) { 
                        $prevID = $countposts;
                    ?>
                        <a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
                    <?php } else { 
                    ?>
                        <a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
                    <?php 
                    //$currProj = $prevID;  
                    }

                    echo $currProj; //This outputs 3 where it should be 1
                    $nextID = $currProj + 1; 

                    if ($nextID > $countposts) { 
                        $nextID = 1;

                    ?>
                        <a class="btn-next" id="nextlink" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $nextID; ?>')" onclick="<?php $currProj=$nextID; ?>"> Next > </a>
                    <?php } else { 

                    ?> 
                        <a class="btn-next" id="nextlink" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $nextID; ?>')" onclick="<?php $currProj=$nextID; ?>"> Next > </a>
                    <?php 
                    //$currProj = $nextID;
                    }

                } ?>

javascript函数工作正常,问题似乎在于$currProj变量。我认为问题在于标记中的onClick属性——有没有更好的方法让onClick事件给变量一个不同的值?或者一种检查此链接是否已单击的方法,然后为currProj提供prevID/nextID的值


我已经在这个问题上纠缠了一段时间了,我似乎什么也没有得到。任何帮助都将不胜感激

创建HTML隐藏输入来存储这些值怎么样

<input type="hidden" value="<?php echo $currProj ?>" id="currProj">

您上面的代码似乎将服务器端发生的事情与客户端发生的事情混为一谈。任何包装在
中的内容都将在服务器上执行,然后以HTML格式发送到客户端——例如,这一行:

<a class="btn-previous" href="javascript:showProject('<?php echo $currProj; ?>','<?php echo $prevID; ?>')" onclick="<?php $currProj=$prevID; ?>"> < Previous </a>
但是,看起来您对这样做感兴趣,而不必再次ping服务器。在这种情况下,您需要在javascript中存储、引用和更新这些变量。可能有一千种方法可以做到这一点;最接近您想要做的事情是让您的
showProject
函数不带任何参数,而是根据当前项目的值确定它需要做什么,比如

var currProj = <?php echo $currProj; ?>; // this initializes the JS variable currProj from whatever it is in PHP when the server sends the page contents
var countposts = <?php echo $countposts; ?>; // initialize this too
var showPrevProject = function showPrevProject() { 
  // hide the current project using jQuery; assumes the DOM element you want to hide is given ID #project-[currProj]
  $('#project-' + currProj).hide();

  // Update the current project variable in JS; scope should update globally
  currProj = currProj - 1;
  if (currProj === 0) { currProj = countposts; } 

  // Now show that element
  $('#project-' + currProj).show();
}
var currProj=;//这将在服务器发送页面内容时从PHP中的任何变量初始化JS变量currProj
var countposts=;//也初始化这个
var showPrevProject=函数showPrevProject(){
//使用jQuery隐藏当前项目;假设要隐藏的DOM元素已给定ID#project-[currProj]
$(“#项目-”+currProj.hide();
//在JS中更新当前项目变量;范围应全局更新
currProj=currProj-1;
如果(currProj==0){currProj=countposts;}
//现在显示该元素
$(“#项目-”+currProj.show();
}
然后在链接中,您可以根据需要使用
showNextProject
showPrevProject


不过,一般来说,最好将javascript作为单独的文件包含,并注册事件处理程序来处理这类事情。我还建议您签出,这是一个功能强大的库,可以极大地简化对DOM元素的访问和操作。

onclick=“”
您想比较一下吗?还是你只是在分配?在这两种情况下,您都需要输入
echo
onclick=“”
Hi!很抱歉回复太晚-我不得不暂时搁置这个项目,但我现在又回来了。非常感谢你的帮助!你所说的和逻辑对我来说很有意义,但是我似乎在实现它时遇到了困难。我肯定我忽略了一些愚蠢的事情。我使用了您上面给出的代码,但是它给出了一个错误“SyntaxError:expected expression,get';'var currProj=;”,因此它似乎没有传递$currProj变量。其次,我不知道如何在我的链接中传递那些“showPrevProject”/“showNextProject”变量。@annie-d要传递javascript函数,请尝试在锚标记中使用
onclick=“showNextProject()”
(参见示例)。对于
currProj
问题,在JS中运行赋值时,PHP中是否定义了变量(
var currProj=;
)?
$currProj = ( $_REQUEST['currProj'] ? $_REQUEST['currProj'] : 1 );
...
<a class="btn-previous" href="<?php echo '[your_url_here]?currProj=' . $prevID ?>">
var currProj = <?php echo $currProj; ?>; // this initializes the JS variable currProj from whatever it is in PHP when the server sends the page contents
var countposts = <?php echo $countposts; ?>; // initialize this too
var showPrevProject = function showPrevProject() { 
  // hide the current project using jQuery; assumes the DOM element you want to hide is given ID #project-[currProj]
  $('#project-' + currProj).hide();

  // Update the current project variable in JS; scope should update globally
  currProj = currProj - 1;
  if (currProj === 0) { currProj = countposts; } 

  // Now show that element
  $('#project-' + currProj).show();
}