Jump to content


Photo

To write a looping function


  • Please log in to reply
16 replies to this topic

#1 manianprasanna

manianprasanna
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationChennai

Posted 07 September 2006 - 10:53 AM

I would like to write a looping function. for example as below

loop(10) {
    echo 'Please answer me';
}

So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?

Please help me.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 September 2006 - 10:56 AM

You should read this:
PHP and looping

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 11:01 AM

This:

<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}
loop(10, "Please answer me");

?>

Should work.

#4 manianprasanna

manianprasanna
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationChennai

Posted 07 September 2006 - 11:24 AM

Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code

loop(10) {
    for($i=0; $i<5; $i++) {
        echo 'please help me';
    }
}

The same loop function should also be able to use like below

loop(10) {
    if($i==8) {
        echo 'please help me';
    }
}

I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function.

#5 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 11:29 AM

Where are you getting "function" from? I don't see any where in your code where you define a function.

#6 manianprasanna

manianprasanna
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationChennai

Posted 07 September 2006 - 11:37 AM

Yes that's what i would like to know that only., that is how to define the function loop to accomplish the functionality said in my previous post.

Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code

loop(10) {
    for($i=0; $i<5; $i++) {
        echo 'please help me';
    }
}


The same loop function should also be able to use like below


loop(10) {
    if($i==8) {
        echo 'please help me';
    }
}

I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function.



#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 11:43 AM

Can't you see looking at the code I posted how to define a function? I don't see why the code I posted won't work for you?

<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}
loop(10, "Please answer me"); //First declare the amount of times you want the text within the parenthesis to be displayed. Then within the parenthesis declare the text you want displayed.

?>

Maybe I am totally mis-understanding you, if that is the case, I applogize. Maybe you can be more specific?

#8 jsimmons

jsimmons
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 September 2006 - 12:09 PM

I would like to write a looping function. for example as below

loop(10) {
    echo 'Please answer me';
}

So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?

Please help me.


Here's a looping function to do what you specified:

<?php
function loop($iterations)
{
for ($i = 1; $i <= $iterations; $i++)
{
echo "Please answer me";
}
}

// usage...
loop(10);
?>

The output on a web page will look like this:

Please answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer me

I'm almost sure that's not what you want, but hey, who am I to judge?  I now re-quote you:

Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code.


How are we supposed to devine the special knowledge we need to write your code for you? 



#9 manianprasanna

manianprasanna
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationChennai

Posted 07 September 2006 - 12:26 PM

Thanks for all those who replied.

I would like to tell one more thing. The statemets inside the looping function is not same in all the case. I can be anything like conditional statements, DB manupulations, etrc....The loop function should take those statements as input and loop it.

case 1:
loop(10) {
    //some set of statements.
}

case 2:
loop(10) {
    //Not the same set of statements as in case 1.
}



#10 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 12:30 PM

With my function, you can change what is looped and how many times, for example:

<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}

loop(10, "Please answer me"); //Please answer me - looped 10 times

loop(8, "Hello, how are you"); //Hello, how are you - looped 8 times

//You could apply statements like:

$test = "8";

if ($test == "8") {
loop(5, "Test equals 8");
}

?>


#11 jsimmons

jsimmons
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 September 2006 - 12:39 PM

Thanks for all those who replied.


You're not a programmer, are ya...  :/

Hostfreak has given you the answer (twice).



#12 manianprasanna

manianprasanna
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationChennai

Posted 07 September 2006 - 12:52 PM

Again thankyou hostfreak. But you had changed my function calling from loop(10) to loop(10, "Please answer me"). First i don't like to have another parameter (your 2nd parameter). And i would like to execute any statements inside the calling loop function.

Example 1:
loop(10) {
    ////MySQL insert statements.
}

Example 2:
loop(10) {
    ////Some simple echo statemets.
}

Example 2:
loop(10) {
    ////Some file manupulations.
}

Sorry if i have hurted any body..
Please reply me.

With my function, you can change what is looped and how many times, for example:

<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}

loop(10, "Please answer me"); //Please answer me - looped 10 times

loop(8, "Hello, how are you"); //Hello, how are you - looped 8 times

//You could apply statements like:

$test = "8";

if ($test == "8") {
loop(5, "Test equals 8");
}

?>



#13 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 07 September 2006 - 01:01 PM

Hmmm , i think you need to hacking PHP code to do something like that , becuase it's a statement and not function .

you can do it with PHP by compile it , i mean write function open the php source file and search the "loop" statement in the php source file , if find the statement replace it by real loop statement like "while" or "for" , i mean do something like templates engine (for example Smarty) .
Sorry for my bad English, it's not my mother language and I am still learning it :)

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 01:58 PM

Can you provide proper examples please. Posting
loop(10) {
    ////MySQL insert statements.
}

loop(10) {
    ////Some simple echo statemets.
}

loop(10) {
    ////Some file manupulations.
}
Doesnt help much.

A good example will be to post the code you want to be repeated over and over in your loop function.

If you dont want a secound paramter, then prehaps use global:
function loop($times)
{
    global $text;

    for ($i = 0; $i < $times; $i++)
    {
        echo "$text<br />";
    }
}

$text = "Please answer me";
loop(10); //Please answer me - looped 10 times

$text =  "Hello, how are you";
loop(8); //Hello, how are you - looped 8 times

Also you cannot use a function to do three different things, without having more than 1 parameter. How will the loop function know you want to run a query, or manupulate a file, echo something etc. I cannot see where you are going with this.

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 September 2006 - 02:20 PM

The OP doesn't want a function, he wants a new statement type.

PHP already has looping statements: for, while, do ... while. Why can't one of those be used?

Example:

<?php
for($i=0;$i<10;$i++) {
//
//  code block to be executed 10 times
//
}?>

Ken

#16 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 02:27 PM

The OP doesn't want a function, she wants a new statement type.

PHP already has looping statements: for, while, do ... while. Why can't one of those be used?

Example:

<?php
for($i=0;$i<10;$i++) {
//
//  code block to be executed 10 times
//
}?>

Ken


She? Lol, his profile would suggest otherwise.

#17 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 07 September 2006 - 05:14 PM

The OP appears to want a general function that loops but has a different body depending on from where it's called.

I'm guessing their intended use is:
// get DB items
$items = mysql_query($sql);
// Loop over items and do something
$results = Loop($items);
// Loop over the results and do something else entirely
$new_results = Loop($results);

To the OP, if this is what you want, sorry you can't do that.  You'll just have to write a new function for each type of looping functionality you want to provide.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users