...

View Full Version : Display top 10 overall rated tutorials?



LoRd_aLmIghTy
12-26-2006, 02:51 PM
Hi i have been developing a tutorial system for a while now and have gotten pretty much everything done but this. I want the top 10 overall rated tutorials to be displayed in the html form below. I was thinking of using the while() loop but then saw that it would not work since their i cant use it and have to <td></td>s in the same <tr> which is being looped. Any thoughts? the score is determined by a field in my table called score and i was thinking of using a query that orders by score.
Thanks



<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="10%">Thumb for tut w/ highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
<td width="10%">Thumb for tut w/ second highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
</tr>
<tr>
<td width="10%">Thumb for tut w/ third highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
<td width="10%">Thumb for tut w/ fourth highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
</tr>
<tr>
<td width="10%">Thumb for tut w/ fifth highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
<td width="10%">Thumb for tut w/ sixth highest score here</td>
<td width="40%"><a href="link to it here">Name of Tutorial</a> - Description</td>
</tr>
</table>

anarchy3200
12-26-2006, 04:04 PM
Ok, may be a better way and i am open to criticism but how i usually do it is with a while. Before starting the while initialise a variable with a 0 value,

$count = 0;
print "<tr>";
while () {
$count++;
if($count%2 == 0){
print "</tr><tr>";
}
print "<td>Whatever</td>";
}
print "</tr>";


As this loops, the start/end tr are only used if the count field/2 has no remainder which happens every other record, may require some slight corrections to get it to display correctly but you should get the general idea.

Hope this helps.

LoRd_aLmIghTy
12-26-2006, 04:17 PM
yes i understand that but thats just displaying them in a list sort of. i wanted them to be 2 apiece in side one <tr> and the places where their is no data to display no entrie in that <td>. if you dont understand look at he html table i posted in my earlier post. hope this helps clear confusion.

thanks

danielwarner
12-26-2006, 04:26 PM
<?php

// ...

echo '<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
';
$columns = 2;
$resultSet = mysql_query('SELECT * FROM `table` ORDER BY `highscore` DESC LIMIT 0,10');
$rowNum = mysql_num_rows($resultSet);
if($rowNum == 0)
{
echo ' <td align="center">No results returned!</td>';
}
else
{
if($rowNum % $columns != 0)
{
$rowNum += $columns - $rowNum % $columns;
}
while($rowNum-- > 0)
{
echo ' <td';
if($result = mysql_fetch_assoc($resultSet))
{

//Edit these echo's

echo ' width="10%">Thumb for tut w/ highest score here</td>
'; echo ' <td width="40%"><a href="' . $result["link_to_tut"] . '">Name of tutorial</a> - Description etc....</td';

//Dont edit beyond here

}else{echo ' colspan="2"';}
echo '></td>
';
if($rowNum != 0 && $rowNum % $columns == 0)
{
echo ' </tr>
<tr>
';
}
}
}
echo ' </tr>
</table>';

?>

The code above should work after you edit the query....

...and anarchy3200 wasnt confused...he was correct in the code he knocked up and should have provided a bit of clarity in how to make the code your asking for. His code simply displays the "<td>What ever</td>" twice, then places a "</tr><tr>" which what your aking for. You could have edited the echo "<td>What ever</td>" to display what ever you want....

LoRd_aLmIghTy
12-26-2006, 04:42 PM
thanks daniel thats pretty much what i meant except for the no data thing. i meant for each <td> with no data i wanted it to have a thumb of my choosing and text saying no entrie. so basicly if i only had 2 tutorials the top <tr> would have them and the bottom 4 <tr>'s would have each <td> saying no content. can you show me that code please.

thanks

anarchy3200
12-26-2006, 05:37 PM
Hi, sorry about before, i did try to update my post and add another reply but my internet kept dropping out...
As to the alternate text if an entry doesn't exist try replacing the lines:


echo ' width="10%">Thumb for tut w/ highest score here</td>'; echo ' <td width="40%"><a href="' . $result["link_to_tut"] . '">Name of tutorial</a> - Description etc....</td';


with something like:


if(strlen($result["link_to_tut"])>0){
echo ' width="10%">Thumb for tut w/ highest score here</td>
'; echo ' <td width="40%"><a href="' . $result["link_to_tut"] . '">Name of tutorial</a> - Description etc....</td';
}else{
echo ' width="10%">Thumb Here</td>
'; echo ' <td width="40%">No Content</td';

}



With this if there is a value of anysort returned from the query then the first echo will run and the value will be displayed, if there is not anything returned from the query the second set of echo's are shows displaying that there is no content.

LoRd_aLmIghTy
12-26-2006, 06:41 PM
thanks for that thats 50% of what i was asking. the other part is lets say i have 3 tutorials and i set my query in that script to limit 6 i want the 3 to b displayed which they do and the right <td> next to the third goes to no content which i want. but i also want the remaining last 2 <tr>s to display No content in their <td>'s as well.

Thanks everyone

anarchy3200
12-26-2006, 07:56 PM
Updated soon, code removed as it was wrong..!

dumpfi
12-26-2006, 08:10 PM
Something like this?
<?php

// ...

define('SHOWN_TUTORIALS', 10);

echo '<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
';
$columns = 2;
$resultSet = mysql_query('SELECT * FROM `table` ORDER BY `highscore` DESC LIMIT 0,'.SHOWN_TUTORIALS);
//$rowNum = mysql_num_rows($resultSet);
// if($rowNum == 0)
// {
// echo ' <td align="center">No results returned!</td>';
// }
// else
// {
$rowNum = SHOWN_TUTORIALS;
if($rowNum % $columns != 0)
{
$rowNum += $columns - $rowNum % $columns;
}
while($rowNum-- > 0)
{
echo ' <td';
if($result = mysql_fetch_assoc($resultSet))
{

//Edit these echo's

echo ' width="10%">Thumb for tut w/ highest score here</td>
'; echo ' <td width="40%"><a href="' . $result["link_to_tut"] . '">Name of tutorial</a> - Description etc....';

//Dont edit beyond here

}
else
{
echo ' width="10%">Thumb Here</td>
<td width="40%">No Content';
}
echo '</td>
';
if($rowNum != 0 && $rowNum % $columns == 0)
{
echo ' </tr>
<tr>
';
}
}
//}
echo ' </tr>
</table>';

?>dumpfi

LoRd_aLmIghTy
12-26-2006, 08:37 PM
dumpfi your code does exactly what danielwarners does with anarchy3200 modification. And anarchy3200 did you mean the code you posted as an addon to daniels or as a solution to my second problem?

thanks all

dumpfi
12-26-2006, 11:05 PM
With my modification the script does display the remaining table rows as you wanted. However, I've edited it to remove some superfluous </td> tags.

dumpfi

danielwarner
12-27-2006, 01:57 AM
<?php

// ...

echo '<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>';

$columns = 2;
$resultSet = mysql_query('SELECT * FROM `table` ORDER BY `highscore` DESC LIMIT 0,10');
$rowNum = mysql_num_rows($resultSet);

if($rowNum % $columns != 0)
{
$rowNum += $columns - $rowNum % $columns;
} $maxResults = 10;
while($maxResults-- > 0)
{
if($result = mysql_fetch_assoc($resultSet))
{

//Active Tutorial
echo '
<td width="10%">Thumb for tut w/ highest score here</td>
<td width="40%"><a href="....">Name of tutorial</a> - Description etc....</td>';

} else {

//Blank Results
echo '
<td width="10%">No Highscore</td>
<td width="40%">No Highscore</td>';

}
if($maxResults != 0 && $maxResults % $columns == 0)
{
echo '
</tr>
<tr>';
}
}

echo '
</tr>
</table>';

?> ...Havn't tested it though

LoRd_aLmIghTy
12-27-2006, 03:53 PM
perfect thats exactly what i was looking for. and their were no errors with it either



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum