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?
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.
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.
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.
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.