gpt4 book ai didi

php - 不显示已经订阅的节目

转载 作者:行者123 更新时间:2023-12-04 15:30:10 25 4
gpt4 key购买 nike

所以我有这段代码:

<?php

global $wpdb;

$table_name = $wpdb->prefix . 'usermeta';
$current_user_id = get_current_user_id();
$Sdoubledata = array($retrieved_data);
foreach($Sdoubledata as $Sdoubledata) {
// Het stuk code hieronder haalt de programma's op uit de database waar de gebruiker al op geabonneerd is.
$retrieve_data = $wpdb->get_results("SELECT * FROM {$table_name} WHERE user_id={$current_user_id} AND meta_key='programma';");
}
echo "<br>";
?>
<!-- Dit stuk code laat de programma's zien waar de radiostation al op geabonneerd is. -->
<br>
<p>De programma's waarop u geabonneerd bent:</p>
<form action="#" enctype="multipart/form-data" method="post">
<?php wp_nonce_field( 'set_programmatest_action', 'set_programmatest' ); ?>
<table>
<?php foreach ( $retrieve_data as $retrieved_data ) { ?>
<tr>
<th>Programma:</th>
<td style="vertical-align: middle;"><?php echo esc_html( $retrieved_data->meta_value ); ?></td>
<th>
<button name="programmatest" type="submit" id="button" value="<?php echo esc_attr( $retrieved_data->meta_value ); ?>">Abonnement opzeggen</button>
</th>
</tr>
<?php }
?>
</table>
</form>

<?php
// Verifieer nonce en sla de data op als de user is ingelogd.
// Nonce docs: https://developer.wordpress.org/themes/theme-security/using-nonces/
if (isset( $_POST['programmatest'] ) && isset( $_POST['set_programmatest'] ) && wp_verify_nonce( $_POST['set_programmatest'], 'set_programmatest_action' )) {

$Data = filter_input( INPUT_POST, 'programmatest', FILTER_SANITIZE_STRING );
$current_user_id = get_current_user_id();

if ( $current_user_id && ! empty( $Data ) ) {
delete_user_meta( $current_user_id, 'programma', $Data );
echo "Uw abonnement op" . ' ' . $Data. ' ' ."is opgezegd.";
//Refresh de pagina
?>

<?php
$page = $_SERVER['PHP_SELF'];
$sec = "2";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<?php
echo ' ' . "U wordt doorverwezen naar de Homepagina";
?>
</body>
</html>
<?php
}
}
?>


<?php
global $wpdb;

$table_name = $wpdb->prefix . 'programma';
// Dit haalt de data op uit de database

$NSdoubledata = array("uitwerkingen", "GoudvanOud");
foreach($NSdoubledata as $NSdoubledata) {
$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );
if(in_array($NSdoubledata)) {
echo "Hij doet het!";
} else {
echo "Nee, toch niet.";
}
}

echo "<br>";

?>
<!--De code hier laat de data die opgehaald is zien en laat de programma's zien waar de radiostation op kan abonneren-->
<br>
<p>Programma's waarop u kunt abonneren:</p>
<form action="#" enctype="multipart/form-data" method="post">
<?php wp_nonce_field( 'set_programma_action', 'set_programma' ); ?>
<table>
<?php foreach ( $retrieve_data as $retrieved_data ) { ?>
<tr>
<th>Programma:</th>
<td style="vertical-align: middle;"><?php echo esc_html( $retrieved_data->Anaam ); ?></td>
<th>
<button name="programma" type="submit" value="<?php echo esc_attr( $retrieved_data->Anaam ); ?>">Abonneer</button>
</th>
</tr>
<?php } ?>
</table>
</form>

<?php
// Verifieer nonce en sla de data op als de user is ingelogd.
// Nonce docs: https://developer.wordpress.org/themes/theme-security/using-nonces/
if (isset( $_POST['programma'] ) && isset( $_POST['set_programma'] ) && wp_verify_nonce( $_POST['set_programma'], 'set_programma_action' )) {

$data = filter_input( INPUT_POST, 'programma', FILTER_SANITIZE_STRING );
$current_user_id = get_current_user_id();

if ( $current_user_id && ! empty( $data ) ) {
//voeg de huidige user_id en data toe in de rij met meta_key programma
add_user_meta( $current_user_id, 'programma', $data );
echo "U bent geabonneerd op". ' ' . $data;
}
}
?>

我尝试过的:

<?php
global $wpdb;

$skip = array($retrieved_data);
if(in_array($retrieved_data, $skip)) {

$table_name = $wpdb->prefix . 'programma';
// This retrieves the data from the database
$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );
echo "<br>";

}

?>

该代码允许用户订阅节目和取消他/她的订阅。但是,当用户订阅了一个节目后,他/她已经订阅的节目仍然显示在允许用户订阅节目的地方。

例子:

enter image description here

如您所见,在第一部分中,用户可以看到他或她已经订阅了哪些节目。在第二部分,用户可以选择他或她可以订阅的节目。由于用户已经订阅了“uitwerkingen”,他不应该将其视为订阅选项。我已经在 Stack Overflow 上寻找解决方案,但找不到任何解决方案。

我该如何解决这个问题?

我的数据库结构:

用户元表:

enter image description here

程序表:

enter image description here

enter image description here

编辑

这是我第一次循环显示所有已订阅节目的数据。

<?php

global $wpdb;

$table_name = $wpdb->prefix . 'usermeta';
$current_user_id = get_current_user_id();
//I made an array containing all the subscribed to programs
$Sdoubledata = array("uitwerkingen", "GoudvanOud");
foreach($Sdoubledata as $Sdoubledata) {
$retrieve_data = $wpdb->get_results("SELECT * FROM {$table_name} WHERE user_id={$current_user_id} AND meta_key='programma';");
if(in_array("uitwerkingen", "GoudvanOud")) {
continue;
}
}
echo "<br>";
?>

这是第二个循环:

<?php
global $wpdb;

$table_name = $wpdb->prefix . 'programma';
// I made an array containing all the programs a user can subscribe to.
$NSdoubledata = array("uitwerkingen", "GoudvanOud");
// I made a foreach which contains the result of the query
foreach($NSdoubledata as $NSdoubledata) {
$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );
// I want to output the programs as you told me.
echo ($NSdoubledata);
}

echo "<br>";

?>

最佳答案

我相信一个简单的 SQL 更改就能为您解决这个问题。

SELECT Anaam FROM programma
WHERE ID_prog NOT IN
(SELECT meta_value FROM usermeta WHERE user_id={$current_user_id})

因此,如果您在当前读取的行中更改它

$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM {$table_name}" );

这应该看起来更像

$retrieve_data = $wpdb->get_results( "SELECT Anaam FROM programma WHERE ID_prog NOT IN (SELECT meta_value FROM usermeta WHERE user_id={$current_user_id})" );

这应该只会生成用户尚未订阅的节目列表。

祝你好运!

关于php - 不显示已经订阅的节目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61384766/

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