Creating a top ten list of bands using dreamweaver cs3

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beiron
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 4:41 pm

Creating a top ten list of bands using dreamweaver cs3

Post by beiron »

Hi guys,
I've been searching for the last 2 hours online to find a way to make a top ten list in PHP drawing some of the data from a database

basically i'm working on a site which will display the top 10 voted bands drawing the band name and the rating from the DB. however with the repeat region I'm having trouble with one coloumn of the table showing the numbers 1-10 even though its within the repeat reagion, this is what i have so far fot the table:

Code: Select all

 
<table border="1" align="center">
          <tr>
            <td>#</td>
            <td>band_rating</td>
            <td>band_name</td>
          </tr>
          <?php do { ?>
            <tr>
              <td>
              <?php 
              $i = 1;
              for($i; $i<10; $i++)
              {
                echo $i;
              }
              
              ?>
               </td>
              <td><?php echo $row_topten['band_rating']; ?>&nbsp; </td>
              <td><a href="profile.php?band_name=<?php echo $row_topten['band_name']; ?>"> <?php echo $row_topten['band_name']; ?>&nbsp; </a> </td>
            </tr>
            <?php } while ($row_topten = mysql_fetch_assoc($topten)); ?>
        </table>
 
Which shows:

# | band_rating | band_name
12345678910 | 0 | beirons band
12345678910 | 0 | band 2


what i want it to show is

# | band_rating | band_name
1 | 3 | beirons band
2 | 2 | band 2


I have done this before on a previous site but have lost the code.

can anyone help? thanks
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Creating a top ten list of bands using dreamweaver cs3

Post by it2051229 »

Code: Select all

 
<?php 
       $i = 1;
       do { 
?>
            <tr>
              <td>
               </td>
               <td><?php echo $row_topten['band_rating']; ?>&nbsp; </td>
              <td><a href="profile.php?band_name=<?php echo $row_topten['band_name']; ?>"> <?php echo $row_topten['band_name']; ?>&nbsp; </a> </td>
            </tr>
            <?php } while ($row_topten = mysql_fetch_assoc($topten)); ?>
 
how about this?
I'm quite confused with your code because you have a FOR loop inside a DO WHILE loop which defeats your goal. I think the code that i have posted above fixes the Band Number Column of the table.. If this is working, tell me so we could move to the next problem.
beiron
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 4:41 pm

Re: Creating a top ten list of bands using dreamweaver cs3

Post by beiron »

Thanks for the reply but im affraid it didnt work. it didnt print out the number in the posistion (#) coloumn.

i know the for loop inside the do while is silly but its the only way i can think of geting it to print out a number for each record in the recordset.

il try to re explain:

the sql is

Code: Select all

 
SELECT band_name, band_rating
FROM bands
ORDER BY band_rating ASC LIMIT 10
 
the table im trying to make is a 3 column by 11 rows, 1st rows show "#" "band rating" "band name"
in the folowing rows, in the "#" coloumn there should be the numbers 1 - 10 and in the other two coloumns should be the rows in the recordset.


so if Band A has 100 ratings and Band B has 103 ratings then the table should look like this:

# | band rating | band name
1 | 103 | Band B
2 | 100 | Band A

Basicly put, Dreamweaver makes variables for the total rows in a recordset what i need to get at is the individual row numbers as they come if thats posible or a work around to get it to print the numbers 1-10 in the 1st coloumn of each row within the do while loop.
beiron
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 4:41 pm

Re: Creating a top ten list of bands using dreamweaver cs3

Post by beiron »

I have fixed it:

Code: Select all

 
<table border="1" align="center">
          <tr>
            <td>#</td>
            <td>band_rating</td>
            <td>band_name</td>
          </tr>
          
          <?php
          $i=1;
           do { ?>
            <tr>
              <td>
              <?php echo $i;
              $i = $i+1;
              ?>
              </td>
              <td><?php echo $row_topten['band_rating']; ?>&nbsp; </td>
              <td><a href="profile.php?band_name=<?php echo $row_topten['band_name']; ?>"> <?php echo $row_topten['band_name']; ?>&nbsp; </a> </td>
            </tr>
            <?php } while ($row_topten = mysql_fetch_assoc($topten)); ?>
        </table>
 
Thanks for your help :)
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Creating a top ten list of bands using dreamweaver cs3

Post by it2051229 »

ok.. soo is it totally working? or just the band # column.. i forgot to echo out the "$i" variable good thing you thought about it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Creating a top ten list of bands using dreamweaver cs3

Post by John Cartwright »

You need to change the ORDER BY clause in your query to sort by DESCENDING band_rating, not ASCENDING.

I.e.

Code: Select all

 
SELECT band_name, band_rating
FROM bands
ORDER BY band_rating DESC LIMIT 10
Post Reply