Php 在wordpress自定义模板中显示插入的数据
嗨,我正在开发一个文字新闻网站。我创建了一个自定义模板,其中我有一个表单,字段被插入数据库MySQL。代码如下所示。请插入数据库 现在我需要在前端以表格或网格的形式显示已经插入到数据库中的数据。我是如何做到这一点的 我需要这样的输出Php 在wordpress自定义模板中显示插入的数据,php,jquery,mysql,ajax,wordpress,Php,Jquery,Mysql,Ajax,Wordpress,嗨,我正在开发一个文字新闻网站。我创建了一个自定义模板,其中我有一个表单,字段被插入数据库MySQL。代码如下所示。请插入数据库 现在我需要在前端以表格或网格的形式显示已经插入到数据库中的数据。我是如何做到这一点的 我需要这样的输出 Ritual Name Ritual Active --------------------------- Test1 | Y Test2 | Y 谢谢你的帮助 funct
Ritual Name Ritual Active
---------------------------
Test1 | Y
Test2 | Y
谢谢你的帮助
function.php
function childtheme_style_andscripts(){
//wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_script('ajax-function', get_stylesheet_directory_uri() . '/js/ajaxfunction.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajax-function', 'usersubmitform', array(
'url'=> admin_url('admin-ajax.php'),
'security'=> wp_create_nonce('our-nonce')
) );
}
add_action('wp_enqueue_scripts','childtheme_style_andscripts');
function form_action_function() {
require_once(dirname( __FILE__ ).'/../../../wp-load.php');
$data = $_POST['data'];
global $wpdb;
if( !check_ajax_referer('our-nonce', 'security' ) ) {
wp_send_json_error('security failed');
return;
}
//var_dump($data);
$rname=$data['rname'];
$ractive=$data['ractive'];
$table_name = "rituals";
$wpdb->insert($table_name, array ('Ritual_Name' => $rname, 'Ritual_Active' => $ractive) );
//$wpdb->show_errors();
//$wpdb->print_error();
echo 'From Submitted Successfully';
die();
}
add_action('wp_ajax_nopriv_form_action_function','form_action_function');
add_action('wp_ajax_form_action_function','form_action_function');
ajaxfunction.js
jQuery(document).ready(function($){
var submitButton = document.getElementById('usersubmit');
var ajaxFunctionformprocess = function(fromdata, action){
$.ajax({
type:'post',
url: usersubmitform.url,
data:{
action:action,
data:fromdata,
security:usersubmitform.security,
},
success:function(reponse){
$('div.msg').html(reponse);
},
error:function(response){
alert(response);
}
});
}
submitButton.addEventListener('click', function(event){
event.preventDefault();
var fromdata = {
'rname':document.getElementById('rname').value,
'ractive':document.getElementById('ractive').value,
};
ajaxFunctionformprocess(fromdata, 'form_action_function');
});
});
Samplepage.phpcustom模板
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<h1 class="headingform">User Form</h1>
<div class="msg"></div>
<form class="userform">
Ritual Name: <input type="text" id="rname" name="rname" /><br> <br>
Ritual Active: <input type="text" id="ractive" name="ractive" /> <br><br>
<input id="usersubmit"type="submit" Value="Submit" />
</form>
</div><!-- #content -->
</div><!-- #primary -->
案例1:为表中的显示信息创建模板
如何使用短代码
php文件内部
内部管理页面
[“展示仪式信息”]
大概是这样的:
<?php
global $wpdb;
$table_name = "rituals";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<?php if( ! empty($retrieve_data) ):?>
<table>
<tr>
<th>Ritual Name</th>
<th>Ritual Active</th>
</tr>
<?php foreach ($retrieve_data as $retrieved_data): ?>
<tr>
<td><?php echo $retrieved_data['Ritual_Name'];?></td>
<td><?php echo $retrieved_data['Ritual_Active'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
不检查,但必须工作。我收到这些警告警告:strpos期望参数1为字符串,在第205行C:\xampp\htdocs\wordpress-4.7\wordpress\wp includes\shortcodes.php中给出的数组警告:preg\u match\u all期望参数2为字符串,第213行C:\xampp\htdocs\wordpress-4.7\wordpress\wp includes\shortcodes.php中给出的数组警告:array\u intersect:Argument 2不是第214行C:\xampp\htdocs\wordpress-4.7\wordpress\wp includes\shortcodes.php中的数组。我正在函数中使用case2。php我已经将此代码函数放置在show\u-info中{require_oncedirname uu FILE_uu./../../../../../../wp load.php';全局$wpdb;$table_name=rituals;$result=$wpdb->get_results SELECT*FROM$table_name;$wpdb->show_errors;$wpdb->print_error;foreach$result作为$riture_info{echo$riture_info['riture_name'];echo$riture_info['Active'}在自定义模板中,我添加了这个函数show_riture_info{global$wpdb;$table_name=rituals;$result=$wpdb->get_results SELECT*FROM$table_name;$wpdb->show_errors;$wpdb->print_error;//foreach$result作为$riture_info/{echo$riture_info['riture_name'];echo$riture_info['riture_Active'];//}添加“show_riture_info”和“show_riture_info”快捷码;检查第二个回答悬赏是否未被奖励。请告诉我您在编辑器中实际需要什么?@VasimVanzara在编辑器中我需要一个包含许多详细信息的弹出表单。当我单击visual editor中的按钮时,弹出窗口应该会出现。
function show_ritual_info(){
global $wpdb;
$table_name = "rituals";
$query="SELECT * FROM $table_name";
$result = $wpdb->get_results ($query);
foreach ( $result as $ritual_info )
{
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
}
}
add_shortcode('show_ritual_info','show_ritual_info');
<?php
global $wpdb;
$table_name = "rituals";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<?php if( ! empty($retrieve_data) ):?>
<table>
<tr>
<th>Ritual Name</th>
<th>Ritual Active</th>
</tr>
<?php foreach ($retrieve_data as $retrieved_data): ?>
<tr>
<td><?php echo $retrieved_data['Ritual_Name'];?></td>
<td><?php echo $retrieved_data['Ritual_Active'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>