为什么我的变量不能用作PHP函数中的参数

时间:2015-07-08 20:26:29

标签: php mysql function loops parameters

在我的文档的最顶部,我创建了一个名为$ selectedID的变量,使用$ _GET来获取所选用户的ID(以后将其切换为加密字符串),所以url看起来有点像这个profile.php? ID = 4。然后,我有一个函数,它循环通过一个数据库,所有博客帖子的编号与$ selectedID变量相同,后者作为函数的第二个参数输入。

问题是我的函数$ selectedId中的第二个参数应该被用作在循环访问我的数据库时过滤的数字,但似乎变量未正确插入到我的函数中循环不能正常工作

这是显示信息的主页

<div id="blogFeed">
                            <ul>
                            <!-- Exicute the fucntion from the functions.inc.php which grabs all the feed articles from the database -->
                            <?php

                                $_result = display_blogs_profile($connect, $selectedId);
                                // Count how many rows are in the database
                                $limit = count($_result);

                                // Loop through all the articles(rows) from the database
                                for($i = 0; $i < $limit; $i++){
                                    // Define varibles for each column
                                    $id         = $_result[$i][6];
                                    $title      = $_result[$i][0];
                                    $date       = $_result[$i][1];
                                    $article    = $_result[$i][2];
                                    $photo      = $_result[$i][3];
                                    $icon       = $_result[$i][4];
                                    $author     = $_result[$i][5];
                                    $findAuthor = $_result[$i][7];
                                    $feedRadius = "50px";
                                    $iconPhoto   = "newsRed.png";
                                    $photo       = $_result[$i][3];
                                    $feedRadius  = "0px";
                                    $contentLink = "Read More";
                                    $realignIcon = 'margin-right: -195px;';


                                // Only show the first 10 articles the stop building the html
                                if((string)$employeeID != $findAuthor){
                                    continue;
                                }else{
                            ?>
                                <li>
                                    <div class="eventBoxImage" style="background: url('images/<?php echo $photo ?>'); background-size: cover; background-position: center; border-radius: 50px"></div>
                                    <div class="eventContentContainer">
                                        <h1><?php echo $title; ?></h1>
                                        <h4>Posted By: <?php echo $author; ?></h4>
                                        <h5><?php echo $date ?></h5>
                                        <p><?php echo $article ?></p>
                                        <h6><a href="blog.php?id=<?php echo $id; ?>"><?php echo $contentLink ?></a></h6>
                                        <span style="background: url('images/<?php echo $iconPhoto ?>') no-repeat; <?php echo $realignIcon; ?> background-size: 45px;"><a href="#"></a></span>
                                    </div>
                                </li>
                            <?php }}?> <!-- Close php loop -->
                            </ul>
                        </div>
                        <div id="blogPostContainer">
                            <h1 class="blog">Create Blog Post</h1>
                            <form class="blog" action="includes/insert.php" method="post">
                                <input name="blogTitle" type="text" placeholder="Enter The Blog Title" required/>
                                <input  name="blogArticle" type="textbox" placeholder="Enter Your Blog Post" required/>
                                <input name="blogSubmit" type="submit" value="Submit" />
                            </form>
                        </div>
                        <div id="profileSearch">
                            <div id="searchBar">
                                <form id="search" action="index.php" method="POST">
                                    <div id="searchIcon"></div>
                                    <input type="search" name="search" placeholder="Search Through <?php echo $firstName ?>'s Posts" onkeyup="searchQ();" />
                                    <input type="hidden" name="editId" id="editId" value="<?php echo $employeeID ?>" />
                                    <div id="userResults">

                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>

这是函数

function display_blogs_profile($connect, $selectId)
            {

                $sql = "SELECT * FROM intranet";
                $query = mysqli_query($connect, $sql);
                $finalArray = array();
                $i = 0;

                while ($row = mysqli_fetch_assoc($query)) {
                    $id =  $selectId;
                    $title =  $row['title'];
                    $date =  $row['date'];
                    $article =  $row['article'];
                    $photo =  $row['photo'];
                    $icon =  $row['icon'];
                    $authorID =  $row['findAuthor'];
                    $author =  $row['author'];
                    if($row['icon'] === "3" && $authorID === $selectId){
                        $finalArray[$i] =  array($title, $date, $article, $photo, $icon, $author, $id, $authorID);
                    }
                    $i++;
                }   
                    $finalArrayDesc = array_reverse($finalArray);
                    return $finalArrayDesc;
            }

2 个答案:

答案 0 :(得分:0)

这是真的

  

&#34;似乎没有正确插入变量&#34;

那是因为它从未设定过。试试这个:

$selectedID = $_GET['id']; 

在检查if(isset($_GET['id']))之后

if(isset($_GET['id'])){
    $selectedID = $_GET['id'];
    .... 

答案 1 :(得分:0)

知道了!感谢您的帮助! if get语句和if $ employeeID!= $ findAuthor语句将破坏或继续循环,无论如何,除非我自己的帐户。我只需要拿出$ employeeID!= $ findAuthor语句:)