KefkaIIV Posted July 22, 2006 Share Posted July 22, 2006 Okay, I am learning PHP, and I simply dread MySQL. Something I wanted to do just popped into my head, which had been a long thought over topic of mine. So bare with me guys, and help me learn how to make this quick script, okay, thanks.[b]T[/b]he goal of the script I want is to display a forum's member list. On the homepage of my website, which is not connected to my "/forum" in any way, I'd like it to have a box at the right, with five names. The names would be the "Usernames" of people from my forum. Now, I know I can just add the usernames to the list, and have it show a random 5, but that would be very difficult for growing forums. Before I ever thought of this idea, I used a script similar to this:[code]<?$bgcolor = '#FFFFCC';$textcolor = 'black';// Array Structure: "Quote","Author"$allqts = array ("The Black Knight Always Triumphs!", "Monty Python", "I swear by my life and love of it that I will never live for the sake of<br>another man, nor ask another man to live for mine" , "Atlas Shrugged", "It is clear that the individual who persecutes a man, his brother,<br> because he is not of the same opinion, is a monster", "Voltaire", "I agree that there is a natural aristocracy among men<br>The grounds of this are virtue and talents.", "Thomas Jefferson", "Liberty, when it begins to take root, is a plant of rapid growth.", "George Washington", "Never argue with an idiot. <br>They drag you down to their level <br>then beat you with experience", "Dilbert", "The Answer is 42. What is the question?", "Hitchikers Guide to the Galaxy", "Anyone who has never made a mistake has never tried anything new", "Albert Einstein", "Progress doesn't come from early risers, progress is made<br>by lazy men looking for easier ways to do things.", "Lazarus Long <font size=-2>(Time Enough for Love by Robert A. Heinlein)</font>", "A little learning is a dangerous thing; Drink deep, or taste not the<br>Pierian spring. There shallow draughts intoxicate the brain, <br>and drinking largely sobers us again", "Alexander Pope", "The early bird gets the worm, but the second mouse gets the cheese", "Anonymous", "Subjugating the enemy's army without fighting is the true pinnacle of excellence", "Sun-tzu, The Art of War", "Work as though you were to live 100 years; pray as if you were to die tomorrow", "Benjamin Franklin", "The world is a stage, but the play is badly cast", "William Shakespeare", "Truth is generally the best vindication against slander.", "Abraham Lincoln", "...mercy to the guilty is cruelty to the innocent...", "Adam Smith", "...I wish that I may never think the smiles of the great and powerful<br> a sufficient inducement to turn aside from the straight path<br> of honesty and the convictions of my own mind", "David Ricardo", "Democracy is the worst form of government except for all the others", "Winston Churchill", "You can only know the highest peaks if you have experianced the lowest valley's", "Richard Nixon", "They dress the wound of my people as though it were not serious. <br>'Peace, peace,' they say, when there is no peace.", "Jeremiah 6:14", "It is better to remain silent and be thought a fool<br> than to open your mouth and remove all doubt.", "Jonathan Swift", "The market system delivers the goods people want,<br>but those who make it work cannot readily explain why it is so.<br>The socialst or communist system does not deliver the goods, <br>but those who operate it can readily explain away its failure.", "F.A. Hayek, Law, Legislation and Liberty, Vol. II" );// Gets the Total number of Items in the array// Divides by 2 because there is a Quote followed by an Author$totalqts = (count($allqts)/2);// Subtracted 1 from the total because '0' is not accounted for otherwise$nmbr = (rand(0,($totalqts-1)));$nmbr = $nmbr*2;$quote = $allqts[$nmbr]; $nmbr = $nmbr+1;$author = $allqts[$nmbr];// You can delete this section// it is only so Search engines can find itif ($PHP_SELF == "/quotes.php") { echo "<Title>Random Quote Script for PHP</title>"; echo "<meta name=\"Description\" content=\"Random Quote script written in PHP\">"; echo "<meta name=\"keywords\" content=\"Random Quote script written in PHP\">"; echo "This is my random quote script that is written in PHP"; echo "<br>Feel free to use it for whatever you want<br/><br/>";}/// End Delete$space = "<font color=$bgcolor>.....................................</font>";$comments = "<br><center><font size='-2'><i><a href='quotes.php'>Random Quote Script by Dave</a></i></font></center>";echo "<center>";echo "<Font color=$textcolor><i>";echo "$quote<br>";echo "</i></font>";echo "$space $author";echo "$comments";echo "</center>";IF ($PHP_SELF == "/quotes.php") { show_source("quotes.php"); }?>[/code]And I would keep adding more "Usernames" to the array. Of course, I used "Usernames" instead of quotes, as this script does. So, how do I get a MySQL database to fetch usernames, and have it work similar to this, but being from the tables, it adds new members? If I am confused on how MySQL works, please tell me. I am not very intelligent when it comes to MySQL. Any information about this subject would be appreciated, thank you. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 22, 2006 Share Posted July 22, 2006 Maybe I missed something.. you simply want to store usernames/quotes/whatever in MySQL? Quote Link to comment Share on other sites More sharing options...
KefkaIIV Posted July 22, 2006 Author Share Posted July 22, 2006 No.I want the usernames of my forum to be displayed on my homepage.How do I get MySQL to display them? And, make them randomized (Like only 5 out of lets say, 1000 members.) Quote Link to comment Share on other sites More sharing options...
phil.t Posted July 23, 2006 Share Posted July 23, 2006 Here is one way you could retrieve the results:[code]$query = 'SELECT username FROM members';$result = mysql_query($query);$max = mysql_num_rows($result) - 1;// Display 5 random usernames; to display more change the 5for ($i=0; $i < 5; $i++){ $user_number = rand(0, $max); $user = mysql_result($result, $user_number, 'username'); echo $user . '<br>';}[/code]The mysql_result function accepts three parameters. The result set, the row, and the column. If you were displaying random users (as in this case), the row number is a random number whose MAX is the number of rows returned in the result set. The column can be accessed by its number or its name.I hope I understood correctly what you are trying to do. No matter how many users you had, this would choose five random from among them all and return their username. You could then add them to an array for further output processing, output them on the spot, or whatever. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 23, 2006 Share Posted July 23, 2006 Well, no need to retrieve all the members, and throw away all but 5... an "ORDER BY RAND() LIMIT 5" will work just as well. Quote Link to comment Share on other sites More sharing options...
KefkaIIV Posted July 24, 2006 Author Share Posted July 24, 2006 Okay, so that code that was displayed, what do I need to change so it will fit? Quote Link to comment Share on other sites More sharing options...
fenway Posted July 24, 2006 Share Posted July 24, 2006 I don't understand your question... just add the aforementioned order by clause. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.