Tutorials
Listing members separated by Favorite Field
-
Listing members separated by Favorite Field
Posted on September 3, 2003
Written by
Louise (view more by Louise)
Comments (0)
Filed under Scripts: PHPFanBase, Tutorials
Say you have a fanlisting, you use PHPFanBase, and you have your favorite field enabled. Now, say you want to list your members separated by their favorite, like you can do with the countries (such as my Romantic Comedies fanlisting). How would you do that?
That’s very simple, actually. First, open up your HTML editor (such as notepad), create a new file, and save it as fave.php. Now copy and paste this into the file:
<?php include_once 'config.php';
include $header;
$query = mysql_query('SELECT * FROM ' . $table . " WHERE apr = 'y' AND fave = '" . $id . "' ORDER BY " . $countrysort . ' ASC');
$num = mysql_num_rows($result);
?>
<h1><?php
if ($num == 1) echo $num . ' member loves';
else echo $num . ' members love';
echo $id; ?></h1>
<div align="center">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td><b>ID</b></td>
<td><b>Name</b></td>
<td><center><b>@</b></center></td>
<td><center><b>URL</b></center></td>
<td><b>Country</b></td>
</tr>
<?php
while($row = mysql_fetch_assoc($query)) {
$id = sprintf("%03.0f", $row['id']); ?>
<tr>
<td>[<?php echo $id; ?>]</td>
<td><?php echo $row['name']; ?></td>
<?php if ($row['hide'] != 'h') {
$array = explode('@', $row['email']);
$first = $array[0];
$last = $array[1]; ?>
<td><center>
<script type="text/javascript">
emailE = ('<?php echo $first; ?>' + '@' + '<?php echo $last; ?>');
document.write('<a href="mailto:' + emailE + '">' + '@' +'<\/a>');
</script>
</center></td>
<?php }
else echo "<td><center><s>@</s></center></td>";
if (empty($url)) echo "<td><center><s>www</s><center></td>";
else echo '<td><center><a href="' . $row['url'] . '">www</a></center></td>';
echo '<td>' . $row['country'] . '</td></tr>';
}
echo '</table></div>';
?>
<p>Also browse by <a href="members.php?id=country">country</a>, <a href="members.php?id=fave">favorite</a> or see the <a href="members.php?id=all">complete list</a>.</p>
<?php include $footer; ?>
Don’t forget to save it! Now open up your members.php file. Erase everything in there. Now copy this and paste into it:
<?php
include_once("config.php");
include("$header");
if ($id == 'all'){
$query="SELECT * FROM $table WHERE apr='y' ORDER BY $allsort ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
<h1><?=$num?> Members</h1>
<div align="center"><table border="0" cellspacing="0" cellpadding="2">
<tr>
<td><b>ID</b></td>
<td><b>Name</b></td>
<td><b><center>@</center></td>
<td><b><center>URL</center></b></td>
<td><b><center>Country</center></b></td>
<?php if ($enablefave == 'Y'){ ?>
<td><b>Favorite</b></td> <? } ?>
</tr>
<?php
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$email=mysql_result($result,$i,"email");
$url=mysql_result($result,$i,"url");
$country=mysql_result($result,$i,"country");
$hide=mysql_result($result,$i,"hideemail");
if ($enablefave == 'Y'){
$fave=mysql_result($result,$i,"fave"); }
$id = sprintf("%03.0f", $id);
echo "<td>[$id]</td>
<td>$name</td>";
if ($hide != "h"){
$array = explode("@", $email);
$first = $array[0];
$last = $array[1];?>
<td><center><SCRIPT TYPE="text/javascript">
emailE=('<?=$first?>' + '@' + '<?=$last?>')
document.write(
'<A HREF="mailto:' + emailE + '">'
+ '@' +'</a>'
)
</SCRIPT></center></td>
<?php } else {
echo "<td><center><s>@</s></center></td>"; }
if ($url == "") {
echo "<td><center><s>www</s></center></td>";
} elseif ($url !== "") {
echo "<td><center><a href="$url" target="_blank">www</a></center></td>"; }
echo "<td><center>$country</center></td>";
if ($enablefave == 'Y'){
echo "<td>$fave</td></tr>";
} else { echo "</tr>"; }
++$i;
}
echo "</table></div>"; ?>
<p>Also view sorted by <a href="members.php?id=country">country</a> or <a href="members.php?id=fave">favorite</a></p>
<?php } elseif ($id == 'fave'){
$query="SELECT DISTINCT fave FROM $table WHERE apr='y' ORDER BY fave ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
<h1>Members Favorite Character</h1>
<p>Click on the character's name to see the members who liked that character the best.</p>
<div align="center"><table border="0" cellspacing="0" cellpadding="5">
<tr>
<td><b>Favorite Character</b></td>
<td><b>Fans</b></td></tr>
<?php
$i=0;
while ($i < $num) {
$fave=mysql_result($result,$i,"fave");
$query3="SELECT id FROM $table WHERE apr='y' AND fave='$fave'";
$result3=mysql_query($query3);
$num3=mysql_numrows($result3);
echo "<td><a href="fave.php?id=$fave">$fave</a></td>";
if ($num3 == "1"){
echo "<td>$num3 fan</td></tr>";
} else {
echo "<td>$num3 fans</td></tr>";
}
++$i;
}
echo "</table></div>";
?>
<p>You can also browse <a href="members.php?id=country">by country</A> or click <a href="members.php?id=all">here</a> to see all members</p>
<?php } elseif ($id == 'country'){
$query="SELECT DISTINCT country FROM $table WHERE apr='y' ORDER BY country ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
<h1><?=$num?> Countries</h1>
<p>Click on the country name to see the members from that country.</p>
<div align="center"><table border="0" cellspacing="0" cellpadding="5">
<tr>
<td><b>Country</b></td>
<td><b>Fans</b></td></tr>
<?
$i=0;
while ($i < $num) {
$country=mysql_result($result,$i,"country");
$query2="SELECT id FROM $table WHERE apr='y' AND country='$country'";
$result2=mysql_query($query2);
$num2=mysql_numrows($result2);
echo "<td><a href="country.php?id=$country">$country</a></td>";
if ($num2 == "1"){
echo "<td>$num2 fan</td></tr>";
} else {
echo "<td>$num2 fans</td></tr>";
}
++$i;
}
echo "</table></div>";
?>
<p>You can also browse by <a href="members.php?id=fave">favorite</a> or click <a href="members.php?id=all">here</a> to view the whole list.</p>
<?php } else {
include("config.php");
$query="SELECT * FROM $table WHERE apr='y'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$query4="SELECT DISTINCT country FROM $table WHERE apr='y'";
$result4=mysql_query($query4);
$num4=mysql_numrows($result4);
?>
<h1>MEMBERS</h1>
<p>We have a total of <b><?=$num?></b> members from <b><?=$num4?></b> countries!
<br>You can browse them by:<br>
<A HREF="members.php?id=country">Country</A><br>
<A HREF="members.php?id=fave">Favorite Character</A><br>
<A HREF="members.php?id=all">All Members</A>
</p>
<div align="center"><p>Powered by <a href="http://www.codegrrl.com" target="_blank">PHPFanBase</a> 2.0</p></div>
<?php } include("$footer");
?>
Now, if your favorite field asks what the member’s favorite character is, you’re all set. However, if you are asking something else (such as favorite song or movie), you have to edit this file a little bit. Go to lines 76-80. They say:
<h1>Members Favorite Character</h1> <p>Click on the character's name to see the members who liked that character the best.</p> <div align="center"><table border="0" cellspacing="0" cellpadding="5"> <tr> <td><b>Favorite Character</b></td>
Of course, change it to reflect your favorite field.
And finally, the last step. Go to line 160. It’s really close to the end of the file. It says:
<A HREF="members.php?id=fave">Favorite Character</A><br>
Again, just change it to reflect your favorite field.
Now upload the new files and you’re all done!
Tips and Tricks
- The list looks better if you only allow one favorite thing per member. Otherwise, it might get a little bit messy.
- Double quotes (") or simple quotes (’) on the favorite field might give you errors. Just use this (`) instead.
- It works just like the countries: it will show any different answers you may have on that field. What does this mean? Well, say you have the Friends fanlisting, and your favorite field is asking about the member’s favorite character. Now, a member can answer "Rachel", the other one might answer "Rachel Green", and another one might say "Rachel Greene". Then you get all three on your list, when, obviously, they all mean the same character. And then your stuck with all three answers on "Member’s Favorite Character" page/list, and the members, which should be all on one list, get split into three lists. So you probably want to edit your members’ profiles, so you have only one answer.
- Also, a good solution for this problem is using a dropdown menu for your favorite field.
Comments
Comments are closed for this entry.