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. However, I've stumbled upon a problem with having php loops loaded through ajax. I've looked up a script for making dynamic pages possible: 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.
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. 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
I don't believe you can use a for loop as a value for a case statement. See the result below:
Without seeing your real code and knowing your real objectives, my best advice is to preprocess your loops before entering that switch statement.
Perhaps the issue ins't trying to execute a loop within a PHP case.
It appears that you are using the $_GET['page'] call to grab the value from the QueryString. This would work if you defined "page" in the URL syntax (ex: 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. This means none of the cases in your switch statement are executing because they don't match the null value returned by the $_GET.
Try parsing the query string to get whatever is after the "#" into the parameter you use in your case statement.
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;};
does nothing is $page
is null. Try
case 'page1' : $page = 'page'; for ($i=0;$i<2;$i++){$page .= $i;};
When I put your loop into a function:
function testthis()
{
for ($i=0;$i<2;$i++){$page .= $i;};
}
It throws an error: "Notice: Undefined variable: page in demo.php"
When I declare $page outside of the loop - just put in the line: $page = ""; it works - no error.
So - my recommendation - declare $page outside the loop and set it to an empty string "". 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.