第一次使用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\":\"[email protected]\",\"identifier\":\"field6\",\"type\":\"email\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"100%\",\"altLabel\":\"Email\"}
]
我试图拉随机行,然后发布name
,company name
。
我被困了,wordpress
不是logging任何错误,虽然我已经打开错误logging。我是通过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) "[email protected]" ["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:
分析解答
首先,您需要制作"stripslashing"并将给定的string转换为JSON数据。然后你需要解码它,而不是编码。
这里是:
$winner = json_decode(stripslashes($result[0]->content),true);
echo $winner[0]["value"];
还有一件事。 $wpdb-& gt;查询不能在get_results内使用。删除它们并使用它
$sql = "
SELECT *
FROM wp_formcraft_3_submissions
ORDER BY RAND()
LIMIT 1
";
$result = $wpdb->get_results( $sql );