How to save php loop into variable?

Hi,

i have a for-loop in php which looks like this:

for ($col = 0; $col < 1700; $col++) {
     $outa = $options['custom_reg_fields'][$col]; 
     echo $outa; // string without <br>\ntest | Test
     if($col == 1124){
         $outb = '<br>\ntest | Test';
         echo $outb; // just <br>\ntest | Test
     }
   }

I do get something like this as output on my page:

\n123456 | Number 1
\n789101 | Number 2
\n112131 | Number 3
\ntest | Test

Each character comes up when i go trough the loop. But what i need, is to save this whole output into a variable. At the moment i do have two variables $outa and $outb. When i do e.g. $outc = $outa . $outb;, im just getting weird outputs. Has anybody an idea how to solve this?

Thanks in advance!

$outall = '';
for ($col = 0; $col &lt; 1700; $col++) {
     $outa = $options['custom_reg_fields'][$col]; 
     echo $outa; // string without &lt;br&gt;\ntest | Test
     $outall .= $outa;   // add the content of $outa to $outall
     if($col == 1124){
         $outb = '&lt;br&gt;\ntest | Test';
         echo $outb; // just &lt;br&gt;\ntest | Test
         $outall .= $outb;  // add the content of $outb to $outall
     }
}
echo $outall;
1 Like

Thanks, this works great!

But how can i fix my code so it does not overwrite the last entry? I am dynamically inserting some values in $outb. And i would like that each time i insert a new $outb, it comes a new row in my output.

Isn’t that what $outall does in Darths code? That contains all the output.

Yes that’s right. But it overwrites that output if I insert a new $outb.E.g.:

With the code of @DarthGuido my $outall looks like this:

\n123456 | Number 1
\n789101 | Number 2
\n112131 | Number 3
\ntest | Test

That’s working fine if i just have one thing to insert into $outb. I if want to insert someting else for each iteration of the loop, then i just get the last value for $outall. I would like to have it like this:

\n123456 | Number 1
\n789101 | Number 2
\n112131 | Number 3
\ntest | Test
\ntest1 | Test2
\ntest2 | Test3

So that it’s inserting somethting and not overwriting it.

Show your code after you’ve inserted a new $outb - I can’t really picture what you mean.

Here is my function:

    global $wpdb;
    $table_name = $wpdb->prefix . 'table_name';
    $id = "SELECT MAX(id) FROM ". $table_name;
    $name = "SELECT name FROM ". $table_name ." WHERE id=". $wpdb->get_var($id);
    
    $options = get_option('ws_plugin__s2member_options');

    $outall = '';
    for ($col = 0; $col < 1700; $col++) {
        $outa = $options['custom_reg_fields'][$col];
        $outall .= $outa;
        if ($col == 1124) {
            $outb = '<br>\n'. $wpdb->get_var($id) .' | '. $wpdb->get_var($name); // here it will insert a new id and a new name when it will be inserted something new to the database-table
            $outall .= $outb;
            
        }
    }

    //echo $outall;

    foreach ($options as $k => &$v) {
        if ($k == "custom_reg_fields") {
            $v = $outall;
        }
    }

    echo '<br><br>';

    print_r($options['custom_reg_fields']);

The problem is, that i’m just getting the last id and name because this is overwriting it. But i would like to keep the old ones and add a new row in my output each time something new is added to the database-table.

Thanks for your help!

I’m still not quite getting what the problem is. Your code to create $outb creates it once, if the $col variable is equal to 1124. Are you saying that you will have another if() for another value of $col, and want to append that to $outb? If so, in the second if(), use the append .= rather than the assignment = for $outb.

I’m sure it’s really clear to you what you mean, but I’m just not getting it.

1 Like

Me neither

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.