Jump to content

turning each array value into a link


Zhyrios

Recommended Posts

Hey i'm having trouble figuring out how to do this.

This is the code grabbing the users who are online and listing them one by one.

$qt=mysql_query("SELECT username FROM users WHERE lastvisit > '$tm' and online='ON'");
echo mysql_error();

while($nt=mysql_fetch_array($qt)){
echo "<tr><td> $nt[username] </td></tr>";
}
?>

 

what i want to do is have each username be a hyperlink that links to their profile how would i go about this? cause i'm completely clueless on this. is there some way i can do it in the php or will i need to make a varchar column for each player that holds their profile link and grab that with the query too?

Link to comment
Share on other sites

if ($result =mysql_query("SELECT id, username FROM users WHERE lastvisit > '$tm' and online='ON'")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)){
      echo "<tr><td><a href=\"profile.php?id={$row['id']}\">$row['username']</td></tr>";
    }
  }
}
?>

 

Then on profile.php have a query that uses the value passed in through $_GET['id'] to that gets a users details.

Link to comment
Share on other sites

I assume that you will be linking to a profile page (call it profile.php for sake of argument).  Your current code needs a couple of changes:

$qry = "SELECT userID, username FROM users WHERE lastvisit > '$tm' and online='ON'"
$qt=mysql_query($qry) or die("An Error Occured trying to run the following:<br>$qry<br><br>The Server Responded With:<br>".mysql_error());
while($nt=mysql_fetch_array($qt)){
echo "<tr><td><a href=\"profile.php?uid={$nt['userID']}\"> {$nt['username']} </a></td></tr>";
}
?>

then on the profile.php have the following code:

<?php
if(isset($_GET['uid'])){
$id = $_GET['uid'];
$sql = "SELECT all, profile, info, fileds FROM profileTable WHERE userID = "id";
$result = mysql_query($sql) or die ("An Error Occured trying to run the following:<br>$qry<br><br>The Server Responded With:<br>".mysql_error());
while($row = mysql_fetch_assoc($result){
//display fields
}
}
?>

That's obviously the bare minimum, and you will need to adapt it to your SQL info, but hopefully it's something that you can work with.

 

Please look over the revision I made to your original code and note the changes, perticulaty the use of single quotes within [], and the use of {} around array key reffrences inside of strings.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.