Cobby Posted June 19, 2007 Share Posted June 19, 2007 Hi all, How do I get a loop to output an array. If $i was any random value. for example, if $i was 4 ... $i = '4'; ... then somehow, it would autmatically output the following array $params[page1]; $params[page2]; $params[page3]; $params[page4]; Obivously, I would want the number page# to automatically increase by 1. Cheers, Cobby Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/ Share on other sites More sharing options...
hackerkts Posted June 19, 2007 Share Posted June 19, 2007 Hmm.. I don't understand what you mean. Maybe you could try +1 at the back. Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277324 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 $i = 0; $arraysize = 100; while ($i < $arraysize) { $params[page$i] = $i; $i++ } something like that Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277326 Share on other sites More sharing options...
neel_basu Posted June 19, 2007 Share Posted June 19, 2007 I Didn't understand anything from your Post. Please explain more. Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277338 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 tell me what $params[pageN] are equal to and I can show you it like $params[page1] = "index"; $params[page2] = "users"; Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277342 Share on other sites More sharing options...
Cobby Posted June 19, 2007 Author Share Posted June 19, 2007 I knew I didn't explain it too well. Its for a pagination system. Each time the writer creates a certain tag (smarty template engine, similar to an include/require), it will get it to add 1 to $i. And for each $i, I need to output an array. $params['pageN']; .... N should start at one, and finish when its equal to $i. So if $i is set to 3, it will make an array that looks like: $params['page1']; $params['page2']; $params['page3']; To understand the $params['pageN'] stuff, have a look here -> http://wiki.cmsmadesimple.org/index.php/User_Handbook/Admin_Panel/Extensions/User_Defined_Tags Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277433 Share on other sites More sharing options...
Cobby Posted June 19, 2007 Author Share Posted June 19, 2007 tell me what $params[pageN] are equal to and I can show you it like $params[page1] = "index"; $params[page2] = "users"; The value of each $param['pageN'] will be set by the user, see the link in my post above. Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277437 Share on other sites More sharing options...
Cobby Posted June 19, 2007 Author Share Posted June 19, 2007 Thanks for the help everyone, I am now getting somewhere. Ill just quickly explain the smarty template engine. On any of my pages, I can add for example: .... {hi} .... And it will search where every smarty stores its tags (either database or file) and output what every is in it. So if I made a tag called {hi}, and its contents was echo "Hello World"; Anywhere in a page, if I put {hi}, it would output Hello World. With the $params thing. If the contents of {hi} was: echo "Hello World! My name is ".$params['myname']; Anywhere on a page, if I put .... {hi myname="Cobby"} .... it would output Hello World! My name is Cobby So $params is just a special variable so that someone without programming knowledge can have some input on the code easily. So now thats out of the way. I am making a pagination system for long articles. Here are the following tags I have created: {i} $i = "0"; {page] echo "<div class=\"virtualpage5\">"; $i++; {qpage} echo "</div>"; {pagination} echo " <div id=\"galleryselect\" class=\"paginationstyle\"> <a href=\"#\" rel=\"previous\">Prev</a> <select style=\"width: 120px\"></select> <a href=\"#\" rel=\"next\">Next</a> </div> <script type=\"text/javascript\"> var gallery=new virtualpaginate(\"virtualpage5\", 1) gallery.buildpagination(\"galleryselect\", [ "; echo $page['1']; echo " ]) </script> "; So the at the start, the user will put {i}. Since PHP is a loosely typed language, a variable doesn't need to be set before you use it, but I wasn't sure if it was affected, so I just added that (I will add an if statement to {page}). Then, the content for each page will go between the {page} and {qpage} tags. Then at the bottom, add the {pagination} tag. So the finally code will look like this: {i} {page} Page 1. {qpage} {page} Page 2. {qpage} {page} Page 3 {qpage} {page} Page 4 {qpage} {pagination} But the actually output code from smarty will be: $i = "0"; echo "<div class=\"virtualpage5\">"; $i++; Page 1 echo "</div>"; echo "<div class=\"virtualpage5\">"; $i++; Page 2 echo "</div>"; echo "<div class=\"virtualpage5\">"; $i++; Page 3 echo "</div>"; $a = "0"; while ($a!=$i){ $page[$a] = "\"".$params[$a]."\", "; $a++; } echo " <div id=\"galleryselect\" class=\"paginationstyle\"> <a href=\"#\" rel=\"previous\">Prev</a> <select style=\"width: 120px\"></select> <a href=\"#\" rel=\"next\">Next</a> </div> <script type=\"text/javascript\"> var gallery=new virtualpaginate(\"virtualpage5\", 1) gallery.buildpagination(\"galleryselect\", [ "; echo $page['1']; echo " ]) </script> "; There is a javascript include at the start of the page, but you don't need to see it. To see the result of that, go to -> http://www.diycomputing.com.au/index.php?page=pagination But the only thing it, I want the user to be able to specify the name of the each page in the drop down box. Cheers, Cobby Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277448 Share on other sites More sharing options...
Cobby Posted June 19, 2007 Author Share Posted June 19, 2007 For some reason, I can't edit my previous post. I made a mistake in {pagination}, it goes as follows: echo " <div id=\"galleryselect\" class=\"paginationstyle\"> <a href=\"#\" rel=\"previous\">Prev</a> <select style=\"width: 120px\"></select> <a href=\"#\" rel=\"next\">Next</a> </div> <script type=\"text/javascript\"> var gallery=new virtualpaginate(\"virtualpage5\", 1) gallery.buildpagination(\"galleryselect\", [ "; //I need the array to output here, its needs to have correct javascript syntax, so it will be-> \"".$params['pageN']."\", echo " ]) </script> "; If you want to know more about the actually javascript pagination stuff, it can be found at -> http://www.dynamicdrive.com/dynamicindex17/virtualpagination.htm. I am using 'Demo 5'. EDIT: WOW! Forgot to mention this, I will use the $params['pageN'] so that users can name each drop down menu, so for the pagination tag, the will put in: {pagination page1="Page 1- Intro" page2="Page 2 - Overview" ...etc, etc. Cheers, Cobby Link to comment https://forums.phpfreaks.com/topic/56146-loop-to-output-arrays/#findComment-277457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.