?
Solved

auto increment in repeat region

Posted on 2002-07-11
15
Medium Priority
?
394 Views
Last Modified: 2012-05-04
Hi,

I have a problem on a repeat region where I want a variable to be dynamically auto incremented.

This is the code of the repeat region, which is a table row:

      <?php do { ?>
      <tr>
        <td height="115" align="center"><a href="/portfolios/gallery.php?id=<?php echo $row_RecPortList['id']; ?>&rank=RANK&cat=<?php echo $row_RecPortList['cat']; ?>"><img src="/images/<?php echo $HTTP_GET_VARS['cat']; ?>/<?php echo $row_RecPortList['id']; ?>.jpg" border="0"></a></td>
      </tr>
       <?php } while ($row_RecPortList = mysql_fetch_assoc($RecPortList)); ?>

I need the RANK variable to be 0 for the first generated row, 2 for the second, 3 for the third one and so on, without the use of database since output is sorted by date and not by rank, so if I use a database value for RANK I will have to change this value for all records when I add any new one.

Does anywone can tell me what code I should use?

Thanks :0)

Stephane
0
Comment
Question by:musclenetworks
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7148068
For embedding php expressions try using this ...
  <?=$row_RecPortList['id']?>
... instead of ...
  <?php echo $row_RecPortList['id']; ?>

Also I assume you want to go 1, 2, 3... not 0, 2, 3...?

<?php
  $rank = 1;
  do { ?>
     <tr>
       <td height="115" align="center"><a href="/portfolios/gallery.php?id=<?=$row_RecPortList['id']?>&rank=<?=$rank>&cat=<?=$row_RecPortList['cat']?>"><img src="/images/<?=$HTTP_GET_VARS['cat']?>/<?=$row_RecPortList['id']?>.jpg" border="0"></a></td>
     </tr>

<?php
  $rank++;
  } while ($row_RecPortList = mysql_fetch_assoc($RecPortList)); ?>

0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7148085
Actually so much of your output is php that you might consider not breaking out of php at all...

<?php
 $rank = 1;
 do {
   echo "<tr>\n";
   echo "<td height='115' align='center'><a href='/portfolios/gallery.php";
   echo "?id=" . $row_RecPortList['id'];
   echo "&rank=" . $rank;
   echo "&cat=" . $row_RecPortList['cat'] . ".>";
   echo "<img src='/images/" . $HTTP_GET_VARS['cat'] . "/" . $row_RecPortList['id'] . ".jpg'  border='0'>";
   echo "</a></td>\n";
   echo "</tr>\n";
   $rank++;
 } while ($row_RecPortList = mysql_fetch_assoc($RecPortList)); ?>

0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7148089
oops, I think I added an extra "." in ...

  echo "&cat=" . $row_RecPortList['cat'] . ".>";

should be ...

  echo "&cat=" . $row_RecPortList['cat'] . ">";
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7148530
The heredoc may be more to your liking ...

<?php
$rank = 1;
do
     {
      echo<<<END_HTML
<tr>
     <td height='115' align='center'>
          <a href="/portfolios/gallery.php?id={$row_RecPortList['id']}&rank=$rank&cat={$row_RecPortList['cat']}">
               <img src="/images/{$HTTP_GET_VARS['cat']}/{$row_RecPortList['id']}.jpg"  border="0">
          </a>
     </td>
</tr>
END_HTML;
     $rank++;
     }
while ($row_RecPortList = mysql_fetch_assoc($RecPortList));
?>

Watch out for using $HTTP_GET_VARS in later versions of PHP.

Use $_GET instead.

Richard.
0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7148540
Hey yeah, I like the heredoc syntax.  I'd been wondering what that's useful for!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7148553
In effect, you can design templates and assign them to variables


$sHeader = <<<END_HTML
<html>
<head>
<title>$sTitle</title>
</head>
<body>
END_HTML;

$sFooter = <<<END_HTML
</body>
</html>
END_HTML;

sort of thing.

Not REAL templates, but it does allow you to write HTML with embedded PHP vars without having to escape in and out of PHP/HTML.

But a good template class does the same thing. I'm currently using patTemplates. Very easy and flexible.

Richard.
0
 

Author Comment

by:musclenetworks
ID: 7150039
Well, unfortunately I'm not php developper and I use Dreamweaver MX to generate PHP code, so I can't change anything. I tried different versions suggested here but I got error each time I tried to make change in code built by Dreamweaver. Is there a way to use exactly the same code I submitted, without wny change, and just had code to generate the RANK variable? This is initial code:

<?php do { ?>
     <tr>
       <td height="115" align="center"><a href="/portfolios/gallery.php?id=<?php echo $row_RecPortList['id']; ?>&rank=RANK&cat=<?php echo $row_RecPortList['cat']; ?>"><img src="/images/<?php echo $HTTP_GET_VARS['cat']; ?>/<?php echo $row_RecPortList['id']; ?>.jpg" border="0"></a></td>
     </tr>
      <?php } while ($row_RecPortList = mysql_fetch_assoc($RecPortList)); ?>
0
 

Author Comment

by:musclenetworks
ID: 7150102
Actually, the heredoc from Richard worked! My only worry, is that all the code generated by Dreamweaver is like:
<?php echo $row_RecPics['pic_id']; ?>

so, if I change the database for example, Dreamweaver will not recognize the heredoc code and will not update it automatically.

Is there a way to use heredoc with Dreamweaver friendly coding, such:

<?php
$rank = 1;
do
    {
     echo<<<END_HTML
<tr>
    <td height='115' align='center'>
         <a href="/portfolios/gallery.php?id=<?php echo $row_RecPics['pic_id']; ?>&rank=$rank&cat=<?php echo $row_RecPics['cat']; ?>
etc....



0
 

Author Comment

by:musclenetworks
ID: 7150176
Actually, the heredoc from Richard worked! My only worry, is that all the code generated by Dreamweaver is like:
<?php echo $row_RecPics['pic_id']; ?>

so, if I change the database for example, Dreamweaver will not recognize the heredoc code and will not update it automatically.

Is there a way to use heredoc with Dreamweaver friendly coding, such:

<?php
$rank = 1;
do
    {
     echo<<<END_HTML
<tr>
    <td height='115' align='center'>
         <a href="/portfolios/gallery.php?id=<?php echo $row_RecPics['pic_id']; ?>&rank=$rank&cat=<?php echo $row_RecPics['cat']; ?>
etc....



0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 7152042
Just do like that

<?php $RANK=1;?>
<?php do { ?>
    <tr>
      <td height="115" align="center"><a href="/portfolios/gallery.php?id=<?php echo $row_RecPortList['id']; ?>&rank=<?php echo $RANK++?>&cat=<?php echo $row_RecPortList['cat']; ?>"><img src="/images/<?php echo $HTTP_GET_VARS['cat']; ?>/<?php echo $row_RecPortList['id']; ?>.jpg" border="0"></a></td>
    </tr>
     <?php } while ($row_RecPortList = mysql_fetch_assoc($RecPortList)); ?>

I made 2 changes
<?php echo $RANK++?> and <?php $RANK=1;?>

Harry
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 11664990
I think we have all contributed to the answer, but no feedback. Grr. Some people. Just SO ungrateful!!!!
0
 

Author Comment

by:musclenetworks
ID: 11722109
Please forgive me about this, I have been kept away from the site for a while and some of my issues such as this one have been kept open while not reloevant anymore. My apologies, I will give more care from now. I will leave to the administrator appreciation who should get what for this questions. Please let me know if any action is required from my end.
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 100 total points
ID: 11723790
You can close the question yourself and split the points as recommended or award points to the answer must likely to have helped or to me simply for asking or to Hamlet who didn't ask and you remember that old saying "Ask you don't get, don't ask you don't want!", which really justs complicates matters in the "who do I assign the points to afterall" question.

Good luck!

Richard.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question