【问题标题】:Trying to display list of members试图显示成员列表
【发布时间】:2012-05-03 19:25:00
【问题描述】:

出于某种原因,我试图在列表中显示数据库中的所有成员,以便在单击它们时访问他们的每个个人资料,但我只获得了最后一个人的链接数据库,有什么帮助吗?

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 ---------

【问题讨论】:

    标签: php mysql html sql database


    【解决方案1】:

    我认为不是

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

    你要输入

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

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

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

    【讨论】:

    • YESSS 谢谢,我回显了下面的代码,但它有效!非常感谢!
    【解决方案2】:

    这是因为您在每次迭代时都覆盖了变量,所以您需要将数据保存在一个数组中,然后在您输出的任何地方执行另一个 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 />');
    }
    ?>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 2016-01-29
        • 1970-01-01
        相关资源
        最近更新 更多