SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to select record from unserialized Records

    i have a script which i`m using in ma site,for friends system.
    that if somebody accept me as friend,i appear in his friend list also he appears in my friends list.
    That works fine.
    Also when displaying the friend list is ok!!!(In which to all scripts i used serialize and unserialized functions.
    My Question is how do i select a single record from the unserialized record.
    Becoz i want to use it,display actions which where done with my friends(exmple:if he/she is in my friend list and he added photo,then i`ll be updated)
    below is accepting request file
    Code:
    <?php //Connect to the database serverinclude"config.php";  $loggain=$_SESSION['user'];$logged_id=$_POST['logged_id'];//the one accpeting requests$friend_id=$_POST['friend_id'];//the one to be added in the list$accept=$_POST['accept'];$reject=$_POST['reject'];if(isset($accept)) {    $query = mysql_query("SELECT * FROM request WHERE user = '$friend_id' AND friend = '$logged_id'");//this is the request table(panding to be accepted)    if(mysql_num_rows($query) > 0) {         $_query = mysql_query("SELECT * FROM profile WHERE id = '$friend_id'");//somebody profile including the friends column      $_row = mysql_fetch_array($_query);       $email=$_row['email'];      $friends = unserialize($_row["friendss"]);      $friends[] =$logged_id;             mysql_query("UPDATE profile SET friendss = '" . serialize($friends) . "' WHERE id = '$friend_id'");       $_query = mysql_query("SELECT * FROM profile WHERE id = '$logged_id'");      $_row = mysql_fetch_array($_query);       $friends = unserialize($_row["friendss"]);      $friends[] =$friend_id;             mysql_query("UPDATE profile SET friendss = '" . serialize($friends) . "' WHERE id = '$logged_id'");//    }   mysql_query("DELETE FROM request WHERE user = '$friend_id' AND friend = '$logged_id'");//delete after accepting.  include"1.php";  }<?php
    
    //Connect to the database server
    include"config.php";
    
    
    $loggain=$_SESSION['user'];
    $logged_id=$_POST['logged_id'];//the one accpeting requests
    $friend_id=$_POST['friend_id'];//the one to be added in the list
    $accept=$_POST['accept'];
    $reject=$_POST['reject'];
    if(isset($accept)) {
        $query = mysql_query("SELECT * FROM request WHERE user = '$friend_id' AND friend = '$logged_id'");//this is the request table(panding to be accepted)
        if(mysql_num_rows($query) > 0) {
    	
            $_query = mysql_query("SELECT * FROM profile WHERE id = '$friend_id'");//somebody profile including the friends column
          $_row = mysql_fetch_array($_query);
           $email=$_row['email'];
          $friends = unserialize($_row["friendss"]);
          $friends[] =$logged_id;      
                    
          mysql_query("UPDATE profile SET friendss = '" . serialize($friends) . "' WHERE id = '$friend_id'");
          
          $_query = mysql_query("SELECT * FROM profile WHERE id = '$logged_id'");
          $_row = mysql_fetch_array($_query);
          
          $friends = unserialize($_row["friendss"]);
          $friends[] =$friend_id;      
                    
          mysql_query("UPDATE profile SET friendss = '" . serialize($friends) . "' WHERE id = '$logged_id'");//
        }
    		
    	
     mysql_query("DELETE FROM request WHERE user = '$friend_id' AND friend = '$logged_id'");//delete after accepting.
     
     include"1.php";
     
     }
    and this one is friend list file
    Code:
     <?php  //Connect to the database server
     include"config.php"; 
      $user=$_SESSION['user'];
    $logged_id=$_SESSION['id'];
    $count=0;
    $query = mysql_query("SELECT friendss FROM profile WHERE id = '$logged_id'");
    $count=mysql_num_rows($query); 
    $sum=0; while($row = mysql_fetch_array($query))
     {    $friends= unserialize($row["friendss"]);    
      if(isset($friends[0])) {  
        foreach($friends as $friend) {       
     $_query = mysql_query("SELECT  *FROM profile WHERE id = '" . $friend . "'");		
    $counta=mysql_num_rows($_query);		
    $_row = mysql_fetch_array($_query); 
    $friend_id=$_row['id'];
    $name=$_row['names'];
    $email=$_row['emails'];
    $country=$_row['countrys'];
    $location=$_row['locations'];
    $langu1=$_row['langu1s'];
    $langu2=$_row['langu2s'];
    $family=$_row['familys'];
    $education=$_row['educations'];
    $age=$_row['ages'];
    $male=$_row['males'];
    }}}
    HELP ME IN THIS PLEASE)))

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just loop through the array until you find the array element that intrests you.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Instead of having a field in the profile table that contains all friend id's of a person, you should have a friends table that contains one row for each pair of friends.
    It would make maintenance and querying so much easier

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Creating a different table,i tried this idea but it was very difficult for me,when when i used a field it works nicely..
    CrMALIBU said i would loop until i find some element that intrests me,it sound Great but still i dont know how i can Do it.
    Example:i want to be updated if someone in my friends list has Uploaded photo..
    How i`m going to do this according to my codes above????
    Thankx)))

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido2004:I tried using using a different table,but it was very difficult for me.
    crmalibu:Said i should loop until i found some element which interest me,This sounds Great,but i still dont know how i`m i going to achive this..
    Example:
    if someone in my friend list uploads photo,i want a report which
    tells,your friend ...has uploaded photo.
    how can i do this ,according to my codes above???
    Thankx.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Before I try and explain to you how to do it with a separate friends table, please tell me how you can tell if someone has done some kind of action, like uploading a photo? Is there another field or table that contains user actions?

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes!!there is a table i named it Updates..there is where i want all the user actions to be stored)))

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Good. What fields does it contain?

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It contains
    id-autoid
    action-the action to be displayed
    sender_id-who sent the id
    date-when was it sent.

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The new table (friends) will have 2 columns, user and friend, just like the request table. Each new friendship will add 2 records to this table.

    (You might also merge the two tables into one, and add a third field, status, that indicates if a friendship is pending for approval, or active. But I'll explain with two separate tables).

    The accepting request file will become something like this:
    PHP Code:
    $loggain=$_SESSION['user'];
    $logged_id=$_POST['logged_id'];//the one accpeting requests
    $friend_id=$_POST['friend_id'];//the one to be added in the list
    $accept=$_POST['accept'];
    $reject=$_POST['reject'];
    if (isset(
    $accept)) {
      
    $result mysql_query("SELECT * FROM request WHERE user = '$friend_id' AND friend = '$logged_id'");//this is the request table(panding to be accepted)
      
    if (mysql_num_rows($result) > 0) {
        
    // don't know if you use $email later on, so I left this query
        
    $_query mysql_query("SELECT * FROM profile WHERE id = '$friend_id'");//somebody profile including the friends column
        
    $_row mysql_fetch_array($_query);
        
    $email=$_row['email'];
        
        
    // let's insert the two rows of the new friendship    
        
    $query 
          
    "INSERT friends 
           VALUES(
               '" 
    mysql_real_escape_string($logged_id) . "'
             , '" 
    mysql_real_escape_string($friend_id) . "'
           )"
    ;
        
    mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
        
    $query 
          
    "INSERT friends 
           VALUES(
               '" 
    mysql_real_escape_string($friend_id) . "'
             , '" 
    mysql_real_escape_string($logged_id) . "'
           )"
    ;
        
    mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
      }
        
      
    mysql_query("DELETE FROM request WHERE user = '$friend_id' AND friend = '$logged_id'"); // delete after accepting.

      
    include"1.php";

    And the friend list:
    PHP Code:
    //Connect to the database server
    include"config.php"
    $user=$_SESSION['user'];
    $logged_id=$_SESSION['id'];
    $count=0;
    $query "
      SELECT 
          id
        , names
        , emails
        , countrys
        , locations
        , langu1s
        , langu2s
        , familys
        , educations
        , ages
        , males 
      FROM friends
      INNER JOIN profile
      ON friends.friend = profile.id 
      WHERE user = '
    $logged_id'
      "
    ;
    $result mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
    $count=mysql_num_rows($result); 
    $sum=0
    while (
    $row mysql_fetch_array($query)) {
      
    $friend_id=$row['id'];
      
    $name=$row['names'];
      
    $email=$row['emails'];
      
    $country=$row['countrys'];
      
    $location=$row['locations'];
      
    $langu1=$row['langu1s'];
      
    $langu2=$row['langu2s'];
      
    $family=$row['familys'];
      
    $education=$row['educations'];
      
    $age=$row['ages'];
      
    $male=$row['males'];
      
    // don't you display the data or something?

    And finally, to get all updates the friends of a user made, you can use this query:
    Code:
    SELECT 
        updates.id
      , updates.action
      , updates.sender_id
      , updates.send_date
    FROM friends
    INNER JOIN updates
    ON friends.friend = updates.sender_id
    WHERE friends.user = '$logged_id'

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankx So much,this is wonderful...everything is working as i wanted)))
    Help me in this also,i want to enable members to able to upload VIDE0 in my site.
    but i dont know how to get Started...i mean i dont have knowledge in this.

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Never done that. I think you'd better start a new thread, with a title that explains the problem. That way you should get more and useful answers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •