Jump to content

Recommended Posts

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.