Page 1 of 1

Creating a top ten list of bands using dreamweaver cs3

Posted: Sat May 09, 2009 4:51 pm
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

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

Posted: Sat May 09, 2009 9:19 pm
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.

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

Posted: Sun May 10, 2009 9:59 am
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.

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

Posted: Sun May 10, 2009 10:01 am
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 :)

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

Posted: Sun May 10, 2009 6:31 pm
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.

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

Posted: Sun May 10, 2009 6:35 pm
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