gpt4 book ai didi

php - 我的 wordpress 插件如何知道学生是谁和用户帐户以及我将如何显示附加信息?

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:14 25 4
gpt4 key购买 nike

我是制作 wordpress 插件的新手,我一直在尝试制作一个管理学生评论的插件。在 wordpress 的 CMS 中,我制作了用于添加评论的 CRUD。现在我正在尝试让学生登录并确保他能在网站上看到他自己的评论。

我遇到了这个错误:

Fatal error: Call to a member function getDagopdracht() on null in D:\xammp\htdocs\ivs_website_kopie\wp-content\plugins\ivs-dagopdracht\includes\views\view_resultaat.php on line 34

我想这是因为网站不知道它是什么id。我知道我当前登录的人有 student_id 4 和 ID(wordpress) 4。但是当我尝试输入 4 时,它给了我同样的错误。

这是 view_resultaat.php 中的网站代码:

// Create instance Beoordelinggeven class
$beoordelinggeven_class = new BeoordelingGeven();

?>

<body>
<div class="container">
<div class="row">
<h3>Resultaten (BETA)</h3>
</div>
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Dagopdracht</th>
<th>Beoordeling</th>
<th>Commentaar</th>
</tr>
</thead>
<tbody>

<?php

$user_id = get_current_user_id();
if ($user_id == 0) {
echo 'You are currently not logged in.';
} else {

$leerling_id = $beoordelinggeven_class->getLeerlingIdByUserId($nummer, $user_id);


//view beoordelinggeven
$beoordelinggeven_objects = $beoordelinggeven_class->getBeoordelingLeerling($nummer, $user_id);
//if(leerling id){
foreach ($beoordelinggeven_objects as $id => $beoordelinggeven_object) {
echo '<tr>';
echo '<td>' . $beoordelinggeven_object->getDagopdracht() . '</td>';
echo '<td>' . $beoordelinggeven_object->getBeoordeling() . '</td>';
echo '<td>' . $beoordelinggeven_object->getComment() . '</td>';
?>

<?php
echo '</tr>';
}
}
//}
?>
</tbody>
</table>
<td><a href="<?php echo $base_url; ?>">Terug</a></td>
</div>
</div> <!-- /container -->
</body>
</html>

这是它在文件 BeoordelingGeven.php 中对应的函数:

public function getLeerlingIdByUserId($nummer, $user_id){

global $wpdb;
echo $user_id;
$id_query = "SELECT l.`nummer`, u.`ID`, d.`naam` d_naam, b.`naam` b_naam, g.`comment` FROM `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling` g
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_dagopdracht` d ON g.`beoordeling_dagopdracht` = d.`dagopdracht_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_leerlingen` l ON g.`beoordeling_leerling` = l.`nummer`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling_type` b ON g.`beoordeling_beoordeling` = b.`beoordeling_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_users` u ON l.`leerling_user_id` = u.`ID`
WHERE l.nummer = $nummer AND u.ID = $user_id";

$id = $wpdb->get_results($id_query);
print_r($id);
return $id;
}

public function getBeoordelingLeerling($nummer, $user_id) {

global $wpdb;

$query = "SELECT l.`nummer`, u.`ID`, d.`naam` d_naam, b.`naam` b_naam, g.`comment` FROM `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling` g
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_dagopdracht` d ON g.`beoordeling_dagopdracht` = d.`dagopdracht_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_leerlingen` l ON g.`beoordeling_leerling` = l.`nummer`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling_type` b ON g.`beoordeling_beoordeling` = b.`beoordeling_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_users` u ON l.`leerling_user_id` = u.`ID`
WHERE l.nummer = $nummer AND u.ID = $user_id";

$result_array = $wpdb->get_results( $query, ARRAY_A);
//"WHERE d.dagopdracht_id = $id";
// For all database results:
$result = $result_array[self::RESULT_ARRAY_OFFSET];

// For all database results:
$this->setDagopdracht($result['beoordeling_dagopdracht']);
$this->setBeoordeling($result ['beoordeling_beoordeling']);
$this->setComment($result['comment']);


return $this;

我错过了什么?

这是我在这里的第一个问题,如果我有任何错误,请原谅。

另外这是我的数据库:学生 table student

用户表 user

评论 reviews

最佳答案

要获取当前用户 ID,您可以使用此代码段:

<?php
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
?>

让我们简化这段代码

查看--

<?php
$user_id = get_current_user_id();
if ($user_id == 0) {
echo 'You are currently not logged in.';
} else {

$nummer = $beoordelinggeven_class->getLeerlingIdByUserId($user_id);
//view beoordelinggeven
$beoordelinggeven_objects = $beoordelinggeven_class->getBeoordelingLeerling($nummer, $user_id);
//if(leerling id){
foreach ($beoordelinggeven_objects as $id => $beoordelinggeven_object) {
echo '<tr>';
echo '<td>' . $beoordelinggeven_object->getDagopdracht() . '</td>';
echo '<td>' . $beoordelinggeven_object->getBeoordeling() . '</td>';
echo '<td>' . $beoordelinggeven_object->getComment() . '</td>';
?>

<?php
echo '</tr>';
}
}
//}
?>

BeoordelingGeven.php-----

public function getLeerlingIdByUserId($user_id){

global $wpdb;

$query = "SELECT `nummer` FROM `wp_ivs_beoordeling` WHERE leerling_user_id = $user_id;
$result = $wpdb->get_results($query);
print_r($result);
$id = $result[0]->nummer;
echo $id;
return $id;
}

public function getBeoordelingLeerling($nummer, $user_id) {

global $wpdb;

$query = "SELECT l.`nummer`, u.`ID`, d.`naam` d_naam, b.`naam` b_naam, g.`comment` FROM `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling` g
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_dagopdracht` d ON g.`beoordeling_dagopdracht` = d.`dagopdracht_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_leerlingen` l ON g.`beoordeling_leerling` = l.`nummer`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_ivs_beoordeling_type` b ON g.`beoordeling_beoordeling` = b.`beoordeling_id`
INNER JOIN `wp_ivs_dagopdracht_db`.`wp_users` u ON l.`leerling_user_id` = u.`ID`
WHERE l.nummer = $nummer AND u.ID = $user_id";

$result_array = $wpdb->get_results( $query, ARRAY_A);
//"WHERE d.dagopdracht_id = $id";
// For all database results:
$result = $result_array[self::RESULT_ARRAY_OFFSET];

// For all database results:
$this->setDagopdracht($result['beoordeling_dagopdracht']);
$this->setBeoordeling($result ['beoordeling_beoordeling']);
$this->setComment($result['comment']);


return $this;
}

关于php - 我的 wordpress 插件如何知道学生是谁和用户帐户以及我将如何显示附加信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39697168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com