0

出于某种原因,我试图在列表中显示数据库中的所有成员,以便在单击它们时访问他们的每个配置文件,但我只获取数据库中最后一个人的链接,任何帮助?

include_once "../../mysql_server/connect_to_mysql.php";

//This code is used to display friends in the box of friends
$sql = mysql_query("SELECT * FROM myMembers");
$numberofRows = mysql_num_rows($sql);

$memberDisplayList = 'There are ' . $numberofRows .' members<br /><br />';

while($row = mysql_fetch_array($sql)) {

    $id = $row['id']; 
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];

    ///////  Mechanism to Display Pic. See if they have uploaded a pic or not 
    $check_pic = "../../members/$id/image01.jpg";
    $default_pic = "../../members/0/image01.jpg";
    if (file_exists($check_pic)) {
        $user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"80px\" />"; 
    } else {
        $user_pic = "<img src=\"$default_pic\" width=\"80px\" />"; 
    }

    $memberDisplayList = '<a href="http://www.pathtosite.com/friends_page.php?id='. $id .'">' . $firstname .' '. $lastname .'</a><br />';   
}
// ------- END WHILE LOOP FOR GETTING THE MEMBER DATA ---------
4

3 回答 3

2

我认为而不是

$memberDisplayList = '<a href= (...etc)

你打算输入

$memberDisplayList .= '<a href= (...etc)

这会将新链接附加到您的字符串。

此外,您似乎没有在任何地方呼应您的$user_pic$memberDisplayList字符串。

于 2012-05-03T19:31:46.047 回答
1

这是因为您在每次迭代时覆盖了变量,您需要将数据保存在数组中,然后在输出时执行另一个 foreach 循环:

<?php 
while($row = mysql_fetch_array($sql)){
    ///////  Mechanism to Display Pic. See if they have uploaded a pic or not  //////////////////////////
    $check_pic = "../../members/{$row['id']}/image01.jpg";
    $default_pic = "../../members/0/image01.jpg";
    if (file_exists($check_pic)) {
        $user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"80px\" />";
    } else {
        $user_pic = "<img src=\"$default_pic\" width=\"80px\" />";
    }

    $user[] = array('id'=>$row['id'],
                    'firstname'=>$row["firstname"],
                    'lasname'=>$row["lastname"],
                    'user_pic'=>$user_pic,
                    'display_list'=>'<a href="http://www.pathtosite.com/friends_page.php?id='. $row['id'].'">' . $row["firstname"] .' '. $row["lastname"] .'</a><br />');
}
?>
于 2012-05-03T19:32:55.217 回答
0

您实际上在哪里构建 HTML?您在提供的代码中设置了一堆变量,这看起来不错。所以它可能在表示逻辑中。如果这在循环中,那么你的状态很好。但是如果它在循环之外,那么我无法想象除了最后一行之外你会在哪里显示任何东西。

于 2012-05-03T19:32:03.407 回答