Forum Moderators: coopster

Message Too Old, No Replies

Complicated While Loop

         

Tehuti

5:03 pm on Apr 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a table of coupons. Each row contains details of the coupon, plus 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 being hidden beneath it. If a visitor wanted to see the store info, they had to click the coupon's "Show store info" button.

I no longer want to present the coupons one-by-one, 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 also be the only one to have a "Show store info" button.

So far, the code that I have produced (see below) only allows me to echo the store info after the first coupon. The first coupon is also the only one to have 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?

Any suggestions will be very much appreciated.

####################################

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

[edited by: Tehuti at 5:05 pm (utc) on April 22, 2009]

Tehuti

5:48 pm on Apr 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



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;
}

BradleyT

8:26 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



Just do your saves after you do your comparisons and you should have no problems.

$savedstore = ''
$savedcoupon = ''
$ctr = 0
Read record
If savedstore != recordstore {
if ($ctr > 0) {
printstore($storerecord)
}

}

echo $recordcoupon
$savedstore = $recordstore
$ctr += 1

if $savedcoupon == $recordcoupon {
printstore($storerecord)
}
$savedcoupon = recordcoupon

function printstore($store) {
echo $store
}

BradleyT

8:30 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



Actually this part might not work correctly -

if $savedcoupon == $recordcoupon {
printstore($storerecord)
}
$savedcoupon = recordcoupon

I put it in there because on the very last record it's going to print the coupon but not the store, but I don't think that code is going to do it. So you'll have to figure something out for the very last record ($ctr + num of records from sql?).

Tehuti

10:03 pm on Apr 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the attempt, BradleyT.

With your help, and with the help of others in other forums, I got it to work in the end.

Basically, what I done was store the first coupon and its store info in a variable. Then, for the next coupon, I checked to see if it had the same store ID. If it didn't, I echoed the previous variable; if it did, I stored it in another variable to be echoed above the first variable.

Code Outline
############

$count = 0;
$firstloop = true;

while ($row = mysql_fetch_assoc($res)) {

$count++;

if ($firstloop) {
$store = $row['store'];
$firstloop = false;
}

if ($row['store'] != $store) {
echo $cpns;
echo $cpn1;
echo $storeinfo;
$store = $row['store'];
$cpns = "";
$cpn1 = "";
$storeinfo = "";
$count = 1;
}

if ($count == 1) {
$cpn1 .= "first coupon . . .";
$storeinfo .= "store info . . .";
} else {
$cpns .= "2nd, 3rd, etc., coupon . . .";
}

} // end while

echo $cpns;
echo $cpn1;
echo $storeinfo;