Ivan Novak
Ivan Novak

Reputation: 666

Alternating CSS Style with multiple Mysql Results

I've got a site where someone searches for x product in their location and the site spits back a list of results.

if(isset($_POST['zip'])){
$qry="SELECT business_id FROM ".TBL_BUSINESS." WHERE zip LIKE '%".$_POST['zip']."%'";
$rs = mysql_query($qry);

$rec = array();

while(($row = mysql_fetch_array($rs)) !== FALSE ){
    $rec[] = $row[0];
}

if(!empty($rec[0])){

    echo "Products for this location<br/>";

    foreach ($rec as $result)
    {
        $bid = $result;
        $qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
        $rs2 = mysql_query($qry2);
        $rec2 = mysql_fetch_array($rs2);
        ?>
            <div class="norm">
                <img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
                <h3><a href="product.php?id=<?php echo $rec2['id']; ?>"><?echo $rec2['name'];?>&nbsp;&nbsp;<?php echo $rec2['prodvalue']?></a></h3>
                <div class="prodlistMeta">
                    <a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
                    <a class="print" href="#">Print</a>
                </div>
            </div>
        <?php
    }
}
else
{
    echo "No Product is added for this location";
}

} ?>

What would be the best way to alternate <div class="norm"> with <div class="alt">?

Upvotes: 1

Views: 760

Answers (6)

moribvndvs
moribvndvs

Reputation: 42497

Widespread adoption of CSS 3 can't come soon enough: http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#structural-pseudos

Upvotes: 0

Mark
Mark

Reputation: 6254

Why not use modulus and a row id? Much simpler, no need for unnecessary variables

foreach ($rec as $rid => $result)
{
    $bid = $result;
    $qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
    $rs2 = mysql_query($qry2);
    $rec2 = mysql_fetch_array($rs2);
    ?>
            <div class="<?=($rid % 2 == 0) ? 'norm' : 'alt' ?>">
                    <img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
                    <h3><a href="product.php?id=<?php echo $rec2['id']; ?>"><?echo $rec2['name'];?>  <?php echo $rec2['prodvalue']?></a></h3>
                    <div class="prodlistMeta">
                            <a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
                            <a class="print" href="#">Print</a>
                    </div>
            </div>
    <?php
}

Upvotes: 0

dnagirl
dnagirl

Reputation: 20456

set a counter on your output loop. Whenever the counter is even, set the class to normal, else set it to alternate.

Upvotes: 0

Tyler Carter
Tyler Carter

Reputation: 61557

if(some expression)
{
    $class="norm";
}
else
{
    $class="alt";
}
?>
<div class="<?php echo $class;?>">

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55720

keep a counter and use it's value modulo 2 to determine whether the class should be "norm" or "alt".

 $rec2 = mysql_fetch_array($rs2);
 $count++;
   ?>
      <div class="<?php echo($count%2?"norm":"alt"); ?>">

Upvotes: 3

cletus
cletus

Reputation: 625037

I tend to use something like this:

$row = 0;
foreach ($rec as $result) {
  $class = $row++ & 1 == 1 ? 'alt' : 'norm';
  ...
  echo <<<END
<div class="$class">
...
END;
}

You can use curly braces to do the expression within the string but I generally prefer not to embed that kind of logic. It's (imho) a little harder to read. Plus the above gives you the opportunity to output it for debugging purposes more easily, etc.

Upvotes: 0

Related Questions