Solved

auto increment in repeat region

Posted on 2002-07-11
15
364 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
15 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
 
LVL 40

Expert Comment

by:RQuadling
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 25 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:RQuadling
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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:RQuadling
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:RQuadling
RQuadling earned 25 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now