0

I have code that returns a list of records and one of the columns is my users column which displays as a hyperlink. I want to make it so if you click on a user (display_name in the code) you will be taken to the bio page for that user. I'm sure I need to user get['id'] but I'm having trouble figuring out how. Currently I can only grab the display name for the current user. How do I write this so that the URL will currently append the user_ID for the user/display name that is clicked on so that the bio for that user will come up dynamically?

There are two tables. The username/display name is stored in users table and the bio information is storied in users_meta table.

echo "<td><a href=\"http://example.com/portfolio/\">$row->display_name</a></td>";
user5000
  • 129
  • 1
  • 9
  • does your users table have a user_id column or id column ? what is exactly your db structure – MikeWu Jul 26 '14 at 19:25

1 Answers1

0

If $row->display_name returns as you expect it. Then concatenate your Serversided to Client sided:

echo '<td><a href="http://example.com/portfolio/">'.$row->display_name.'</></td>';
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • Yes, display_name returns as expected, which means it displays the user name on the page as hyperlink. Now I need the hyperlink to append the userId to url so when people click to get the bio for that user they are taken to it by clicking that link. Are you saying to skip the userID and get the bio using the display name? – user5000 Jul 26 '14 at 19:23
  • @user5000 How are you accessing the UserID? `$rows->UserID`? or otherwise. It'll help me update my answer to give you the best possible outcome – Daryl Gill Jul 26 '14 at 19:30
  • I am getting the display_name to output on the page through this join query: $results = $wpdb->get_results("SELECT stories.story_name, stories.genre, wp_users.display_name FROM stories LEFT JOIN wp_users ON stories.ID=wp_users.ID where stories.active = 1"); – user5000 Jul 26 '14 at 20:48
  • @user5000 You have to select the ID you want from the table you want, before it's pulled into the result set – Daryl Gill Jul 26 '14 at 21:07
  • I'll try that. So do I need a second query to do a join to get the userID and store in a variable and then append that variable to the URL? something like example.com?variable ?? And to grab that variable on the bio page do I just need use post[variable] to get the bio to show data for that user that was click on? – user5000 Jul 26 '14 at 21:12
  • Depends how you've got your htaccess setup. a Normal request might look like: `http://example.com/portfolio.php?UserID=$rows->UserID` – Daryl Gill Jul 26 '14 at 21:16
  • That would work. My issue is still how to get the wp_users_ID. I tried to add that to the existing join query and that returned no results. So do I need a separate query to get the wp_users_ID variable, perhaps using a different variable name for the query like: $biopageID = $wpdb->get_results("SELECT wp_users_ID FROM wp_users WHERE stories.ID=wp_users.ID); ? Then do http://example.com/portfolio.php?biopageID ? – user5000 Jul 26 '14 at 22:06
  • OK-I'm getting there. I have the code appending the display_name in the URL and passing it to the bio page for that user. I do this with echo "display_name\">$row->display_name"; Then I can display the user name on the bio page using $_GET['user'] The last part is how do I also grab the rest of the user's data from a table on the bio page if this approach only passes the actual user name? – user5000 Jul 27 '14 at 00:03
  • @user5000 If your usernames are unique, then this will go off without a problem. be sure to protect your site/page from injection attacks such as Javascript Injection ^ improperly formatted queries (a question I asked myself might shed some light on "SQL Injection") http://stackoverflow.com/questions/14311686/properly-escaping-with-mysqli-query-over-prepared-statements. Perform a query on the portfolio page to pull all the necessary information from the database to be displayed on the page – Daryl Gill Jul 27 '14 at 00:20
  • Last question for this I think: On the bio page, I now have the user name from the users table. That user has a bunch of data in another table called stories. How would I take the user name I now I brought over to this page and use that to access data in stories table for this user? I suppose I first need to convert the user name to the user's user_ID first since I do have the userID field in both stories and users table, right? But how would I do this. – user5000 Jul 27 '14 at 00:26
  • So I changed it so that I am passing the ID instead of display name to the bio page. But now the opposite question...on the bio page how would I then take the ID and use that to display the user name? – user5000 Jul 27 '14 at 00:46
  • @user5000 `select bla,bla,bla,bla FROM table WHERE UserID=$_GET['UserID'];` for example. But before to use prepared statements & do not trust your user input direct – Daryl Gill Jul 27 '14 at 00:51
  • Got it all. Working like a charm. Now I have to read up on sql injection which will likely mean another stackoverflow question later when it's all messed up again when I change code to prevent sql injection. Ugh. – user5000 Jul 27 '14 at 01:17
  • @user5000 Always develop with security in mind. Do not neglect such a thing. You will regret it – Daryl Gill Jul 27 '14 at 01:29