根据用户从mysql数据库中显示特定数据

时间:2014-02-07 00:08:29

标签: php mysql database

我试图让它成功,如果用户查看他们的页面,他们可以看到他们已经或正在完成的作业。然后我想这样做,如果他们查看另一个用户页面,他们可以看到他们创建的项目。

    $my_id = $_SESSION['user_id'];
        $frnd_query = mysql_query("SELECT user_one, user_two FROM frnds WHERE user_one = '$my_id' OR user_two = '$my_id'");
        while($run_frnd = mysql_fetch_array($frnd_query)) {
            $user_one = $run_frnd['user_one'];
            $user_two = $run_frnd['user_two'];
            if($user_one == $my_id) {
                $user = $user_one;
            } else {
                $user = $user_two;
            }
            $username = getuser($user, 'username');
            echo "<a href = 'my_ideas.php?user=$user' class = 'list' style=display:block>Ideas</a>";

        ?>  
    <ul>

        <li><a href = '#my_links'>Linked Ideas</a></li>
        <li><a href = '#idea_chemistry'>My Idea Chemistry</a></li>
        <li><a href = 'settings.php'>Profile Settings</a></li>>
    </ul>
</div>

            <div id = 'my_projects'>

    <?php


            if($user != $_SESSION['user_id']) {
                $ideas_sql = "SELECT * FROM projects WHERE user_id = $username";
                $query = mysql_query($ideas_sql) or die(mysql_error());
                $rsIdeas = mysql_fetch_assoc($query);

                do {
                ?>
                <h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>

                    <?php echo $rsIdeas['keywords']; ?></p>
                    <p><?php echo $rsIdeas['description']; ?></p>
                    <?php } while ($rsIdeas = mysql_fetch_assoc($query));


            } else {

            $ideas_sql = "SELECT * FROM projects WHERE user_id = $my_id";
            $query = mysql_query($ideas_sql) or die(mysql_error());
            $rsIdeas = mysql_fetch_assoc($query);   


            do {
            ?>
        <h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>

            <p><?php echo $rsIdeas['keywords']; ?></p>

            <p><?php echo $rsIdeas['description']; ?></p> 
            <?php } while ($rsIdeas = mysql_fetch_assoc($query)); 
                }
                     ?>
                <?php
                }

            ?>

我已经搜索了这个并在这里搜索了相关的问题,但我似乎无法找到任何有助于解决我的问题。

1 个答案:

答案 0 :(得分:0)

这基本上是一个代码审查问题。 SO网络上还有另一个网站,您可以在此处提出此类问题。虽然你应该证明你有一些编码知识。

我不确定为什么没有启动PHP标记,但这是第一部分。

<?php

$my_id = $_SESSION['user_id'];
    $frnd_query = mysql_query("SELECT user_one, user_two FROM frnds WHERE user_one = '".$my_id."' OR user_two = '".$my_id."'");
    while($run_frnd = mysql_fetch_array($frnd_query)) {
        $user_one = $run_frnd['user_one'];
        $user_two = $run_frnd['user_two'];
        if($user_one == $my_id) {
            $user = $user_one;
        } else {
            $user = $user_two;
        }
        $username = getuser($user, 'username');
        echo "<a href = 'my_ideas.php?user='".$user."' class = 'list' style=display:block>Ideas</a>";

    ?>  
<ul>

    <li><a href = '#my_links'>Linked Ideas</a></li>
    <li><a href = '#idea_chemistry'>My Idea Chemistry</a></li>
    <li><a href = 'settings.php'>Profile Settings</a></li>>
</ul>



            <div id = 'my_projects'>

<?php


        if($user != $_SESSION['user_id']) {
            $ideas_sql = "SELECT * FROM projects WHERE user_id = '".$username."'";
            $query = mysql_query($ideas_sql) or die(mysql_error());
            $rsIdeas = mysql_fetch_assoc($query);

            do {
            ?>
            <h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>

                <?php echo $rsIdeas['keywords']; ?></p>
                <p><?php echo $rsIdeas['description']; ?></p>
                <?php } while ($rsIdeas = mysql_fetch_assoc($query));


        } else {

        $ideas_sql = "SELECT * FROM projects WHERE user_id ='".$my_id."'";
        $query = mysql_query($ideas_sql) or die(mysql_error());
        $rsIdeas = mysql_fetch_assoc($query);   


        do {
        ?>
    <h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>

        <p><?php echo $rsIdeas['keywords']; ?></p>

        <p><?php echo $rsIdeas['description']; ?></p> 
        <?php } while ($rsIdeas = mysql_fetch_assoc($query)); 
            }
                 ?>
            <?php
            }

        ?>

问题:这是自定义函数getuser()