SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: using a for loop
-
May 25, 2008, 07:12 #1
- Join Date
- Jul 2007
- Location
- London
- Posts
- 161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
using a for loop
hello all,
basically i have a script that i'm modifying and i'm trying to list a couple urls dynamycally.
the format i need is;
page=["pg/bg.php?usr=13&pageno=1", "pg/bg.php?usr=13&pageno=2", "pg/bg.php?usr=13&pageno=3", "pg/bg.php?usr=13&pageno=4"]
As you can see its just seperating each url with a comma and each url in quotes "". The only thing that changes is the pageno. I have the total number of pages in a php variable so i thought i could use a for loop like the following
Code JavaScript:<script type="text/javascript"> for (i = 0; i <= <?php echo $pgno; ?>; i++) { document.write('"') document.write("pg/bg.php?usr=<?php echo $usr; ?>&pageno="+i) document.write('",') } </script>
On its own the above prints out everything the way i want it to but I don't know how to use the for loop when doing page=
in other words i don't know how to put the urls into page since its a loop am almost sure i can't just go page= or (i = 0; i <= <?php echo $pgno; ?>; i++)....etc
Not sure if am making sense, hopefully i am and any help would be greatly appreciated.
Thanks
-
May 25, 2008, 08:31 #2
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am assuming you mean that you can't guarentee that $pgno will have a value? if thats the cause, then you can simply do some simple validation. If you don't get a value, set it to a default value, otherwise use the value given.
ie
Code php:<%php $pgno = isset($_GET['pgno']) ? $_GET['pgno'] : 1; %> <script type="text/javascript"> for (i = 0; i <= <?php echo $pgno; ?>; i++) { document.write('"') document.write("pg/bg.php?usr=<?php echo $usr; ?>&pageno="+i) document.write('",') } </script>
-
May 25, 2008, 09:37 #3
- Join Date
- Jul 2007
- Location
- London
- Posts
- 161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no sorry, i didn't think i was making much sense. so am gonna try again.
what i have is
<script type="text/javascript">
var myajaxbook={} //arbitrary variable to hold page settings for this book
myajaxbook.page=[
for (i = 0; i <= <?php echo $pgno; ?>; i++)
{
"pg/bg.php?usr=<?php echo $usr; ?>&pageno="
}
]
//this is an example of what works without trying anything fancy and setting urls by hand
//the bit in red above is what am trying to change from the bit below thats in red
myajaxbook.selectedpage=0 //set page shown by default (0=1st page)
var myajaxbookalt={} //arbitrary variable to hold page settings for another book
myajaxbookalt.page=["pg/demofiles/external.htm", "pg/demofiles/external2.htm", "pg/demofiles/external3.htm"]
myajaxbookalt.selectedpage=0 //set page shown by default (0=1st page)
var mypages=new ajaxpageclass.bindpages(myajaxbook, "pcontent", ["paginate-top", "paginate-bottom"])
</script>
-
May 25, 2008, 16:11 #4
- Join Date
- Jan 2007
- Location
- Christchurch, New Zealand
- Posts
- 14,729
- Mentioned
- 104 Post(s)
- Tagged
- 4 Thread(s)
The best way is to place each string into a php array, then join them together with a comma-space separator.
You should take this question over the the PHP forum instead.
http://www.sitepoint.com/forums/forumdisplay.php?f=34Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
Bookmarks