-2

I try to insert 7 price values into a table and color the lowest price. I don't know where to place $lowestprice. I'm aware it's poor code, but it worked until I try to add a 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 with css
            }
            else
            {
                echo ${"url".$i}." class=\"price\">".${"price".$i}." €</a></td>";
            }                   
        }
        else
        {
            echo "<td>Out of stock</td>";
        }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kernel
  • 17
  • 1
  • 1
  • 6

1 Answers1

0

First of all, do not use ${"string".$i} type variables. It is great that it works, sure, but if you are working on data that seems to follow an array structure, just use an array. Less clutter, less drama, everyone happy.

$rawprice should be an array containing 7 integers with 7 prices in cents. Then you would do something like 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>";
  }
}
Sumurai8
  • 20,333
  • 11
  • 66
  • 100