Current location: Hot Scripts Forums » Programming Languages » PHP » Make a WHILE statment repeat a certain amount of times??? Please Help


Make a WHILE statment repeat a certain amount of times??? Please Help

Reply
  #1 (permalink)  
Old 08-23-10, 09:53 AM
davestar057 davestar057 is offline
Wannabe Coder
 
Join Date: Dec 2007
Posts: 115
Thanks: 1
Thanked 1 Time in 1 Post
Make a WHILE statment repeat a certain amount of times??? Please Help

Hello,

I am performing a database query and retrieving the results.

The problem is I need to to repeat AT LEAST 6 times...despite there only sometimes being 2 or 3 records in the database for the particular query?

For example...I have a table in HTML with 6 fields...if it finds a review in the database (for example) it will print it on the page...untill it has run out of results then it will print something along the lines of "no more reviews - click here to add yours".

The problem is obviously with the WHILE loop it will print the results , but if there are less than 6 results in the database it will just leave the other fields blank! and not create the rest of the tables!?!?

My initial thought is if I can force a WHILE statement to loop a certain amount of times (regardless of how many records it pulls from the database)...that should do the trick.

If anybody could let me know how to do this I would be very greatful! Or any other suggestions are welcome.

MANY THANKS
DAVID
Reply With Quote
  #2 (permalink)  
Old 08-23-10, 10:59 AM
Golith Golith is offline
Newbie Coder
 
Join Date: Jun 2010
Posts: 87
Thanks: 6
Thanked 1 Time in 1 Post
Your code would be needed to give a complete answer

Quote:
The problem is I need to to repeat AT LEAST 6 times...despite there only sometimes being 2 or 3 records in the database for the particular query?
Although with what you say I would be giving a query first to check how many rows are returned. If it is <6 then fill the rest with empty string data .. this would be a quick fix.
problem here is that it would look like a big empty space before "no further ..." is presented

I myself would be asking for the rows and then using $string as STring from the loaded array.

I dont understand why it must be 6 or is this due to layout schema?

Remember to repost your code so we have something to work with
__________________
Its My script and I'll if I want to
www.cictradinggroup.com.au
Reply With Quote
  #3 (permalink)  
Old 08-30-10, 04:25 PM
sleignnet sleignnet is offline
New Member
 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
without looking at your code, I would do the query, find out how many rows exist ($rowCount), and do your while loop dumping your tabular data into a table. for the remaining rows -

while($rowCount < 6) {
//print out your remaining empty table(s) here - only need one empty table as the loop should repeat until the
//magic number 6 has been reached
$rowCount++;
}
Reply With Quote
  #4 (permalink)  
Old 08-31-10, 10:24 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
What I presume you have is code like this:

PHP Code:

while($row mysql_fetch_array($result)) {

    
// Some Code Here

but with that will only loop the number of times the mysql_fetch_array() returns true.

For what you are saying I would use a FOR loop instead for the amount of times you want to loop.

PHP Code:

$rows mysql_num_rows($result);

for(
$i=0$i<6$i++) {
    if(
$row mysql_fetch_array($result)) {
        
// CODE IF ROW
    
} else {
        
// CODE IF NO ROW
    
}

Reply With Quote
Reply

Bookmarks


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

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

Forum Jump


All times are GMT -5. The time now is 08:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.