您好我正在开发一个新闻网站。我创建了一个自定义模板,其中我有一个表单和字段插入数据库(mySql)。代码如下所示。Insert into database
现在我需要以table
或grid
的形式显示已经插入前端数据库的数据。我怎么能这样做。
我需要像这样的输出。
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.php(自定义模板)
<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 -->
答案 0 :(得分:0)
案例1:为表格中的显示信息创建模板
<?php
/*
Template Name: Disply Ritural
Description: display ritual information in table formate
*/
global $wpdb;
$table_name = "rituals";
$result = $wpdb->get_results ("SELECT * FROM $table_name");
foreach ( $result as $ritual_info )
{
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
}
案例2:创建一个短代码,方便使用。打开你的functions.php文件,在那里添加代码。
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文件
<?php echo do_shortcode('[show_ritual_info]'); ?>
内部管理页面
['show_ritual_info']
答案 1 :(得分:0)
这样的事情:
<?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; ?>
不检查但必须工作。)