Jump to content

[SOLVED] php mysql loop question


woop

Recommended Posts

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 />";

}

 

 

Link to comment
Share on other sites

Suppose I would do it like this.

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.