在php循环之外设置文本

时间:2016-11-19 05:39:10

标签: php html loops

我被困在一个可能非常简单的事情上......只是无法解决这个问题。我有这个:echo "Pending Friends Are - " . $pending_friend_1 . "<br>";在我的循环中。在输出时我需要$pending_friend_1在循环内,否则我得不到我的完整循环结果。如何取出文本Pending Friends Are -,以便仍然与列表拼凑在一起。我想做这样的事情:

待定朋友 -

鲍勃

乔治

    <div id="main">
<?php
//Display pending friends
    $friends_pending_sql = "
        SELECT *
        FROM friends
        WHERE friend_two = ?
        AND status = ?
    ";
    $pending_friend_count_stmt = $con->prepare($friends_pending_sql);
    $pending_friend_count_stmt->execute(array($user_id, $status_one));
    $pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
    echo '<div id="pending-request_count">Total Pending Friends -' . $total_pending_count . '</div>';
    foreach ($pending_friend_rows as $pending_friend_row) {
        $pending_friend_1           = $pending_friend_row['friend_one'];
        $pending_friend_2           = $pending_friend_row['friend_two'];
        $pending_friend_status      = $pending_friend_row['status'];
        $pending_friend_status_date = $pending_friend_row['date'];
        $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

        if ($pending_friend_2 == $user_id) {

            echo "Pending Friends Are - " . $pending_friend_1 . "<br>";
        }
        else if ($pending_friend_1 = $user_id) {
            echo "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
        }
    }
    echo $friend_status_button;
    echo $profile_viewer_message;
?>

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

$friends_pending_arr=array();
$friends_pending_sql = "
    SELECT *
    FROM friends
    WHERE friend_two = ?
    AND status = ?
";
$pending_friend_count_stmt = $con->prepare($friends_pending_sql);
$pending_friend_count_stmt->execute(array($user_id, $status_one));
$pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="pending-request_count">Total Pending Friends -' . $total_pending_count . '</div>';
foreach ($pending_friend_rows as $pending_friend_row) {
    $pending_friend_1           = $pending_friend_row['friend_one'];
    $pending_friend_2           = $pending_friend_row['friend_two'];
    $pending_friend_status      = $pending_friend_row['status'];
    $pending_friend_status_date = $pending_friend_row['date'];
    $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

    if ($pending_friend_2 == $user_id) {
        $friends_pending_arr[]=$pending_friend_1;
    }
    else if ($pending_friend_1 == $user_id) {
        echo "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
    }
}
echo "Pending Friends Are - ";
foreach($friends_pending_arr as $friend){echo $friend . "<br>";}

答案 1 :(得分:1)

插入数组

case android.R.id.home:
            //call onBackPressed here
            onBackPressed();
            return true;

然后在循环外显示

$friends_pending_arr[]=$pending_friend_1;  

完整代码

   echo "Pending Friends Are - <br>";
   echo implode("<br>",$friends_pending_arr);

答案 2 :(得分:1)

else循环中,您的代码的foreach分支上似乎有故意代码或潜在错误。 else if ($pending_friend_1 = $user_id) {更多的是赋值而不是检查相等性。也许下面的代码 - 基本上是你的代码:稍微调整一下 - 可能有所帮助。

<?php
    // OBSERVE THAT THE GLOBAL PART OF YOUR PHP WAS MOVED OUT
    // FROM THE DIV... YOU DON'T NEED IT WITHIN A HTML DIV

    // CREATE A STRING TO HOLD PENDING FRIENDS.
    $strPendingFriends          = "";
    $friends_pending_sql        = "
        SELECT *
        FROM friends
        WHERE friend_two = ?
        AND status = ?
    ";
    $pending_friend_count_stmt  = $con->prepare($friends_pending_sql);
    $pending_friend_count_stmt->execute(array($user_id, $status_one));
    $pending_friend_rows        = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div id="main">
    <?php if($total_pending_count){ ?>
    <!-- Display pending friends -->
    <div id="pending-request_count">Total Pending Friends - <?php echo $total_pending_count; ?></div>
    <?php } ?>
    <?php
        foreach ($pending_friend_rows as $pending_friend_row) {
            $pending_friend_1           = $pending_friend_row['friend_one'];
            $pending_friend_2           = $pending_friend_row['friend_two'];
            $pending_friend_status      = $pending_friend_row['status'];
            $pending_friend_status_date = $pending_friend_row['date'];
            $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

            if ($pending_friend_2 == $user_id) {
                $strPendingFriends     .= "Pending Friends Are - " . $pending_friend_1 . "<br>";
            }else if ($pending_friend_1  == $user_id) {  //<== USED '==' THAN '='               
                $strPendingFriends     .= "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
            }
            echo $strPendingFriends;

        }
        echo $friend_status_button;
        echo $profile_viewer_message;
    ?>
</div>