Welcome to Welcome to DNF.com™ - Domain Sales, Domain Forum, Domain Appraisals, Domain Registrars

If you are new to domains and looking to buy, sell and learn about domains then you have come to the right place. DNForum is the largest domain name community on the internet and continues to grow every day. There are over 105,000 domainers on DNForum doing everything from buying domains, selling domains, learning about domains and discussing domains. Take a minute and Register.

Register Today on DNForum IT'S FREE!

Results 1 to 7 of 7
  1. #1
    Platinum Lifetime Member
    mgstudent's Avatar
    Join Date
    Sep 2006
    Location
    New York
    Posts
    278
    DNF$
    802
    Bank
    0
    Total DNF$
    802
    Donate  

    php mysql loop question

    Can anyone help? I have a mysql table with the following information:

    ID Name Chapter Description
    1 uniqueName1 Ch1 Name uniqueDescription1
    2 uniqueName2 Ch1 Name uniqueDescription2
    3 uniqueName3 Ch1 Name uniqueDescription3
    4 uniqueName4 Ch2 Name uniqueDescription4
    5 uniqueName5 Ch2 Name uniqueDescription5
    6 uniqueName6 Ch3 Name uniqueDescription6
    7 uniqueName7 Ch3 Name uniqueDescription7

    I have some php loop code which echos the rows in the following format:


    1 - uniqueName1 - uniqueDescription1

    2 - uniqueName2 - uniqueDescription2

    3 - uniqueName3 - uniqueDescription3

    4 - uniqueName4 - uniqueDescription4

    5 - uniqueName5 - uniqueDescription5

    6 - uniqueName6 - uniqueDescription6

    7 - uniqueName7 - uniqueDescription7

    What I would like to do is to get the Chapter Name above the lines with the same chapter. But not above every line. See below:


    Ch1 Name

    1 - uniqueName1 - uniqueDescription1

    2 - uniqueName2 - uniqueDescription2

    3 - uniqueName3 - uniqueDescription3

    Ch2 Name

    4 - uniqueName4 - uniqueDescription4

    5 - uniqueName5 - uniqueDescription5

    Ch3 Name

    6 - uniqueName6 - uniqueDescription6

    7 - uniqueName7 - uniqueDescription7

    Do you know how I could edit my loop below:



    $query = "SELECT * FROM tbl order by ID ASC";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)) {
    echo "<br />";
    echo $row['id'];
    echo " - ";
    echo $row['Name'];
    echo " - ";
    echo $row['Description'];
    echo "<br />";
    }

  2. #2
    Platinum Lifetime Member

    Join Date
    Jun 2007
    Location
    Vancouver
    Posts
    164
    DNF$
    165
    Bank
    0
    Total DNF$
    165
    Donate  
    You'll need an extra field for chapter in database, then use a if else within the loop

  3. #3
    Platinum Lifetime Member
    mgstudent's Avatar
    Join Date
    Sep 2006
    Location
    New York
    Posts
    278
    DNF$
    802
    Bank
    0
    Total DNF$
    802
    Donate  
    Quote Originally Posted by dncsacnt View Post
    You'll need an extra field for chapter in database, then use a if else within the loop
    Thanks for your reply dnc. I already have that field in the table (in the database) as shown in the first quote box in my first post.

    My problem is that I only want the chapter name to be echoed once above the corresponding lines (but importantly not above each line). That's where I'm having some trouble.

    As shown ID 1-3 are all part of chapter1, ID 4-5 are chapter2, ID 6-7 are chapter3 - so in this batch of rows I'd only need 3 chapter headings.

    Could you give me an example of an if else which could work to echo the chapter name only once for each group that chapter name?

    Many thanks.

  4. #4
    Jedi Master
    Bob's Avatar
    Join Date
    Apr 2002
    Posts
    3,133
    DNF$
    49,605
    Bank
    0
    Total DNF$
    49,605
    Donate  
    Try this:


    Code:
     $query = "SELECT * FROM tbl order by ID ASC";
    $result = mysql_query($query) or die(mysql_error());
    
    $currentChapter = '';  // added line
    
    while($row = mysql_fetch_array($result)) {
    
    if($currentChapter!=$row['Chapter']) {echo "<b><font color='red'>".$row['Chapter']."</font></b><br>";} // added line
    $currentChapter=$row['Chapter'];   // added line
    
    echo "<br />";
    echo $row['id'];
    echo " - ";
    echo $row['Name'];
    echo " - ";
    echo $row['Description'];
    echo "<br />";
    }
    . . .

  5. #5
    Platinum Lifetime Member
    mgstudent's Avatar
    Join Date
    Sep 2006
    Location
    New York
    Posts
    278
    DNF$
    802
    Bank
    0
    Total DNF$
    802
    Donate  
    Bob - I owe you.

    Your method was a really smart solution and I'm grateful for your help. If our paths cross in future and I can help you in some way let me know (you have saved me many hours!)

    Thanks again.

  6. #6
    Jedi Master
    Bob's Avatar
    Join Date
    Apr 2002
    Posts
    3,133
    DNF$
    49,605
    Bank
    0
    Total DNF$
    49,605
    Donate  
    Welcome.

    When and if we ever cross path, you can buy me a free glass of water.

    -Bob
    . . .

  7. #7
    Platinum Lifetime Member
    mgstudent's Avatar
    Join Date
    Sep 2006
    Location
    New York
    Posts
    278
    DNF$
    802
    Bank
    0
    Total DNF$
    802
    Donate  
    My pleasure. I'll buy you 2 !

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Domain name forum recommended by Domaining.com