I’m trying to give each textarea a name so I can use them later to send back to the database.
With this code I got some weird results and I guess its because I use str_replace.
Here’s the code:
$description2 = mysql_result($product, 0, 'productDescription2');
$totalteknisk = preg_match_all('/x(a|b|d|e)/', $description2, $matches);
$searchArray = array('xa', 'xb', 'xc', 'xd', 'xe');
if ($description2 !=""){
for($z=1;$z <= $totalteknisk; $z++){
$xa = '<textarea name="'. $z .'" style="background-color:#FFFFFF;resize: none; height: 20px; width: 200px;">';
$z++;
$xb ='</textarea><textarea name="'. $z .'" style="background-color:#FFFFFF;resize: none; height: 20px; width: 200px;">';
$z++;
$xc = '</textarea><br>';
$xd = '<textarea name="'. $z .'" style="background-color:#EAF2D3;resize: none; height: 20px; width: 200px;">';
$z++;
$xe = '</textarea><textarea name="'. $z .'" style="background-color:#EAF2D3;resize: none; height: 20px; width: 200px;">';
$replaceArray = array($xa, $xb, $xc, $xd, $xe);
$teknisk .= str_replace($searchArray, $replaceArray, $description2);
}
}
Example string from database xa1xb2xcxd3xe4xcxa5xb6xc(description2)
As you can see I’m trying to just loop it all and give it a value 1 to $totalteknisk.
I’m open for suggestions on how I can make this work.
I solved it a bit differently, by using preg_replace_callback(…)
I would also suggest, that you dont use only numbers for the ‘name’ attribute in the textareas. You may should consider using another name, like
fields[$count]and then refer to it by$_POST['fields'] ...