0

I have a website I'm building with two user streams. I have the code for the befriended and its just a case of using the pseudo code else if statments in the right place.. Only its racking my brains out.

What I need is that when me user1_id visits you user2_ids stream for it to hide both the form and the stream if we're not friends and tell me to add you before viewing. Showing you've added this friend but friend has not accepted...Or show the form and stream if we're friends.

Here is all the code

This is to determine my users and its working..

<?

$user1_id=$_SESSION['id'];
if($_SESSION['id']==$user1_id){
        define('user_isself','2');
    }else{
        define('user_isself','1');
    }

    $friends = rawfeeds_user_core::check_active_friends($id,$_SESSION['id']);

    define('USER_BEFRIENDED',$friends);

    ?>  

Here is the code for my two feeds the first feed from the frist form is user1_ids feed and from the second user2_id's feed.

<?php if($user2_id==$_SESSION['id']){ ?>
    <div id="statusholder">
    <div class="divider">
        <h2>Post a status</h2>  
        <form action="insert.php" method="POST" target="ifr1" class="form_statusinput">
        <input type="hidden" name="toid" value="<?php echo $user1_id ?>">
        <input type="text" name="newmsg" id="newmsg" class="homescreen_status_input" placeholder="Say something" autocomplete="off">
        <iframe name="ifr1" id="ifr1" style="display:none;"></iframe>
        <input type="submit" target="ifr1" value="Share" >
    </form>
    </div></div>


    <?php 

    //FRIENDSHIPS
            $following_string       =   $_SESSION['id'];
            $sql_follows="SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2";
            $query_follows=mysql_query($sql_follows) or die("Error finding friendships");
            if($query_follows>0){
            $friendlist="(".$_SESSION['id'].",";
            $t=0;
            while ($follow=mysql_fetch_array($query_follows))
            {
                if($follow['user1_id']==$_SESSION['id']){
                    if($t>0){
                    $friendlist=$friendlist.",";
                    }
                    $friendlist =   $friendlist. $follow['user2_id'];
                }else{

                    if($t>0){
                        $friendlist=$friendlist.",";
                    }
                    $friendlist =   $friendlist. $follow['user1_id'];

                }
                $t=$t+1;
            }
            $friendlist=$friendlist.")";
            }
        //STREAMDATA
        $badcall = "(".$_SESSION['id'].",)";

if($friendlist==$badcall){
$friendlist="(".$_SESSION['id'].")";
}

        //FRIENDSHIPS
        $user1_id;
            if(isset($data['id'])){
            $user2_id = $data['id'];
            }else{
            $user1_id = $_SESSION['id'];
            }
            $following_string       =   $_SESSION['id'];
            $sql_follows="SELECT * FROM friends WHERE user1_id=".$user1_id." OR user2_id=".$user2_id;
            $query_follows=mysql_query($sql_follows) or die("Error finding friendships");

            while ($follow=mysql_fetch_array($query_follows))
            {
            if($follow['user2_id']==$_SESSION['id']){
                $following_string   =   $following_string. " OR streamitem_creator =" . $follow['user1_id']." OR streamitem_target=".$follow['user1_id'];
            }else{
                $following_string   =   $following_string. " OR streamitem_creator =" . $follow['user2_id']." OR streamitem_target=".$follow['user2_id'];
            }
            }

        //STREAMDATA

        $call="SELECT * FROM streamdata WHERE streamitem_target = " . $user1_id ." AND streamitem_type_id NOT IN ('2') ORDER BY streamitem_timestamp DESC LIMIT 15";
    $chant= mysql_query($call) or die(mysql_error());
        $num = mysql_num_rows($chant);
        if($num>0){

            while($streamitem_data = mysql_fetch_array($chant)){



                echo'<div id="statusholder"><div class="divider">';
                if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
                echo "<img class='stream_profileimage' style='border:none;padding:0px;display:inline;' src='";rawfeeds_user_core::output_profile_image_url($streamitem_data['streamitem_creator']);echo "' onerror='this.src=\"/img/no_profile_img.jpeg\";'>
                ";
                    $poster_name = rawfeeds_user_core::getuser($streamitem_data['streamitem_creator']);
                    $target_name = rawfeeds_user_core::getuser($streamitem_data['streamitem_target']);
                    $cont = stripslashes($streamitem_data['streamitem_content']);

                    if(!($streamitem_data['streamitem_type_id']==2)){
                    $cont = htmlentities($cont);
                    $cont = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a class='user_link' href=\"\\0\">\\0</a>", $cont);

                    }

                    if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        echo "<a href='/sn-profile.php?user1_id=".$poster_name['id']."'>" . $poster_name['fullusersname']."</a><span class='subtleLink'> said </span>";
                    }else{
                        echo "<a href='/sn-profile.php?user1_id=".$poster_name['id']."'>" .$poster_name['fullusersname']."</a>  
                        <span class='subtleLink'>wrote</span>";
                    }
                    echo "<br/><a href='#' class='subtleLink' style='font-weight:normal;'>".Agotime($streamitem_data['streamitem_timestamp'])."</a><hr>";

                    if($streamitem_data['streamitem_type_id']==2){
                    $cont = nl2br($cont);
                    echo "<div style='display:inline;'><span class='subtleLink'>".$cont."</span> </div>";
                    }else{

                        if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        $cont = nl2br($cont);
                            echo "<div>".$cont."</div>";
                        }else{
                        $cont = nl2br($cont);
                        echo "<div>".$cont."</div>";

                        }
                        echo"</div></div>";
                    }}}

                ?>  
                    <div class="stream_show_posts" onClick="global_streamcount=global_streamcount+10;refreshstream();">Show More Posts</div>
                    <?php                   
}else{
            echo "<div class='global_softerror'>No posts yet!</div>";
        }

    }else{
    ?>
-----------------------------------------START OF USER2_ID'S STREAM-------
<div id="statusholder">
    <div class="divider">
        <h2>Post on <?php echo $data['fullusersname']; ?>'s stream</h2>
        <form action="insert.php" method="POST" target="ifr2" class="form_statusinput">
        <input type="hidden" name="toid" value="<?php echo $user2_id ?>">
        <input type="text" name="newmsg" id="newmsg" class="homescreen_status_input" placeholder="Say something" autocomplete="off">
        <iframe name="ifr2" id="ifr2" style="display:none;"></iframe>
        <input type="submit" target="ifr2" value="Share" >

    </form>
    </div></div>


    <?php 
    //FRIENDSHIPS
            $following_string       =   $_SESSION['id'];
            $sql_follows="SELECT * FROM friends WHERE user2_id=".$_SESSION['id']." AND status=2 OR user1_id=".$_SESSION['id']." AND status=2";
            $query_follows=mysql_query($sql_follows) or die("Error finding friendships");
            if($query_follows>0){
            $friendlist="(".$_SESSION['id'].",";
            $t=0;
            while ($follow=mysql_fetch_array($query_follows))
            {
                if($follow['user2_id']==$_SESSION['id']){
                    if($t>0){
                    $friendlist=$friendlist.",";
                    }
                    $friendlist =   $friendlist. $follow['user1_id'];
                }else{

                    if($t>0){
                        $friendlist=$friendlist.",";
                    }
                    $friendlist =   $friendlist. $follow['user2_id'];

                }
                $t=$t+1;
            }
            $friendlist=$friendlist.")";
            }
        //STREAMDATA
        $badcall = "(".$_SESSION['id'].",)";

if($friendlist==$badcall){
$friendlist="(".$_SESSION['id'].")";
}

  date_default_timezone_set("GMT");


        //FRIENDSHIPS
        $user1_id;
            if(isset($data['id'])){
            $user2_id = $data['id'];
            }else{
            $user1_id = $_SESSION['id'];
            }
            $following_string       =   $_SESSION['id'];
            $sql_follows="SELECT * FROM friends WHERE user2_id=".$user2_id." OR user1_id=".$user1_id;
            $query_follows=mysql_query($sql_follows) or die("Error finding friendships");

            while ($follow=mysql_fetch_array($query_follows))
            {
            if($follow['user1_id']==$_SESSION['id']){
                $following_string   =   $following_string. " OR streamitem_creator =" . $follow['user2_id']." OR streamitem_target=".$follow['user2_id'];
            }else{
                $following_string   =   $following_string. " OR streamitem_creator =" . $follow['user1_id']." OR streamitem_target=".$follow['user1_id'];
            }
            }

        //STREAMDATA

        $call="SELECT * FROM streamdata WHERE streamitem_target = " . $user2_id ." AND streamitem_type_id NOT IN ('2') ORDER BY streamitem_timestamp DESC LIMIT 15";
    $chant= mysql_query($call) or die(mysql_error());
        $num = mysql_num_rows($chant);
        if($num>0){

            while($streamitem_data = mysql_fetch_array($chant)){

                echo'<div id="statusholder">
    <div class="divider">';
                if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
                echo "<img class='stream_profileimage' style='border:none;padding:0px;display:inline;' src='";rawfeeds_user_core::output_profile_image_url($streamitem_data['streamitem_creator']);echo "' onerror='this.src=\"/img/no_profile_img.jpeg\";'>
                ";
                    $poster_name = rawfeeds_user_core::getuser($streamitem_data['streamitem_creator']);
                    $target_name = rawfeeds_user_core::getuser($streamitem_data['streamitem_target']);
                    $cont = stripslashes($streamitem_data['streamitem_content']);

                    if(!($streamitem_data['streamitem_type_id']==2)){
                    $cont = htmlentities($cont);
                    $cont = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a class='user_link' href=\"\\0\">\\0</a>", $cont);

                    }

                    if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        echo "<a href='/profile.php?username=".$poster_name['username']."'>" . $poster_name['fullusersname']."</a><span class='subtleLink'> said </span>";
                    }else{
                        echo "<a href='/profile.php?username=".$poster_name['username']."'>" .$poster_name['fullusersname']."</a>  
                        <span class='subtleLink'>wrote</span>";
                    }
                    echo "<br/><a href='#' class='subtleLink' style='font-weight:normal;'>".Agotime($streamitem_data['streamitem_timestamp'])."</a><hr>";

                    if($streamitem_data['streamitem_type_id']==2){
                    $cont = nl2br($cont);
                    echo "<div style='display:inline;'><span class='subtleLink'>".$cont."</span> </div>";
                    }else{

                        if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        $cont = nl2br($cont);
                            echo "<div>".$cont."</div>";
                        }else{
                        $cont = nl2br($cont);
                        echo "<div>".$cont."</div>";
                        }
                    echo'</div>';
                    }}
            }
                    }else{
            echo "<div class='global_softerror'>No posts yet!</div>";
                    }
                    echo'<div class="stream_show_posts" onClick="global_streamcount=global_streamcount+10;refreshstream();">Show More Posts</div>';
    }
}

            ?>

And this is the code I need to insert.

<?php if(USER_BEFRIENDED==0){ ?>
    Add <?php echo $data['fullusersname']; ?> as a friend to see posts and information.
    <?php } 
    if(USER_BEFRIENDED==1){
    ?>
    You've added <?php echo $data['fullusersname']; ?> as a friend, but he hasn't accepted yet. Check back later!
    <?php } }?>

If I thought about it..I would have put this in the code before coding user2_id's stream..And I wouldn't be in such a pickle now.

Hope someone can help.

Thank you.

dave
  • 1,009
  • 5
  • 15
  • 26
  • 3
    Hey Mike. With all respect to you, I didn't ask for your opinion on if we wanted another social networking site. You don't even know what I'm making my website for..I just need help with what is more than likely a small coding issue that can be solved by an outsider who hasn't coded it. I'm sure this is the place to come for help/guidence and suggestions..Maybe I'm missing the point of it.... – dave Jul 03 '12 at 23:13
  • dave, you are correct. It is a place for help/guidance and I apologize if my comment offended you. Just whenever I see a question asking about friend relationship stuff, I always think "blegh, another person trying to make the new facebook". However, nobody is going to read through your wall of code. If you want someone to actually answer your question correctly, you're far better to provide as little code as possible. Also see here for a related question: http://stackoverflow.com/questions/1009025/facebook-database-design – Mike Jul 03 '12 at 23:28
  • Its ok. You didn't offend me at all. If people would read, they would see I'm only asking where I place my pseudo code between user1_ids form/stream and user2's so it works like I intend it to.. All the code is there, its just getting it in the right place that is baffling me. :) – dave Jul 03 '12 at 23:35

2 Answers2

1

Looking through your code, I assume you want to drive the friendships from a database. I don't quite see the relevance of a lot of the code posted but I can make a few recommendations:

Write a function to return the friendship "status"

I guess you'll be wanting to do similar things (content-hiding/primitive privacy) elsewhere, so this'll hopefully be useful. Obviously I can't test this but something like:

function checkUserFriendship($user1,$user2)
{
  $sql = "SELECT status FROM friends WHERE user1_id = $user1 AND user2_id = $user2 OR user1_id = $user2 AND user2_id = $user1";
  $query = mysql_query($sql);
  $data  = mysql_fetch_array($query);
  return $data['status'];
}

Now, I highly doubt that will work straight off, but what it would (hopefully) achieve is returning the "status" field you have in the "friends" table. You could then use this when outputting. For instance:

$friendshipStatus = checkUserFriendship($user1,$user2);

switch($friendshipStatus){
 case "accepted":
  //Show something
 break;
 case "pending":
  //Show "pending" message
 break;
 case 0:
  //Show nothing (or an error message)
 break;
}

This might not be what you are looking for, but it's how I would tackle the situation. It looks like you may have already done this with the line containing

rawfeeds_user_core::check_active_friends

but you posted a lot of code and I'm not entirely sure how it all pieces together.

Use Of Globals

This is a side note, but I'd avoid the use of defined globals. It doesn't look like they serve a purpose here.

Unnecessary Code (?)

In your user streams (let us refer to them as that) you have a huge block of code constructing the "IN (n,n,n,n)" part of the SQL to only select certain friends. I imagine this is necessary on the global / public / "News Feed" part of the site but I can't see where you use it. (It may be an oversight / just stupidity on my part!)

  • Thank you Simon, your post was very constructive, helpfull and to the point. I have successfully implemnted what you suggested onto what I already had, and its working 100% how I needed it to. Thank you so much for your time and your effort. – dave Jul 04 '12 at 11:31
0

Without reading all your code, i would recommend a simple multidimensional array:

$friends_of_users["user1"] = array("user3", "user9");
$friends_of_users["user2"] = array("user5", "user6");
$friends_of_users["user3"] = array("user1", "user7");

this is pseudocode, you should work with IDs instead of "userX", sure. This is by far not the sexiest solution, but it will solve your problem.

Sliq
  • 15,937
  • 27
  • 110
  • 143
  • I plan on the site holding many memebers tho. And I don't think a multidimensional array is an ideal solution for this. I need to try minimizing code for a clean backend! – dave Jul 03 '12 at 23:07