Confusing While Loop

Discussion in 'PHP' started by Masterful, Apr 21, 2009.

  1. #1
    On my site, I present:

    * coupons for stores
    * info about the stores

    The coupons run vertically down my page.

    The first coupon for each store has a "Show store info" button.

    When clicked, the respective store's info appears just below the first coupon.

    It looks like this:

    * Coupon
    * Store Info
    * Coupon
    * Coupon

    I want the "Show store info" button to appear on the last coupon.

    And, when clicked, I want the respective store's info to appear after all of the coupons, like this:

    * Coupon
    * Coupon
    * Coupon
    * Store Info

    The problem is, I can't figure out how to do it. I can only get the store info to appear above all of the coupons, like this:

    * Store Info
    * Coupon
    * Coupon
    * Coupon

    Can anyone help, please? There must be a way . . .

    <?php
    
    if (mysql_num_rows($res) >= 1) 
    {
    
    
    
    $store = '';
    
    
    
    while ($row = mysql_fetch_assoc($res)) 
    {
    
    
    
    if ($row['store'] != $store)
    {
    
    
    
    if ($store)
    {
    echo "</div>";
    }
    
    
    
    $store = $row['store'];
    
    
    
    echo "<div class=\"cpn\">";
    
    
    
    // First coupon. Note the "Show store info" button
    echo "<table>";
    echo "<tr>";
    echo "<td><img src=\"images/{$row['logo']}\" /></td>";
    echo "<td><a href=\"{$row['go']}\">{$row['anchor']}</a><br />";
    echo (empty($row['cde']) ? $row['str'] : "$exclusive {$row['cde']}");
    echo "<br /> Expires: $expires</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td><a href=\"#\" onclick=\"ToggleVis(this, '{$row['sh']}'); return false;\">Show store info</a></td>";
    echo "<td>&nbsp;</td>";
    echo "</tr>";
    echo "</table>";
    
    
    
    // Store info
    echo "<div id=\"{$row['sh']}\" style=\"display: none;\">";
    echo "<table>";
    echo "<tr>";
    echo "<td>";
    echo "<ul>
    <li>Store: {$row['store']}</li>
    <li>Web site: <a href=\"{$row['visit']}\">{$row['site']}</a></li>
    <li>Category: {$row['category']}</li>
    <li>Return policy: {$row['returns']}</li>
    <li>Customer support: {$row['support']}</li>
    <li>Payment options: {$row['payment']}</li>
    </ul>";
    echo "</td>";
    echo "</tr>";
    echo "</table>";
    echo "</div>";
    
    
    
    } // end if
    
    
    
    else
    {
    // 2nd, 3rd, etc. coupon for same store
    echo "<table>";
    echo "<tr>";
    echo "<td>&nbsp;</td>";
    echo "<td><a href=\"{$row['go']}\">{$row['anchor']}</a><br />";
    echo (empty($row['cde']) ? $row['str'] : "$exclusive {$row['cde']}");
    echo "<br /> Expires: $expiration</td>";
    echo "</tr>";
    echo "</table>";
    }
    
    
    
    } // end while
    
    
    
    echo "</div>";
    
    
    
    } // end first if
    
    ?>
    PHP:
     
    Masterful, Apr 21, 2009 IP
  2. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #2
    I'll try to explain myself more clearly.

    I have a table of coupons. The coupons are in rows. Each row also contains the details of the coupon's store.

    Before, I used to echo the coupons one-by-one onto my page, with each coupon's store info appearing beneath it. However, I no longer want to do that, because, if I have multiple coupons for a single store, that store's details appear multiple times on a single page. I am therefore trying to figure out a way to get the coupons for a single store to appear together, with the store's info appearing just once after the last coupon. This means that the last coupon must be the only one to have a "Show store info" button.

    So far, the code that I have produced only allows me to echo the store info after the first coupon. The first coupon is also the only one with a "Show store info" button.

    How can I echo all of the coupons and make sure that the store info only appears after the last one, which is also the only one to have a "Show store info" button?
     
    Masterful, Apr 22, 2009 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Here's a better way to explain it.

    How can I do the following?

    if (last or only coupon with this store ID)
    
    {
    echo coupon + "Show store info" button + store info;
    }
    
    else
    
    {
    echo coupon;
    }
    Code (markup):
     
    Masterful, Apr 22, 2009 IP
  4. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    I no longer need help with this problem, guys.
     
    Masterful, Apr 22, 2009 IP