[pmwiki-devel] ZAPcart configurability [was: Stumped on string insertions...]

J. Meijer commentgg at hotmail.com
Sun Apr 8 11:29:38 CDT 2007


On 4/7/07, The Editor <editor at fast.st> wrote:Hi all!...2) I'm still trying to make it more configurable by setting a fewvariables like the following as SDV variables.  But can't figure outhow to get the $vars to substitute with their value when used in thefunction.  Here's a repost of my question on the topic yesterday.> SDV($ZAPorderrows, '<tr><td> nowrap>$itemname </td><td><input type=text size=4> name=$itemlabel value=$itemqty> onblur=calculate()> at</td><td> align=right>$itemprice </td><td><input type=text size=7> name=$itemlabel value=$itemcost onblur=calculate()> onFocus=blur()></td></tr>');>> Then somewhere in the markup for the form I want to be able to do> something like this (within a foreach loop):>> $orderrows .= $ZAPorderrows;>> However I need to be able to replace the variables like $itemname and> $itemlabel with their current value at that time.  I can't seem to get> the right combination of ' " etc to make this work.  Can someone> suggest something?Cheers,DanHi Dan, Your intuition is apparently bugging you, like this: there must be a way to invoke "string $var substitutions" on a string-variable, not just on literal strings. Well the closest thing (AFAIK) is strtr() (http://www.php.net/manual/en/function.strtr.php). However, since you're performance conscious, I did some experimenting just to see how various alternatives would fare. See the code below. It turns out that str_replace outruns everything by far. Strtr() is 4 times slower(!) and using eval() to invoke "string $var substitutions" is 7..8x slower. Patrick at some point probably did the same profiling. A similar problem occurs in FmtPageName(), he uses:    $fmt = str_replace(array_keys($g),array_values($g),$fmt);
Which is also your best bet. -Keep that mindstorm going!/jmps On the topic of the formula to calculate shipping etc: you could possibly output it directly from the cart markup, custom made. ============================<?php $EnableStopWatch=1;function StopWatch($x) {   global $StopWatch, $EnableStopWatch;  if (!$EnableStopWatch) return;  static $wstart = 0, $ustart = 0;  list($usec,$sec) = explode(' ',microtime());  $wtime = ($sec+$usec);   if (!$wstart) $wstart = $wtime;  if ($EnableStopWatch != 2)     { $StopWatch[] = sprintf("%05.2f %s", $wtime-$wstart, $x); return; }  $dat = getrusage();  $utime = ($dat['ru_utime.tv_sec']+$dat['ru_utime.tv_usec']/1000000);  if (!$ustart) $ustart=$utime;  $StopWatch[] =     sprintf("%05.2f %05.2f %s", $wtime-$wstart, $utime-$ustart, $x);}function Expand(){  global $StopWatch;  $g=array('itemname'=>'Easter Egg', 'itemprice'=>'7,00', 'itemqty'=>'12', 'itemlabel'=>'Customer Delusion', 'itemcost'=>'2 pennies');  $fmtrep='<tr><td> nowrap>$itemname </td><td><input type=text size=4> name=$itemlabel value=$itemqty> onblur=calculate()> at</td><td> align=right>$itemprice </td><td><input type=text size=7> name=$itemlabel value=$itemcost onblur=calculate()> onFocus=blur()></td></tr>';  $fmtrep2='<tr><td> nowrap>$itemname </td><td><input type=text size=4> name=$itemlabel value=$itemqty> onblur=calculate()> at</td><td> align=right>$itemprice </td><td><input type=text size=7> name=$itemlabel value=$itemcost onblur=calculate()> onFocus=blur()></td></tr>';  $fmteval='return "<tr><td> nowrap>$$g[itemname] </td><td><input type=text size=4> name=$$g[itemlabel] value=$$g[itemqty]> onblur=calculate()> at</td><td> align=right>$$g[itemprice] </td><td><input type=text size=7> name=$$g[itemlabel] value=$$g[itemcost] onblur=calculate()> onFocus=blur()></td></tr>";';  $fmteval2='return "<tr><td> nowrap>$$itemname </td><td><input type=text size=4> name=$$itemlabel value=$$itemqty> onblur=calculate()> at</td><td> align=right>$$itemprice </td><td><input type=text size=7> name=$$itemlabel value=$$itemcost onblur=calculate()> onFocus=blur()></td></tr>";';  foreach($g as $k=>$v)     $$k=$v;    StopWatch('STREPLACE');  for($i=0;$i<100000;$i++)     $fmt1 = str_replace(array_keys($g),array_values($g),$fmtrep);  StopWatch('STRTR');    for($i=0;$i<100000;$i++)     $fmt1b = strtr($fmtrep2,$g);  StopWatch('EVALARR');    for($i=0;$i<100000;$i++)     $fmt2 = eval($fmteval);  StopWatch('EVALVARS');    for($i=0;$i<100000;$i++)     $fmt2b = eval($fmteval2);  StopWatch('END');    if($fmt1!=$fmt1b)     print"FAILED\n\nREP =$fmt1\n\nTR  =$fmt1b";  if($fmt1!=$fmt2)     print"FAILED\n\nREP =$fmt1\n\nEVALARR=$fmt2";  if($fmt1!=$fmt2b)     print"FAILED\n\nREP =$fmt1\n\nEVALVARS=$fmt2b";  print_r($StopWatch);}Expand();exit;
_________________________________________________________________
Your friends are close to you. Keep them that way.
http://spaces.live.com/signup.aspx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/pmwiki-devel/attachments/20070408/a498f38c/attachment.html 


More information about the pmwiki-devel mailing list