简体   繁体   English

使用ajax动态内容时,PHP循环不起作用

[英]PHP Loops not working while using ajax for dynamic content

I've recently tried to turn a website of mine a bit more dynamic. 我最近尝试使我的网站更具活力。 It's heavily based on php and I tried looking into having pages changed dynamically with ajax. 它很大程度上基于php,我尝试使用ajax动态更改页面。 However, I've stumbled upon a problem with having php loops loaded through ajax. 但是,我偶然发现了通过ajax加载php循环的问题。 I've looked up a script for making dynamic pages possible: http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp 我查找了使动态页面成为可能的脚本: http : //www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp

switch($_GET['page'])  {
case 'page1' : $page = 'Page 1';
                break;
case 'page2' : $page = 'Page 2';
                break;
case 'page3' : $page = 'Page 3';
                break;
case 'page4' : $page = 'Page 4';
                break;
}
echo $page;

For example, if you change 例如,如果您更改

 case 'page1' : $page = 'Page 1';

into a loop 陷入循环

 case 'page1' : for ($i=0;$i<2;$i++){$page .= $i;};

it just doesn't do anything. 它什么也没做。 Any ideas? 有任何想法吗? :) :)

EDIT: I'm expecting it to output what the given loop normally outputs. 编辑:我期望它输出给定循环通常输出的内容。 There seems to be conflict of some sort when it's being dealt with. 处理它时似乎存在某种冲突。 The posted code without loop works as intended, but when you bring php loops into play it won't work. 所发布的无循环代码按预期工作,但是当您使php循环生效时,它将无法工作。

EDIT2: I've pinpointed the problem to be the loops, so I don't think it is necessary to bring out the code I'm using. EDIT2:我已经确定问题是循环,所以我认为没有必要带出我正在使用的代码。 I just put a simple loop there as an example. 我仅以一个简单的循环为例。 The code is just basically looping through data entries in database and outputting them. 该代码基本上只是遍历数据库中的数据条目并输出它们。 I'd love if someone could point out why this does not work and if there is a work-around. 如果有人可以指出为什么这行不通,并且有解决方法,我很乐意。 :P :P

I don't believe you can use a for loop as a value for a case statement. 我不认为您可以将for循环用作case语句的值。 See the result below: 看到下面的结果:

http://ideone.com/GXXMs http://ideone.com/GXXMs

Without seeing your real code and knowing your real objectives, my best advice is to preprocess your loops before entering that switch statement. 最好的建议是在看不到实际代码和实际目标的情况下, 输入switch语句之前对循环进行预处理。

Perhaps the issue ins't trying to execute a loop within a PHP case. 也许问题在于没有试图在PHP案例中执行循环。

It appears that you are using the $_GET['page'] call to grab the value from the QueryString. 看来您正在使用$ _GET ['page']调用从QueryString中获取值。 This would work if you defined "page" in the URL syntax (ex: foo.php?page=page1). 如果您在URL语法中定义了“页面”(例如:foo.php?page = page1),这将起作用。 In this example, you never define the variable called "page" in the query string, instead just throwing the #page1 at the end of the URL. 在此示例中,您永远不会在查询字符串中定义名为“ page”的变量,而只是在URL末尾抛出#page1。 This means none of the cases in your switch statement are executing because they don't match the null value returned by the $_GET. 这意味着switch语句中没有任何一种情况正在执行,因为它们与$ _GET返回的空值不匹配。

Try parsing the query string to get whatever is after the "#" into the parameter you use in your case statement. 尝试解析查询字符串,以获取在case语句中使用的参数中“#”之后的内容。

Use regular expression replace 使用正则表达式替换

echo preg_replace('/page([0-9]+)/i', 'Page $1', $_GET['page']);

The reason case 'page1' : for ($i=0;$i<2;$i++){$page .= $i;}; 原因case 'page1' : for ($i=0;$i<2;$i++){$page .= $i;}; does nothing is $page is null. 什么都不做是$page为空。 Try 尝试

case 'page1' : $page = 'page'; for ($i=0;$i<2;$i++){$page .= $i;};

When I put your loop into a function: 当我将循环放入函数中时:

function testthis() 函数testthis()
{ {
for ($i=0;$i<2;$i++){$page .= $i;}; for($ i = 0; $ i <2; $ i ++){$ page。= $ i;};
} }

It throws an error: "Notice: Undefined variable: page in demo.php" 它将引发错误:“注意:未定义的变量:demo.php中的页面”

When I declare $page outside of the loop - just put in the line: $page = ""; 当我在循环外声明$ page时,只需将其放在行中:$ page =“”; it works - no error. 它有效-没有错误。

So - my recommendation - declare $page outside the loop and set it to an empty string "". 所以-我的建议-在循环外声明$ page并将其设置为空字符串“”。 See if that fixes the issue. 查看是否可以解决问题。

The root cause seems to be that you're concatenating a string to a variable that has not yet been declared. 根本原因似乎是您将字符串连接到尚未声明的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM