Jump to content

Is this possible? While echo $i textarea1 - echo $y textarea2 endwhile;


soma56

Recommended Posts

I have a script where I need to be able to work with the echoed contents afterwards. The problem is that everything is being echoed during a loop in two different areas. When I highlight or otherwise copy the contents - it highlights everything.

 

<?PHP
$i = 0;
$y = 0;

while ($i < 50){
//INSTANCE OF IRRELAVANT OUTPUT
include_once ("textareabegin.php");
echo $i;
echo "<br />";
    $i++;
ob_flush();
flush();
usleep (100000);
include_once ("textareaend.php");

//OUTPUT THAT I NEED
include_once ("2ndtextarebegin.php");
echo $y;
$y = $y + 100; 
ob_flush();
flush();
usleep (100000);
include_once ("2ndtextareend.php");

}
?>

 

What I'm trying to do is echo all instances of $i in one textarea and all instances of $y in an another. This way when the script is complete I can simply highlight $y to work with it.

 

The include_once seemed like the logical way to go. Here's a sample of the includes that I have for the above:

///textareabegin
echo "<textarea name=\"test_textarea\" cols=\"50\" rows=\"6\">";

 

///textareaend
echo "<textarea name=\"test_textarea\" cols=\"50\" rows=\"6\">";

 

Although the textarea only shows up once the loop continues to echo outside the textarea. So if I've made any sense what I'm ultimately trying to accomplish is "how can I echo out $i in textarea1 and $y in textarea2 while they're both in a loop?

I think I understand what you are trying to do but I suspect you will say if not.  You want two text areas side-by-side, one showing $i and the other $y.  If so you need two loops, see below:

 

<?php

$i = 0;
$y = 0;

echo "<textarea name='test_textarea' cols='50' rows='6'>";  
while ($i < 50){
echo $i;
echo "\n";
    $i++;
ob_flush();
flush();
usleep (100000);
}
echo "</textarea>";

echo "<textarea name='test_textarea' cols='50' rows='6'>";
$i = 0;
while ($i < 50){
echo $y;
echo "\n";
$y = $y + 100; 
ob_flush();
flush();
usleep (100000);
$i++;
}
echo "</textarea>";

?>

 

Hope it helps!  :)

You really don't need any loops for this:

<?php
$numbers = implode("\n",range(0,49));
echo "<textarea name='textarea1' cols='6' rows='50'>$numbers</textarea><br>\n";
echo "<textarea name='textarea2' cols='6' rows='50'>$numbers</textarea><br>\n";
?>

or with a small loop:

<?php
$numbers = implode("\n",range(0,49));
for ($i=1;$i<3;++$i) {
     echo "<textarea name='textarea$i' cols='6' rows='50'>$numbers</textarea><br>\n";
}
?>

 

Ken

What kenrbnsn said is also true but with that method I think you will have less control over anything else you want to do with the data inside the text areas at the same time of outputting them.  Can you explain why you want them in one loop, what is it that you intend to do with the textareas that makes you think one loop is needed?  :shrug:

Thanks Ken.

 

What kenrbnsn said is also true but with that method I think you will have less control over anything else you want to do with the data inside the text areas at the same time of outputting them.  Can you explain why you want them in one loop, what is it that you intend to do with the textareas that makes you think one loop is needed?  :shrug:

 

I'd love to post the code but it's a mess. The reason I need a loop is because if certain conditions aren't met then I need to change a few values and run through that section again. Ultimately, of the two textareas, one is the status and the second is the results. It's the results that I want to give users the ability to highlight and use afterwards - however the nature of how it's being echoed out now:

 

Loop -> Status, Results, Status, Results, Status, Results <-End Loop

 

...gives me multiple textareas which makes it difficult for users to use afterwards.

 

It would be nice to be able to loop and ehco these results into only two textareas: one for the status and one for the results.

 

I'm going to try Kens suggestion. I really appreciate the insight from both of you!

Just as a thought:  instead of using the loop to echo out your numbers, you could use the loop to instead store the data in an array.

 

From what my understanding is, you need to do some working so that you can adjust numbers accordingly before they are echo'd, but you need two textareas (one for status one for report), which is where the two loops come in to it.  This basically means your "two loops" are required because you have to echo out each textarea individually.

 

If you run through your loop and store the data into an array, this may give you an option to run one loop to do whatever it is you need first.  The only problem here is you would either need to use Ken's method to "implode" the data into one thing to echo it out, or you'd have to do two more loops to echo them into the text boxes.

 

To be honest, I'm still not 100% sure what it is we are trying to achieve in the end, which is why I haven't written any code... but if this idea helps.. great.. if not... good luck :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.