Skip to main content
remove backticks from query because they were causing issues
Source Link

You 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`f_name, `l_name`l_name, IF(`tdata`tdata.`score`score = `tdata`tdata.`maxscore`maxscore, 'PASSED', 'FAILED') as `score`score FROM `wp_usermeta`wp_usermeta `umeta`umeta LEFT JOIN `wp_testing_data`wp_testing_data `tdata`tdata ON `tdata`tdata.user_id = `umeta`umeta.user_id WHERE `umeta`umeta.meta_key = 'site_id' AND `umeta`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.

You 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.

You 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.

small fixes
Source Link

You 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_result>get_results("SELECT `f_name`, `l_name`, IF(`tdata`.`score` = `tdata`.`maxscore`, 'PASSED', 'FAILED') as `score` FROM `c5k0o2h7gu_usermeta``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.

You 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_result("SELECT `f_name`, `l_name`, IF(`tdata`.`score` = `tdata`.`maxscore`, 'PASSED', 'FAILED') as `score` FROM `c5k0o2h7gu_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.

You 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.

added 262 characters in body
Source Link

You 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_result("SELECT `f_name`, `l_name`, IF(`tdata`.`score` = `tdata`.`maxscore`, 'PASSED', 'FAILED') as `score` FROM `c5k0o2h7gu_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.

You 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_result("SELECT `f_name`, `l_name`, IF(`tdata`.`score` = `tdata`.`maxscore`, 'PASSED', 'FAILED') as `score` FROM `c5k0o2h7gu_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 . "'");

You 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_result("SELECT `f_name`, `l_name`, IF(`tdata`.`score` = `tdata`.`maxscore`, 'PASSED', 'FAILED') as `score` FROM `c5k0o2h7gu_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.

provide example of required query
Source Link
Loading
Source Link
Loading