0

我的 content.php 中有一个 while 循环,我想像参数一样使用 $result。如何从 content.php 文件中调用函数并在我的 while 循环中使用 $result?

  while ($row = mysql_fetch_array($result)) 
                {
                    $ename = stripslashes($row['name']);
                    $eemail = stripslashes($row['email']);
                    $ebox = stripslashes($row['lund']);
                    $ecategori = stripslashes($row['LundaBlogg']);
                    $epost = stripslashes($row['post']);
                    $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

                    echo
                    (
                        '<li> 
                            <div class="datum">'.$row['date'].'</div>
                            <div class="meta"><img src="'.$grav_url.'" alt="Gravatar" /><p>'.$ename.'</p></div>
                            </br>
                            <div class="categori"><p>Kategori: '.$ecategori.'</p></div>
                            <div class="topic"><p>'.$ebox.'</p></div>   
                            <div class="shout"><p>'.$epost.'</p></div>
                            <div class="raderaknapp"><a href="../delete_ac.php?id=' . $row['id'] . '"class=\"icon-2 info-tooltip\">Radera Inlägg</a></div>
                            <div class="raportpost"><p><a href="mailto:someone@example.com?Subject=Hello%20again">Rapportera inlägg</a></p></div>
                        </li>'
                    );

                    ?>
                        <h4>
                        <form action="<?php echo $self?>" method="POST">
                            <input type="hidden" name="id" value="<?php echo $row['id']?>"/>
                            <input type = "submit" name="Gilla" value = 'Gilla'"/>
                        </form>
                        </h4>

                        <div id="radera-infotwo">
                            <span onmouseover="ShowText('Messagetwo'); return true;" onmouseout="HideText('Messagetwo'); return true;" href="javascript:ShowText('Messagetwo')">
                                <img src="http://www.wallpaperama.com/forums/post-images/20081107_6718_question-mark.gif">
                            </span>
                        </div>  

                        <div id="radera-info">
                            <span onmouseover="ShowText('Message'); return true;" onmouseout="HideText('Message'); return true;" href="javascript:ShowText('Message')">
                                <img src="http://www.wallpaperama.com/forums/post-images/20081107_6718_question-mark.gif">
                            </span>
                        </div>

                        <?
                        ?>
                    <?

                    echo ("Antal Gilla: " .$row['likes']); 

                    if(isset($_POST['Gilla']) && $_POST['id'] == $row['id']){echo '<h5><img src="../images/tummen-upp.png"/>Du har Gillat detta inlägg.</h5>';}

                    echo "<hr>";  
                }   

这是我的 functions.php 文件,其中有我的函数 blogPosts_get()

function blogPosts_get(){ 
  $query = "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 8;";
  $result = mysql_query($query) or die('<p class="error">Wrong.</p>');   
}

我已经尝试在我的函数 blogPosts_get() 中返回,但我不明白它是如何工作的。有人可以帮我从我的函数 blogPosts_get() 中获取 $result 变量,以便我可以在我的 while 循环中使用它吗?

4

2 回答 2

1

首先将你的functions.php包含在content.php中require_once(./'your path'/functions.php');,它应该在你的文件content.php的开头,然后再将你的functions.php中的代码修改为:

function blogPosts_get(){ 
  $query = "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 8;";
  $result = mysql_query($query) or die('<p class="error">Wrong.</p>');  
return $result; 
}

当您在 content.php 中的循环之前调用它时,如下所示:

$result = blogPosts_get();
于 2013-03-13T18:15:49.267 回答
0

首先,简单的方法是修改函数以获取结果并构建一个数组对象供您使用,而不是返回一个 MySQL 查询对象。

function blogPosts_get(){
    $query = "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 8;";
    $result = mysql_query($query);
    if(!$result)
        return array();
    else{
        while($row = mysql_fetch_array($result))
            $posts[] = $row;
       return $posts;
   }  
}

这将处理获取帖子,并将始终返回一个数组对象供您使用。

接下来,您必须确保包含带有requireor的功能require_once。您可以通过以下方式调用它

$posts = blogPosts_get();

然后使用一个

foreach ($posts as $post){

注意:自 PHP 5.5 起,MySQL_* 已被弃用。使用 MySQLi_* 或 PDO。

于 2013-03-13T18:09:52.280 回答