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
https://forums.phpfreaks.com/topic/259399-turning-each-array-value-into-a-link/
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.

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.