Jump to content

[SOLVED] Need to Modify a Complicated Loop. Confused!


Fluoresce

Recommended Posts

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> </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> </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

?>

Thanks, Potatis.

 

I tried to simplify my question. It seems that I made it more confusing instead!  ;D

 

Here's my problem in more detail.

 

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?

Try this

 

<?php
    $firstpass=true;
    if (mysql_num_rows($res) >= 1){
        while ($row = mysql_fetch_assoc($res)) {
// test the first pass element, if first time, set the store value
                if($firstpass){
                    $store=$row['store'];
                    $firstpass=false;
                }
                if ($row['store'] != $store){
                        if ($store){
                                echo "</div>";
                        }
                        $store = $row['store'];
...
        } // end while
        echo "</div>";
    } // end first if
?>


Thanks, Radi8, but that only ensures that the store info doesn't appear after the first coupon.

 

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;

}

I essence you are already doing that. With the

<?php
if ($row['store'] != $store){
    if ($store){...}
?>

 

test, you are testing to see if the store has changed or is now empty. I wold create a variable at the beginning of the coupon iteration storing all the store data you want and then when you hit this condition print the store data. So, in essence you create the store variable FIRST, and then iterate through the coupons, then print the store data from the variable.

 

Another way to do this is with javascript and the use of <div> tags. I have some samples I might be able to send to demonstrate but it will take a bit to gather that and send to you. Send me a PM and we can get this working.

Thanks, Radi8.

 

However, I have already thought about storing the store info in a variable. What I don't understand is, how do I get the store info to echo after the last coupon, which itself must be different from the preceding coupons for having a "Show store info" button?

No, thanks. I would prefer not to use Javascript.

 

I just need to put the following into a proper code.

 

$store_id = $row['store_id'];

if (last or only coupon with this $store_id)

{
echo coupon + "Show store info" button + store info;
}

else

{
echo coupon;
}

Give this a shot:

<?php
    $table = "<div class=\"cpn\">";
    $firstpass=true;
    $count=0;
    if(mysql_numrows($res)>0){
      while ($row=mysql_fetch_assoc($res)){
          $count++;
          if($firstpass){
              $store=$row['store'];
              $firstpass=false;
          }
          if($row['store']!=$store){
              // reset counter if store data changes
              $count=1;
          }
          if($count==1){
              // store data
              $store_row .="<div id=\"".$row['sh']."\" style=\"display: none;\">";
              $store_row .="<table>";
              $store_row .="<tr>";
              $store_row .="<td>";
              $store_row .="<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>";
              $store_row .="</td>";
              $store_row .="</tr>";
              $store_row .="</table>";
              $store_row .="</div>";              
              // 1st coupon data
              $cpn_row.= "<table>";
              $cpn_row.= "<tr>";
              $cpn_row.= "<td><img src=\"images/".$row['logo']."\" /></td>";
              $cpn_row.= "<td><a href=\"".$row['go']."\">".$row['anchor']."</a><br />";
              $cpn_row.= (empty($row['cde']) ? $row['str'] : $row['cde']);
              $cpn_row.= "<br /> Expires:".$expires."</td>";
              $cpn_row.= "</tr>";
              $cpn_row.= "<tr>";
              $cpn_row.= "<td><a href=\"#\" onclick=\"ToggleVis(this, '".$row['sh']."'); return false;\">Show store info</a></td>";
              $cpn_row.= "<td> </td>";
              $cpn_row.= "</tr>";
              $cpn_row.= "</table>";
          }
          else{
              // subsequent coupon data
              $cpn_row .="<table>";
              $cpn_row .="<tr>";
              $cpn_row .="<td> </td>";
              $cpn_row .="<td><a href=\"".$row['go']."\">".$row['anchor']."</a><br />";
              $cpn_row .=(empty($row['cde']) ? $row['str'] : $row['cde']);
              $cpn_row .="<br /> Expires: $expiration</td>";
              $cpn_row .="</tr>";
              $cpn_row .="</table>";
          }
          
      }// end while
  }// end if
  $table.=$cpn_row;
  $table.=$store_row;
  $table.="</div>";
  print $table;
?>

I did not do anything more than reformat your original code some. There may be errors because I did not run or test it, but it should be close to what you need.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.