hi to all

i am making a chat app using php/mysql

i am faceing a problem here ..i want that user click on button and it give him a random online user from database...but dont know how to do it ..i am trying..

{
           $i=0;
            while ($i < $num) 
            {
             $f1=mysql_result($result,$i,"MemberUserName");
              $i++;
<a href="#" onclick="setCurrentUser('<?php echo $f1; ?>');">
            }
            }

here i am getting values from database and storing them into $f1 and calling setCurrentUser to pass user...but i want random..

function setCurrentUser (userName)
{	
if (isMoiveLoaded = true)
{ 	
if (navigator.appName.indexOf("Microsoft") != -1)
{
					document.getElementById('chatApp').SendUserName(userName);
}
else
{
								document.chatApp.SendUserName(userName);
}
}
else
{
alert ("let the flash content load firt please");
}
}

i just want that onclick give me random user for chat ...

Recommended Answers

All 5 Replies

Did you 'hard-coded' the mySQL look-up? I mean, it would be hard-coded if you specify the mySQL to look for a specific 'userName' in the data base. As a result, your mySQL will always return the same user. What you may need to do is either create a pool of userName and random select from the pool to get mySQL result, or use your server side script code to random select a user (using order by RAND()) and return it.

i am getting the users list and on clicking on that user it send to chat app ..but i want a button which send random user..

As I said earlier, you specify a user by using whatever user name is. Do you know how to 'select' data from database in mySQL? For example, a query for selecting a random record from a database table called 'users' is...

"SELECT * FROM users ORDER BY Rand() LIMIT 1";

If you do so, you should get a random user record back. Is that what you want???

By the way, this question is not about javascript, but it is mySQL or PHP thing...

you are right but this will give me only one use from chat and to get other user i have to refresh the page which i dont want ...so i want to get a random user without refreshing the page .. i think this make some things clear..

in my code you can see that it will give me userlist one time only and i have to refresh the page to get new users ...and by clicking on one of them it pass that user value to javascript form chat ....but i want a one button only to send random user for chat ...without refreshing ...

If you want content of a page to be reloaded without refreshing the page, you may need to use Ajax. However, if I understand correctly, you have already obtain a user list at one time already, so you could actually collect all user name of the list in an array. Then, randomly select a user name from the list whenever you want. I am not sure about PHP format because I have not used it for over 3 years. I hope that below code will give you some idea. I added some code in your original version.

// create an array for use in javascript as a global variable if you want
var userNameArray = new Array()
{
  $i=0;
  while ($i < $num) {
    $f1=mysql_result($result,$i,"MemberUserName");
    $i++;
    <a href="#" onclick="setCurrentUser('<?php echo $f1; ?>');">
    // here, push the $f1 value to the user array
    userNameArray.push('<?php echo $f1; ?>')
  }
  // once done, you have a filled userNameArray to play with
  <a href="#" onclick="setRandomCurrentUser()">
}

// do the random user in here
function setRandomCurrentUser() {
  if (isMoiveLoaded = true) {
    // random pick a user name from the array
    var userName = userNameArray[Math.floor(Math.random()*userNameArray.length)]
    if (navigator.appName.indexOf("Microsoft") != -1) {
      document.getElementById('chatApp').SendUserName(userName);
    }
    else {
      document.chatApp.SendUserName(userName);
    }
  }
  else {
    alert ("let the flash content load firt please");
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.