指定PHP查询的输出位置

时间:2014-04-20 18:09:53

标签: php mysql

我正在开发一个小型PHP项目作为该语言的介绍,并且在格式化数据库查询结果时遇到了问题。我的PHP函数运行正常,但结果显示在所有HMTL上方,如下所示:here

这是我的代码,全部在名为index.php的文件中。有人能指出我哪里错了吗?我希望它显示在导航栏的下方(主页,好友列表等),如何准确指定我希望他在PHP中出现哪些PHP?

#!/usr/local/bin/php
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>BumpIt</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="css/custom.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>



    <!-- Login PHP -->
    <?php
    ini_set("display_errors", true);

    session_start();

    // Store username and password in session variables
    if (isset($_POST['signin_user']))
    {
      $_SESSION['signin_user']=$_POST['signin_user'];
      $_SESSION['signin_pass']=$_POST['signin_pass'];

    }


    // Reset username and pass to that of the session variables
    $signin_user = $_SESSION['signin_user'];
    $signin_pass = $_SESSION['signin_pass'];
    $isSigningIn = $_POST['isSigningIn'];




    // Connect to the database, check if error happens
    $conn = pg_connect(Redacted*);
    $_SESSION['conn'] = $conn;
    if (!$conn) {
      echo "Connection failed";
    exit;
  }


/**************************************************************
**************************************************************
*****************    FUNCTIONS     ***************************
**************************************************************
***************************************************************/


    // Load Friend List function
    if($_GET['friendList']){friendList();}

    function friendList(){
      $userID = $_SESSION['userID'];
      $query = sprintf("SELECT username FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id = user_id;");
      $result = pg_query($_SESSION['conn'], $query);

      echo "<table class='table table-striped table-bordered table-hover'>\n";
      echo "<caption>Friend List</caption>\n";
      while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
          echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
      }
      echo "</table>\n";
    }


    // LOGIC NOT WORKING RIGHT YET (DISPLAYING MULTIPLES OF SAME NAME)
    // Find Friends function
    if($_POST['search']){searchForFriends();}

    function searchForFriends() {
      $search = $_POST['search'];
      $userID = $_SESSION['userID'];
      $query = sprintf("SELECT userName FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id <> users.user_id AND userName ILIKE ( '%%' || "."'".$search."'"." || '%%');");
      echo "DEBUG PURPOSES-> SEARCHFORFRIENDS QUERY IS: ".$query;
      $result = pg_query($_SESSION['conn'], $query);

      echo "<table class='table table-striped table-bordered table-hover'>\n";
      echo "<caption>Users</caption>\n";
      while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
          echo "\t\t<td>$col_value</td>\n";
        }
         echo "\t</tr>\n";
        }
        echo "</table>\n";

    }

?>










    <img class="header-logo" src="img/bumpit.png"></img>

    <!-- Navigation Bar -->
        <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a id="friendList" href="index.php?friendList=true">Friend List</a></li>
            <li>
              <a id="logoutBtn" href="index.php?logoutBtn=true">Logout</a>
            </li>

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>

              <ul class="dropdown-menu">
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li class="divider"></li>
                <li><a href="#">Separated link</a></li>
                <li class="divider"></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
          <!-- Search bar -->
          <form class="navbar-form navbar-right" role="search" action="index.php?search=true" method="post">
            <div class="form-group">
              <input name="search" type="text" class="form-control" placeholder="Search">
              <input name="isSigningIn" type="hidden" value="1">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>


        </div><!-- /.navbar-collapse -->





      </div><!-- /.container-fluid -->
      </nav>

      <!-- Main content -->

      <div class="container">
        <div class="starter-template">

<!-- WE WANT THE FRIENDS LIST HERE -->
         </div>
       </div><!-- /.container -->


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>

1 个答案:

答案 0 :(得分:0)

我相信你想移动它:

// Load Friend List function
if($_GET['friendList']){friendList();}

到你想要打印friendList的地方:

 <div class="container">
    <div class="starter-template">

     <!-- WE WANT THE FRIENDS LIST HERE -->
     <?php
         // Load Friend List function
         if (isset($_GET['friendList'])) { friendList(); }
     ?>
     </div>
   </div><!-- /.container -->

您必须在<?php ?>中添加它,并且我还更改了if语句,因此如果不设置friendList,它就不会发出通知。