Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用php做一个while循环的短代码?_Php_Sql_Wordpress_Function_Shortcode - Fatal编程技术网

如何用php做一个while循环的短代码?

如何用php做一个while循环的短代码?,php,sql,wordpress,function,shortcode,Php,Sql,Wordpress,Function,Shortcode,我是PHP的初学者,我一直在尝试创建一个函数,通过添加一个短代码来显示我的while循环。我知道add_shortcode的语法要求在函数中返回,但我似乎无法让它在while循环中显示字符串。当我尝试返回字符串时,它只显示最后一个值,而不是循环。任何意见都将不胜感激 function events_homepage() { global $connection; mysqli_select_db($connection); $query = ("SELECT * FROM

我是PHP的初学者,我一直在尝试创建一个函数,通过添加一个短代码来显示我的while循环。我知道add_shortcode的语法要求在函数中返回,但我似乎无法让它在while循环中显示字符串。当我尝试返回字符串时,它只显示最后一个值,而不是循环。任何意见都将不胜感激

function events_homepage() {
    global $connection;
    mysqli_select_db($connection);
    $query = ("SELECT * FROM events WHERE start_date >= CURDATE() ORDER BY start_date LIMIT 3");
    $result = $connection->query($query);
    while ($row = $result->fetch_assoc()) {
        $title = $row['title'];
        $start_date = date('M d, Y', strtotime($row['start_date']));
        $location = $row['location'];
        $link = $row['link'];
        $str = "<p class='events_homepage_date'>$start_date</p> <p class='events_homepage_title'><a href='$link' target='_blank'>$title</a></p> <p class='events_homepage_location'>$location</p>";
        echo $str;
    }
    return;
}
add_shortcode( 'events_homepage_shortcode', 'events_homepage' );
功能事件\u主页(){
全球美元连接;
mysqli_select_db($connection);
$query=(“从开始日期>=CURDATE()按开始日期限制3排序的事件中选择*);
$result=$connection->query($query);
而($row=$result->fetch_assoc()){
$title=$row['title'];
$start_date=date('md,Y',strottime($row['start_date']);
$location=$row['location'];
$link=$row['link'];
$str=“

$start\u date

$location

”; echo$str; } 返回; } 添加_短代码(‘事件_主页_短代码’、‘事件_主页’);
您说得对,您需要在shortcode函数中返回。您缺少的部分是,您需要在shortcode函数中返回整个输出,而不能回显它

因此,您需要连接$str,并在函数末尾输出构建的字符串。下面代码的更新版本将生成所需的输出

function events_homepage() {
    global $connection;
    mysqli_select_db($connection);
    $query = ("SELECT * FROM events WHERE start_date >= CURDATE() ORDER BY start_date LIMIT 3");
    $result = $connection->query($query);
    $str = "";
    while ($row = $result->fetch_assoc()) {
        $title = $row['title'];
        $start_date = date('M d, Y', strtotime($row['start_date']));
        $location = $row['location'];
        $link = $row['link'];
        $str .= "<p class='events_homepage_date'>$start_date</p> <p class='events_homepage_title'><a href='$link' target='_blank'>$title</a></p> <p class='events_homepage_location'>$location</p>";
    }
    return $str;
}
add_shortcode( 'events_homepage_shortcode', 'events_homepage' );
功能事件\u主页(){
全球美元连接;
mysqli_select_db($connection);
$query=(“从开始日期>=CURDATE()按开始日期限制3排序的事件中选择*);
$result=$connection->query($query);
$str=”“;
而($row=$result->fetch_assoc()){
$title=$row['title'];
$start_date=date('md,Y',strottime($row['start_date']);
$location=$row['location'];
$link=$row['link'];
$str.=“

$start\u date

$location

”; } 返回$str; } 添加_短代码(‘事件_主页_短代码’、‘事件_主页’);
您说得对,您需要在shortcode函数中返回。您缺少的部分是,您需要在shortcode函数中返回整个输出,而不能回显它

因此,您需要连接$str,并在函数末尾输出构建的字符串。下面代码的更新版本将生成所需的输出

function events_homepage() {
    global $connection;
    mysqli_select_db($connection);
    $query = ("SELECT * FROM events WHERE start_date >= CURDATE() ORDER BY start_date LIMIT 3");
    $result = $connection->query($query);
    $str = "";
    while ($row = $result->fetch_assoc()) {
        $title = $row['title'];
        $start_date = date('M d, Y', strtotime($row['start_date']));
        $location = $row['location'];
        $link = $row['link'];
        $str .= "<p class='events_homepage_date'>$start_date</p> <p class='events_homepage_title'><a href='$link' target='_blank'>$title</a></p> <p class='events_homepage_location'>$location</p>";
    }
    return $str;
}
add_shortcode( 'events_homepage_shortcode', 'events_homepage' );
功能事件\u主页(){
全球美元连接;
mysqli_select_db($connection);
$query=(“从开始日期>=CURDATE()按开始日期限制3排序的事件中选择*);
$result=$connection->query($query);
$str=”“;
而($row=$result->fetch_assoc()){
$title=$row['title'];
$start_date=date('md,Y',strottime($row['start_date']);
$location=$row['location'];
$link=$row['link'];
$str.=“

$start\u date

$location

”; } 返回$str; } 添加_短代码(‘事件_主页_短代码’、‘事件_主页’);
您想在add_短代码中执行这两个功能??只是一个站点注释。。。我不会在这里编写SQL查询。改为使用WP Query。您希望在add_短代码中执行这两个函数??只是一个站点注释。。。我不会在这里编写SQL查询。请改用WP查询。谢谢您的解释!!这起作用了,现在我知道我做错了什么:)谢谢你的解释!!这起作用了,现在我知道我做错了什么:)