admin管理员组文章数量:1302341
I am pretty new to extending wordpress and trying to get my head around using Mysql and PHP to fetch data into a html table and print it on a wp page. What i have so far is fetching all form data to display a pass or fail by matching the result from a score and max_score column in the table wp_testing_data
This is the tricky part that i just cannot find any details of how to make the query. If the current user visits the page with the html table output, i want it to only display entries from other users that have the same site_id in the wp_usermeta table as the current user
In theory, the user viewing the page has a site_id entry in the wp_usermeta table of DE49. Now i want to only show the pass or fail results table if the results are from other users with the site_id value equal to the current users site_id value. The site_id will be unique to a group of users, so putting the actual result (DE49) as the target isn't the answer. It needs to be dynamic to the user viewing the page at that time (current user). Basically, you can only see the results from members of your own group. In this case the group is stored in the site_id column of the wp_usermeta table. I can get all results using the following, but cannot figure out only getting results from users with a matching meta value to the current user. Does anyone know how to write the query to add the conditions above to the query i use below to get the filtered data only?
global $wpdb;
$result = $wpdb->get_results( "SELECT f_name, l_name, date, IF(score = max_score, 'PASSED','FAILED') AS score
FROM wp_testing_data, wp_usermeta
WHERE wp_usermeta.meta_key = 'site_id'
AND wp_usermeta.meta_value = 'DE49'");
echo "<style>";
echo "body {font-family: Arial;}";
echo ".table_container { padding: 10px 12px 0px 12px; border: 1px solid #ccc; }";
echo ".table_container th { background-color:lightblue; color:#000; font-weight:bold; border-left: 1px solid white;}";
echo "</style></head>";
echo "<body>";
echo "<div class=\"table_container\"><table>";
echo "<tr><th style=\"padding-left:10px;\">First Name</th><th>Last Name</th><th>Date</th><th>Score</th></tr>";
foreach ($result as $row) {
echo "<tr><td>" . $row->f_name. "</td><td>" . $row->l_name . "</td><td>" . $row->date . "</td><td>" . $row->score. "</tr>";
}
echo "</table></div>";
Any guides or rough ideas would be greatly appreciated and thanks for taking the time to read my post.
I am pretty new to extending wordpress and trying to get my head around using Mysql and PHP to fetch data into a html table and print it on a wp page. What i have so far is fetching all form data to display a pass or fail by matching the result from a score and max_score column in the table wp_testing_data
This is the tricky part that i just cannot find any details of how to make the query. If the current user visits the page with the html table output, i want it to only display entries from other users that have the same site_id in the wp_usermeta table as the current user
In theory, the user viewing the page has a site_id entry in the wp_usermeta table of DE49. Now i want to only show the pass or fail results table if the results are from other users with the site_id value equal to the current users site_id value. The site_id will be unique to a group of users, so putting the actual result (DE49) as the target isn't the answer. It needs to be dynamic to the user viewing the page at that time (current user). Basically, you can only see the results from members of your own group. In this case the group is stored in the site_id column of the wp_usermeta table. I can get all results using the following, but cannot figure out only getting results from users with a matching meta value to the current user. Does anyone know how to write the query to add the conditions above to the query i use below to get the filtered data only?
global $wpdb;
$result = $wpdb->get_results( "SELECT f_name, l_name, date, IF(score = max_score, 'PASSED','FAILED') AS score
FROM wp_testing_data, wp_usermeta
WHERE wp_usermeta.meta_key = 'site_id'
AND wp_usermeta.meta_value = 'DE49'");
echo "<style>";
echo "body {font-family: Arial;}";
echo ".table_container { padding: 10px 12px 0px 12px; border: 1px solid #ccc; }";
echo ".table_container th { background-color:lightblue; color:#000; font-weight:bold; border-left: 1px solid white;}";
echo "</style></head>";
echo "<body>";
echo "<div class=\"table_container\"><table>";
echo "<tr><th style=\"padding-left:10px;\">First Name</th><th>Last Name</th><th>Date</th><th>Score</th></tr>";
foreach ($result as $row) {
echo "<tr><td>" . $row->f_name. "</td><td>" . $row->l_name . "</td><td>" . $row->date . "</td><td>" . $row->score. "</tr>";
}
echo "</table></div>";
Any guides or rough ideas would be greatly appreciated and thanks for taking the time to read my post.
Share Improve this question edited Feb 28, 2021 at 17:17 Davey asked Feb 28, 2021 at 11:03 DaveyDavey 33 bronze badges1 Answer
Reset to default 0You can use get_current_user_id to get the id of the user currently viewing the page, and get_user_meta to get the site_id associated with that user. For example:
<?php
$user_id = get_current_user_id();
$site_id = get_user_meta($user_id, 'site_id', true);
Note: This does assume there is a user logged in, you might get unexpected results if there isn't. So assuming you haven't already done so, make sure the page can only be viewed by a logged in user.
To then use this site_id to get the scores per 'site', you can use a LEFT JOIN. The query you can use is:
$result = $wpdb->get_results("SELECT f_name, l_name, IF(tdata.score = tdata.maxscore, 'PASSED', 'FAILED') as score FROM wp_usermeta umeta LEFT JOIN wp_testing_data tdata ON tdata.user_id = umeta.user_id WHERE umeta.meta_key = 'site_id' AND umeta.meta_value = '" . $site_id . "'");
Do note that this will give you a query result for every user that has a site_id associated that matches $site_id, even if there's no corresponding result in wp_testing_data. If you ONLY want those that have testing data, use INNER JOIN instead of LEFT JOIN.
本文标签: customizationGet All Results From Other Users That Share The Same Custom User Meta As Current User
版权声明:本文标题:customization - Get All Results From Other Users That Share The Same Custom User Meta As Current User 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741714623a2394036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论