将所有项目显示在div显示框

时间:2016-03-04 12:51:13

标签: php function variables mysqli scope

这里我试图显示div显示区域中的所有项目。但是我一直得到这些错误;

  

注意:未定义的变量:connect in   第76行的C:\ xampp \ htdocs \ includes \ landlord_profile.php

     

警告:mysqli_query()期望参数1为mysqli,null给定   在第76行的C:\ xampp \ htdocs \ includes \ landlord_profile.php

     

警告:mysqli_fetch_array()期望参数1为mysqli_result,   在第78行的C:\ xampp \ htdocs \ includes \ landlord_profile.php中给出的null

<?php
session_start(); 

include ("includes/connect.php");
?>

<!doctype html>
<html>
<head>
    <title>Landlord Profile</title>


  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="css/index.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>
<body>

<div class="whole_page_div" style="width:100%; height:935px;;">
        <div class="container-fluid" style="margin-top: 15px;>
                <div class="row">
                <div class="col-md-12">
                <div class="logo" style="margin-left: 750px;"><a href="Index.html"><img class="logo" src="images/logo.png"/></a></div>
                 <a href="logout.php"> <button type="submit" class="btn btn-primary" name="logout" value="Log out" style="background-color:#337AB7; margin-top: -130px; margin-left: 1600px; ">Log Out</button></a>

                </div>


<hr>
    <nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
    <div class="container topnav">


            <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>
                <a class="navbar-brand topnav" href="#">MyAstonSpace</a>
            </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 navbar-right">
                    <li>
                        <a href="#Home">Home</a>
                    </li>
                    <li>
                        <a href="#Undergraduate Information">Undergraduate Information</a>
                    </li>
                    <li>
                        <a href="#Post-Grad Information">Post-Grad Information</a>
                    </li>
                     <li>
                        <a href="#International Students">International Students</a>
                    </li>
                     <li>
                        <a href="#Contact Us ">Contact Us </a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
      </div>
        <!-- /.container -->
    </nav>


    <?php
    function getprop (){

        $getprop = mysqli_query($connect,"SELECT * FROM properties");

        while ($rowprop = mysqli_fetch_array($getprop)){

            $prop_id = $rowprop['prop_id'];
            $prop_name = $rowprop['prop_name'];
            $prop_address = $rowprop['prop_address'];
            $prop_img = $rowprop['prop_img'];
            $prop_radius = $rowprop['prop_radius'];
            $prop_bedrooms = $rowprop['prop_bedrooms'];
            $prop_type = $rowprop['prop_type'];
            $prop_price = $rowprop['prop_price'];

            echo "
                <div class='singleproperty'>

                <h3>$prop_name</h3>
            "   ;       

            }


        }


    ?>
    <div class="container-fluid">
  <h3>Your Profile <h5> Landlord </5></h3>
  <ul class="nav nav-pills nav-stacked" style="width:300px;">
     <li><a href="#">Personal Information</a></li>
    <li><a href="includes/landlord_profile.php">My Proporties</a></li>
    <li><a href="#">Upload Profile Image</a></li>
    <li><a href="incldues/deactivate_myaccount.php">Deactivate My Account</a></li>

  </ul>
</div>


<center>
<div class="panel panel-default" style="width: 1200px; height: 400px; margin-top: -200px; margin-left: 100px; border: 2px solid blue;  " >
    <div class="panel-body" style="text-align:center; margin-left:30px; margin-bottom:10px;"> 
        <div class="listproperties">
        <?php
        getprop();
        ?>
        </div>

     </div>
</div>
</center>


<?php
include ("includes/footer-inc.php");
?>

2 个答案:

答案 0 :(得分:0)

因为你的变量$connect在函数内使用。

但是,那里无法访问。

使用global之类的:

function getprop (){
  global $connect;
// Your code ...

答案 1 :(得分:0)

我认为$connect中定义了includes/connect.php。您尝试在函数中访问该变量,该函数根本无法访问此变量(也就是在另一个范围内)。

您应该将$connect作为参数添加到您的函数getprop()中,以使其可访问:

function getprop ($connect) {
  ...
}

每次使用该功能时,请使用

添加参数
getprop($connect);

而不是

getprop();

另一种可能性是使用全局变量,但这是非常糟糕的编码风格,所以不要这样做。

您应该阅读可变范围的手册页,其中说明了谁可以访问哪些变量:http://php.net/manual/en/language.variables.scope.php