Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

counter in do/while loop broke my page



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old February 13 '04, 12:26 AM (#1)
mikedfunk is offline
WDF Member
 
mikedfunk's Avatar
 
Join Date: May 2003
Location: Orlando, FL
Posts: 31
mikedfunk
Send a message via AIM to mikedfunk
counter in do/while loop broke my page

I'm trying to insert a counter that lets me do a repeating region of MySQL query results, displayed in 3 columns then going to the next row and starting over. Like a table of results. I thought I had the code right but now my page won't display at all. First I went to the first appearing <?php area in my page, making sure it wasn't inside any loops, and inserted on separate lines:
PHP Code:
$counter 0;
$columns 0
My code, generated by dreamweaver as a horizontal repeating region, is in a do/while loop:
PHP Code:
<table width="200" border="1" cellspacing="2" cellpadding="5">
  <tr>
    <?php do { ?>
    <td><span class="header1"><a href="/closeups/<?php echo $row_mostRecent['picture_id']; ?>"><img src="/thumbs/<?php echo $row_mostRecent['picture_URL']; ?>" border="0"><span class="header2"><br>
      </span></a><span class="header2"><a href="closeups/<?php echo $row_mostRecent['picture_id']; ?>"><?php echo $row_mostRecent['picture_title']; ?> </a> <br>
  Size: <?php echo $row_mostRecent['picture_size']; ?><br>
  Viewed <?php echo $row_mostRecent['picture_viewCount']; ?> times<br>
  Created: </span><span class="header2"><?php echo $row_mostRecent['picture_createdWhen']; ?>
  </span></span></td>
This is where I put my counter that broke the page:
PHP Code:
<?php 
    $counter
++;
    
if_($counter_==_$columns)
    {
    
$counter_=_0;
    
echo_"</tr><tr>";
    }
    
?>
And the code finishes up...
PHP Code:
    <?php } while ($row_mostRecent mysql_fetch_assoc($mostRecent)); ?>
  </tr>
</table>
Shouldn't this work? Shouldn't a ++ thingy work in that location since it is inside the "do" part of the do/while function? What am I doing wrong?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 13 '04, 12:10 PM (#2)
bwedekin is offline
WDF Member
 
bwedekin's Avatar
 
Join Date: February 2004
Location: The Midwest
Posts: 33
bwedekin
Send a message via AIM to bwedekin
What is the counter for?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 13 '04, 02:14 PM (#3)
mikedfunk is offline
WDF Member
 
mikedfunk's Avatar
 
Join Date: May 2003
Location: Orlando, FL
Posts: 31
mikedfunk
Send a message via AIM to mikedfunk
I am trying to dynamically display a Picture, it's name, when it was last updated, and then to the right of that another picture, etc. I want to limit it to 3 pictures horizontally and then go to the next row down and start to do another row.

See To view the link you have to Register to view an example of what I mean.

Any suggestions?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 13 '04, 05:56 PM (#4)
Elroy is offline
Just having fun
 
Elroy's Avatar
 
Join Date: January 2004
Location: UK
Posts: 206
Elroy
Send a message via ICQ to Elroy
I see some _ (underline) in your codes below "counter++". You don't need them, for what I know.

Quote:
<?php } while ($row_mostRecent = mysql_fetch_assoc($mostRecent)); ?>
Try put a "==" there instead of "=". Inside the while() thing you are comparing the two variables, yea?

Hope it helps.

Last edited by Elroy; February 13 '04 at 05:56 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 13 '04, 07:38 PM (#5)
rosland is offline
Retired Staff
 
rosland's Avatar
 
Join Date: July 2003
Location: Norway
Posts: 1,939
rosland is a jewel in the roughrosland is a jewel in the roughrosland is a jewel in the roughrosland is a jewel in the rough
Did the code work before you altered it with the counter (displaying everything on one row it appears)?

Don't change the "=" to "==" in the while statement, that will destroy the boleean logic. With one "=" sign, it's telling the program to stay in the loop as long as query is retrieving data. When the internal 'while' iterator reaches the last result set, it comes up 'false' and the loop breaks.

You counter doesn't make sense though. (I don't know if it's WDF that inserts all the underscore lines, but if they exist in your code you should remove them.) In your If statement you're comparing $counter to $column which are both set to '0' on top of your script. That means the clause will never be true as the value of "$counter" will have the value of 1 the first time the comparison is made, and thereafter only increase.

What you could do is:
PHP Code:
# Change $columns=0 to $i=0 outside loop to begin with
$counter++;
$i++;
if (
$counter==3)
 {
   
# The while clause is to prevent an empty <tr> after last image
   
while ($i<mysql_num_rows($mostRecent))
    {
      echo 
"</tr>\n<tr>";
      
$counter=0;
    }
 } 
EDIT:
In the future when you make a post containing either the PHP or CODE BB-tags, be aware that WDF ignores word wrapping for any text inside these tags. That means that your posted code will expand the column to fit the length of the text line. If you write a 6 feet long line without a line break, then we will have to scroll 6 feet horizontally to read your post

Last edited by rosland; February 14 '04 at 06:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 14 '04, 08:20 PM (#6)
mikedfunk is offline
WDF Member
 
mikedfunk's Avatar
 
Join Date: May 2003
Location: Orlando, FL
Posts: 31
mikedfunk
Send a message via AIM to mikedfunk
I fixed it. It seems I had a few problems.

1. In Dreamweaver's code view, I had typed a few spaces in certain areas. Instead of inserting spaces, Dreamweaver inserted the ascii character for the dagger.

2. In my code for the variables at the top, I forgot a $ before one of the variables.

3. I see what you're saying about comparing the $counter with the $columns and them both being zero. That's not the way it actually was in the code; the $columns was set to 5. I typed it in wrong in the forum.

Thanks for all the help, though, you all did make some things clearer!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:48 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163