mysql - Php Str_replace with a for loop -
i'm trying give each textarea name can use them later send database. code got weird results , guess because use str_replace.
here's 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 database xa1xb2xcxd3xe4xcxa5xb6xc(description2)
as can see i'm trying loop , give value 1 $totalteknisk.
i'm open suggestions on how can make work.
i solved bit differently, using preg_replace_callback(...)
$description2 = 'xa1xb2xcxd3xe4xcxa5xb6xc'; $html = preg_replace_callback('/x(?:a|b|c|d|e)/', function($match) { static $count; $count++; switch($match[0]) { case 'xa': return "<textarea name=\"$count\" style=\"background-color:#ffffff;resize: none; height: 20px; width: 200px;\">"; case 'xb': return "</textarea><textarea name=\"$count\" style=\"background-color:#ffffff;resize: none; height: 20px; width: 200px;\">"; case 'xc': return "</textarea><br />"; case 'xd': return "<textarea name=\"$count\" style=\"background-color:#eaf2d3;resize: none; height: 20px; width: 200px;\">"; case 'xe': return "</textarea><textarea name=\"$count\" style=\"background-color:#eaf2d3;resize: none; height: 20px; width: 200px;\">"; } }, $description2); $teknisk .= $html; i suggest, dont use numbers 'name' attribute in textareas. may should consider using name, fields[$count] , refer $_post['fields'] ...
Comments
Post a Comment