PHP Color the lowest price in a table -
i try insert 7 price values table , color lowest price. don't know place $lowestprice
. i'm aware it's poor code, worked until try add color.
for ($i = 1; $i <= 7; $i++){ ${"price".$i} = preg_replace($regexp, $match, ${"rawprice".$i}); if (preg_match('/'.$article.'/i', ${"stock".$i})) { $list[$i] = ${"price".$i}; $lowestprice = min($list); if (preg_match('/^'.$lowestprice.'/i', ${"price".$i})) { echo ${"url".$i}." class=\"lowestprice\">".${"price".$i}." €</a></td>"; // lowest price in color css } else { echo ${"url".$i}." class=\"price\">".${"price".$i}." €</a></td>"; } } else { echo "<td>out of stock</td>"; } }
first of all, not use ${"string".$i}
type variables. great works, sure, if working on data seems follow array structure, use array. less clutter, less drama, happy.
$rawprice should array containing 7 integers 7 prices in cents. this:
$lowestprice = min( $rawprice ); for( $i = 0; $i <= 6; $i++ ) { if( instock( $article[$i] ) ) { $price = str_replace( ".", ",", (string) ($rawprice[$i] / 100 ) ); if( $rawprice[$i] == $lowestprice ) { $class = "lowestprice"; } else { $class = "price"; } echo "<td><a href=\"{$url[$i]}\" class=\"{$class}\">{$price} €</a></td>"; } else { echo "<td>out of stock</td>"; } }
Comments
Post a Comment