Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-07-2009, 05:38 PM Looping with PHP
Junior Talker

Posts: 4
Trades: 0
Im kind of new at php and I just have the hardest time with looping. I have a database that I want to put into separate HTML tables. If my database contained

Table: game
id___level___stage___name
1_____1_______2_____Jim
2_____3_______1_____Mark
3_____3_______4_____Jane
4_____2_______1_____Joyce
5_____1_______1_____Kim
6_____1_______1_____Matt
7_____3_______2_____Mike
8_____1_______3_____Steve

What I would like is for it to end up looking like this:

Level 1 - Stage 1

Kim
Matt

Level 1 - Stage 2

Jim

Level 1 - Stage 3

Steve

Level 1 - Stage 4

Level 2 - Stage 1

Joyce

Level 2 - Stage 2

Level 2 - Stage 3

Level 2 - Stage 4

Level 3 - Stage 1

Mark

Level 3 - Stage 2

Mike

Level 3 - Stage 3

Level 3 - Stage 4

Jane

I have this but it doesn't work that well.

$i=1;
$result = mysql_query("SELECT * FROM game ORDER BY level,stage");
while ($row = mysql_fetch_array($result)) {
$level=$row['level'];
$stage= $row['stage'];
$name= $row['name'];
if($level==$i){
print 'Level '.$level.' - Stage '.$stage'<br />';
$i++;}
print $name'<br />';
}

This does well for the first couple lines but then it stops printing the levels. I don't think that I did the correct thing and for the life of me I cannot figure this out. Any help would be great.
Thank you
Matt
immatt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-09-2009, 06:20 AM Re: Looping with PHP
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
If you remove the if() and always print both level, stage and the name then are they in the correct order?

In some cases maybe there will be a gap in level (for example not having anyone at lvl 4 but some at both 3 and 5)? Then this would be better.

PHP Code:
$pre_level 0// Assuming there is no level zero, if there is set it to -1 instead
$pre_stage 0;
while ( ... ) {
   
// some code here
   
if ($level $pre_level) {
      
// print new level and new stage
      
$pre_level $level;
      
$pre_stage $stage;
   } elseif (
$stage $pre_stage) {
      
// print same level again with new stage
      
$pre_stage $stage;
   }
   
// print the name

__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 08-09-2009 at 06:24 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Looping with PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12118 seconds with 12 queries