Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

auto increment in repeat region

Posted on 2002-07-11
15
372 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 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