Page 1 of 1

help with user profile PHP

Posted: Thu Jul 19, 2012 5:32 pm
by aduss
Hello, I would like to ask.. I have table where are users, i would like to retrieve data which will be display in table and every username should be link, when anybody click on link the user profile will show.

and one more question is possible to order it by 2 columns for example: by level and by experience.

Thanks a lot ;)

Re: help with user profile PHP

Posted: Thu Jul 19, 2012 5:53 pm
by vitinho444
You want to gather all players information?

Then do:

Code: Select all

$query = "SELECT * from users";

echo "<table border=1><tr><td>Username</td><td>Gold</td></tr>";

while($player_info = mysql_fetch_assoc($query))
{
$username = $player_info['username'];
$gold = $player_info['gold'];

$other_info = $player_info['table_field_name'];

//then you want the link
//€ EDIT i forgot the table




echo "<tr><td><a href='profile.php?username='. $username . '> $username </a></td><td>$gold</td></tr>"; 

//this will display the links with the player username, i recommend a GET function on the profile page, so you can link easily.
}
It a basic code for displaying the users with click able links to the profile.

Then in the profile.php

Code: Select all

if(isset($_GET['username']))
{
$username = $_GET['username'];
$query = mysql_fetch_array(mysql_query("SELECT * from users where username='$username' "));

$username = $query['username']; 
$gold = $query['gold'];
$email = $query['email'];

echo "$username 's Profile<br> Gold: $gold  <br> Email: $email";

//I think it should work.. I wrote this manually without checking my profile.php so it might have any error.. 
//if it does just reply :D


//now just present the player info
}
It should do what you want if i understood all

Re: help with user profile PHP

Posted: Thu Jul 19, 2012 6:08 pm
by Jackolantern
EDIT: You beat me to it! lol :?

I am assuming you are probably using mysql extension rather than mysqli since that is the extension of most tutorials. If you are using mysqli (which you should be), let me know and I can write it up in mysqli. I also assume the names of your table and their columns, so you will need to change those. I have not tested this code, but it should work :)

Code: Select all

$sql = "SELECT id, name, level, xp FROM players ORDER BY level, experience";
$result = mysql_query($sql);

//Do error checking to make sure nothing went wrong
if (!$result) {
    echo "Could not connect to database: " . mysql_error();   //remove the mysql_error() before making the game public
    exit;
}

//Make sure results were returned
if (mysql_num_rows($result) == 0) {
    echo "No players found in results.";
    exit;
}

//Start beginning of table
echo "<table>";
echo "   <tr>";
echo "      <th>Name</th>";
echo "      <th>Level</th>";
echo "      <th>Experience</th>";
echo "   </tr>";

//Loop through results and format into table with clickable links to player profiles
while ($row = mysql_fetch_assoc($result)) {
     echo "   <tr>";

       //this next line takes a bit of piecing together. What we are doing is making a table data point that has the player name 
       //   ...linked to a "profiles.php" page and it is sending the id along in a GET query string. Retrieve this on the profiles page
       //   ...and query there for the player's profile info

     echo "      <td><a href='profiles.php?id=".$row['id']."'>".$row['name']."</a></td>";
     echo "      <td>".$row['level']."</td>";
     echo "      <td>".$row['xp']."</td>";
     echo "   </tr>";
}

//Close the table
echo "</table>"; 

Re: help with user profile PHP

Posted: Fri Jul 20, 2012 6:39 pm
by aduss
Thanks ^_^ ;)