第一次使用Wordpress。我希望从一个formcraft表中得到一个随机行,这样我就可以在一个页面上发布一堆注册抽签的获胜者。
我希望从formcraft表中获得的内容存储在名为content的列中,存储在名为wp_formcraft_3_submissions的表中
[
{\"label\":\"Name\",\"value\":\"Annette\",\"identifier\":\"field1\",\"type\":\"oneLineText\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Name\"},
{\"label\":\"Company Name\",\"value\":\"My Co \",\"identifier\":\"field3\",\"type\":\"oneLineText\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Company Name\"},
{\"label\":\"Email\",\"value\":\"annette@email.com\",\"identifier\":\"field6\",\"type\":\"email\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Email\"}
]我试图拉出一个随机行,然后发布name,company name。
我被卡住了,wordpress没有记录任何错误,尽管我打开了错误日志记录。我是通过shortcode来做这件事的。首先,我想回应屏幕上的内容,但最终,回显的只是这三个字段。
function wa_awards_winner() {
global $wpdb;
$sql = $wpdb->query("
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
");
$result = $wpdb->get_results( $sql );
if ( ! $result ) { return false; }
$winner = json_encode($result[0]->content);
echo $winner;
}
add_shortcode( 'get_wa_awards_winner', 'wa_awards_winner' );不幸的是,我试图像这样得到这些值,但这是行不通的:
// NAME
echo '<h3 class="col">' . $winner[0][0] . '</h3>';
// COMPANY NAME
echo '<h3 class="col">' . $winner[1][0] . '</h3>';
// EMAIL
echo '<h3 class="col">' . $winner[2][0] . '</h3>';感谢你的帮助。谢谢。
以下是这样的尝试性建议:
$winner = json_decode(stripslashes($result[0]->content),true);
echo '<div style="color:white;">' . print_r($winner, true) . '</div>';我明白了;
[13-Jun-2019 12:28:25 UTC] PHP Notice: Undefined offset: 0 in /var/www/site.com/wp-content/themes/dp-click-Child/functions.php on line 30
[13-Jun-2019 12:28:25 UTC] PHP Notice: Trying to get property 'content' of non-object in /var/www/site.com/wp-content/themes/dp-click-Child/functions.php on line 30这要归功于以下建议:
$sql = "
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
";
$result = $wpdb->get_results( $sql );然后我试着访问这个信息,比如:
$winner = json_decode(stripslashes($result[0]->content),true);
echo var_dump($winner);
echo '<h3 style="color: white;">NAME: ' . $winner[0]["Name"] . '</h3>';
echo '<h3 style="color: white;">COMPANY: ' . $winner[1]["Company Name"] . '</h3>';
echo '<h3 style="color: white;">EMAIL: ' . $winner[2]["Email"] . '</h3>';var_dump返回:
array(3) { [0]=> array(8) { ["label"]=> string(4) "Name" ["value"]=> string(14) "Annette" ["identifier"]=> string(6) "field1" ["type"]=> string(11) "oneLineText" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(4) "Name" } [1]=> array(8) { ["label"]=> string(12) "Company Name" ["value"]=> string(19) "My Company" ["identifier"]=> string(6) "field3" ["type"]=> string(11) "oneLineText" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(12) "Company Name" } [2]=> array(8) { ["label"]=> string(5) "Email" ["value"]=> string(20) "annette@email.com" ["identifier"]=> string(6) "field6" ["type"]=> string(5) "email" ["page"]=> int(1) ["page_name"]=> string(6) "Step 1" ["width"]=> string(4) "100%" ["altLabel"]=> string(5) "Email" } }但是我的变量返回为空:
NAME:
COMPANY NAME:
EMAIL:发布于 2019-06-13 11:18:14
首先,您需要进行“反斜杠”,并将给定的字符串转换为JSON数据。那么你需要解码它,而不是编码。
下面是:
$winner = json_decode(stripslashes($result[0]->content),true);
echo $winner[0]["value"];还有一件事。$wpdb->查询不能在get_results内部使用。移除这些,然后用它代替
$sql = "
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
";
$result = $wpdb->get_results( $sql );https://stackoverflow.com/questions/56578921
复制相似问题