我在content.php中有一个while循环,我想像参数一样使用$ result。如何从content.php文件中调用函数并获取$ result以在我的while循环中使用?
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循环中使用它吗?
答案 0 :(得分:1)
首先使用require_once(./'your path'/functions.php');
在content.php中包含你的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();
答案 1 :(得分: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;
}
}
这将处理获取帖子并始终返回一个数组对象供您使用。
接下来,您必须确保包含require
或require_once
的功能。你可以通过
$posts = blogPosts_get();
然后使用
foreach ($posts as $post){
注意:自PHP 5.5起,不推荐使用MySQL_ *。使用MySQLi_ *或PDO。