1

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">?

6 Answers 6

3

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"); ?>">
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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

Comments

0

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

Comments

0

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
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.