前言:
需求介绍:
将wordpress数据库的积分表其中的几个字段,获取出来制作成表格。
插件调查: 免费的版本都是一些静态的表格,其他收费的插件没有查看,打算通过短代码的形式开发
wordpress 短代码不会的看上篇文章有介绍,下面直接附上function .php 开发代码
、//PHP查询MySql数据库,将结果用表格输出
function get_user_point_msg()
{
//php连接数据库
$conn = mysqli_connect("localhost", "root", "root");
if (!$conn) {
return "连接失败";
}
//把连接的返回值穿进去和要查询的数据库
mysqli_select_db($conn, "litest");
//定义sq字符串
$sql = "select id Pionts, user_login Date, user_nicename Entry from wp_users";
//将sql传进去返回结果
$res = mysqli_query($conn, $sql);
$rows = mysqli_affected_rows($conn); //获取行数
$colums = mysqli_num_fields($res); //获取列数
// echo "litest数据库的" . "wp_users" . "表的所有用户数据如下:<br/>";
// echo "共计" . $rows . "行 " . $colums . "列<br/>";
//从这里开始生成表格
$tableStart = "<table style='width:100%;border: 0.5px solid black;color:black;'><tr>";
$thTitle = "";
for ($i = 0; $i < $colums; $i++) {
// $field_name=mysqli_field_name($res,$i);
$field_name = mysqli_fetch_field_direct($res, $i)->name;
$thTitle .= "<th style='width:33%;text-align:center;border: 0.5px solid black;background-color:#1c57de;'>$field_name</th>";
}
$trOneEnd = "</tr>";
$tableTrTwo = "";
while ($row = mysqli_fetch_row($res)) {
$tableTrTwo .= "<tr>";
for ($i = 0; $i < $colums; $i++) {
$tableTrTwo .= "<td style='text-align:center;border: 0.5px solid black;'>$row[$i]</td>";
}
$tableTrTwo .= "</tr>";
}
$tableEnd = "</table>";
//关闭数据库连接
mysqli_close($conn);
//最后将字符串拼接,返回
return $tableStart . $thTitle . $trOneEnd . $tableTrTwo . $tableEnd;
}
//将此函数挂在到wordpress的短代码上
add_shortcode('user_point_msg', 'get_user_point_msg');
使用elementor或者是wordpress的古腾堡编译器,插入短代码
最后表格的结果
如果你想直接将此页面显示出来,不使用段代码,你可以在任意页面直接写这个函数,并且调用。
将代码当中拼接的字符串,全部换成echo,显示出来。
下篇文章会改进,使用wordpress连接数据库而不是,原生的php连接。