www.webdeveloper.com
Results 1 to 6 of 6

Thread: how do i check if my string is in part of my sql row

  1. #1
    Join Date
    Feb 2007
    Posts
    66

    how do i check if my string is in part of my sql row

    hey,

    I have a string "4902030187736"
    And I want to check if it exists in part of my row data

    row data: "4902030187736, 9182743918623, 19283615263177" - it exists
    or
    row data: "9182743918623, 19283615263177" - it doesn't exist

    $query = "SELECT * FROM likedislike WHERE site='".$_GET['site']."'";

    > If it exists I want to delete it, aswell as the ", " if one exists after it
    > If it doesn't exist I want to append to it

    thanks in advance for your help

    I haven't tried doing this myself, as I am not very experienced in php mysql, and I haven't a clue where I would start

  2. #2
    Join Date
    Sep 2009
    Posts
    93
    Try it, it helps you

    $row_data = "4902030187736, 9182743918623, 19283615263177";

    $req = "4902030187736";

    $row_data_pieces = explode(",", $row_data);

    $req_data = "";

    if($row_data_pieces[0] == $req)
    {
    for($i = 1; $i<count($row_data_pieces); $i++)
    {
    $req_data .= $row_data_pieces[$i] . ",";
    }

    $req_data .= $req;
    }

    if (!in_array($req, $row_data_pieces))
    {
    $req_data .= $row_data . "," . $req;
    }

    echo $req_data;

  3. #3
    Join Date
    Nov 2010
    Posts
    9
    Pprakash is dead on but one thing, I would reommend not using spaces at all for the stored data, just use the ','

  4. #4
    Join Date
    Feb 2007
    Posts
    66
    thankyou guys, but I kinda wanted it for mysql

    my "row_data" is a mysql result

    table > likedislike
    rows > site
    > like
    > dislike

    DB screenshot
    http://okapi.no-ip.biz/images/db.png

    and i have this code, which finds out if the user is logged in and already likes/dislikes + it also counts home many people like/dislike if you set todo=count

    PHP Code:
    <?php
    include('include/session.php');
        if(
    $session->logged_in) {
            if(
    $_GET['type']) {
                
    $query "SELECT * FROM likedislike WHERE site='".$_GET['site']."'";
                
    $sql mysql_query($query);
                
    $count mysql_num_rows($sql);
                while(
    $row mysql_fetch_array$sql ) ) {
                    
    $part explode(", "$row[$_GET['type']]);
                    
    $array = array();
                    
    $parts = (int)count($part) - 1;
                    for(
    $i=0$i <= count($part); $i++) {
                        
    $array[] = $part[$i];
                    }
                    if(@
    $_GET['todo'] == 'count') {
                        echo 
    $parts;
                    }else{
                        if(
    in_array($session->accessid$array)) {
                            echo 
    "you_".$_GET['type'];
                        }else{
                            echo 
    "you_dont_".$_GET['type'];

                            
    $query "UPDATE likedislike SET ".$_GET['type']." = CONCAT(".$_GET['type'].", '".$session->accessid.", ') WHERE site='".$_GET['site']."'";
                            
    mysql_query($query);
                        }
                    }
                }
            }
        }

    ?>
    where it says > echo "you_dont_".$_GET['type']; ( where $_GET['type'] is like/dislike )
    I want it to add the users id which is $session->accessid and then remove them from the other type

    so say type is "like"
    then it would append their id to "like", and remove it from "dislike"

    To remove, would this work:

    PHP Code:
    $sql mysql_query("SELECT {$_GET['type']} FROM likedislike WHERE site='".$_GET['site']."'");
    while( 
    $row mysql_fetch_array$sql ) ) {
         
    $new_str str_replace("{$session->accessid}, """$row[$_GET['type']]);
         
    mysql_query("UPDATE likedislike SET ".$_GET['type']." = $new_str WHERE site='".$_GET['site']."'");

    haha, sorry if that makes no sense, just finished work and I am really tired

    thanks

    Vinny
    Last edited by vinny619; 11-19-2010 at 03:09 AM.

  5. #5
    Join Date
    Feb 2007
    Posts
    66
    p.s. I have removed all spaces

  6. #6
    Join Date
    Feb 2007
    Posts
    66
    right this is what i have come out with, It adds the users id to the "dislike", but it will not remove it from the "like"... can one of you correct it for me please

    PHP Code:
    <?php
    include('include/session.php');
        if(
    $session->logged_in) {
            if(
    $_GET['type'] == 'like' || $_GET['type'] == 'dislike') {
                if(@
    $_GET['type'] == 'like') {
                    
    $otherLike 'dislike';
                }else if(@
    $_GET['type'] == 'dislike') {
                    
    $otherLike 'like';            
                }
                
    $query "SELECT * FROM likedislike WHERE site='".$_GET['site']."'";
                
    $sql mysql_query($query);
                
    $count mysql_num_rows($sql);
                while(
    $row mysql_fetch_array$sql ) ) {
                    
    $part explode(","$row[$_GET['type']]);
                    
    $parts = (int)count($part) - 1;
                    if(@
    $_GET['todo'] == 'count') {
                        echo 
    $parts;
                    }else{
                        if(
    in_array($session->accessid$part)) {
                            echo 
    "you_".$_GET['type'];
                        }else{
                            echo 
    "you_dont_".$_GET['type'];
                            
    $sql mysql_query("SELECT * FROM likedislike WHERE site='".$_GET['site']."'");
                            while( 
    $row mysql_fetch_array$sql ) ) {
                                
    $new_str str_replace($session->accessid.","""$row[$otherLike]);
                                
    mysql_query("UPDATE likedislike SET ".$otherLike." = '".$new_str."' WHERE site='".$_GET['site']."'");
                                
    mysql_query("UPDATE likedislike SET ".$_GET['type']." = CONCAT(".$_GET['type'].", '".$session->accessid.",') WHERE site='".$_GET['site']."'");
                            } 
                        }
                    }
                }
            }
        }

    ?>
    ---------------------------------------------- EDIT
    I have gone over the code about 20 times, and I cannot see a problem with it ... I thinks me needs Coffee

    my DB again - before/after I click Dislike Button
    http://okapi.no-ip.biz/images/db2.png
    Last edited by vinny619; 11-19-2010 at 05:01 AM. Reason: :( checked code - no problems

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles