Insert Into Mysql - Arrays

Discussion in 'PHP' started by zaay, Aug 14, 2009.

  1. #1
    Hello!

    I am budiling my own tags system. So when i post news, i have tags files, where i insert tags : some1,some2,some3 ... So now i want these tags to be each own for news. So every tag is one record for news. Because i want to make that they will click on tag and it will show all news with that tag. It cannot work if i won't have array of tags for each news or something. Because now some1,some2,some3 are one records and they are all with "," ... I want each tag own record for one news. How can i get array from input field, and insert into mysql database, how to read it. Any other option than array? Maybe trim() function?

    Thank you !
     
    zaay, Aug 14, 2009 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Use explode to split the values:
    $array = explode(",", $value_from_mysql);
    PHP:
    Would return something like;
    $array[] = 'some1';
    $array[] = 'some2';
    etc
    PHP:
    Or am I getting your question wrong?
     
    Sky AK47, Aug 14, 2009 IP
  3. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #3
    My first ideea on this :
    You can use mysql "like":
    
    $q=mysql_query("Select * from test where name like '%{$query}%'");
    While($r=mysql_fetch_array($q))
    {
    echo $r['id']."-".$r['name'];
    echo "<br/>";
    }
    
    Code (markup):
    I hope you got what i meant.
    Codebreaker.
    That will get all entries where one of the tags it's the query.
     
    codebreaker, Aug 14, 2009 IP
  4. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #4
    Hi!
    Thanx for that.. So i will get arrays out from database. That means " , " will get deleted and each tag in it's own array. But how can i insert arrays in database, so that i can create search script. Because i think your code is just for selecting from database and show it. Am i right ?

    Thanx for that, but i don't know what you mean with that. This is just a simple select from database code which i can use for search. Or i am wrong ? :)
     
    zaay, Aug 14, 2009 IP
  5. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    Yes, it's supposed to select from the database.
    You can use implode to add a array to the database:
    $stringFromArray = implode(",", $array);
    PHP:
     
    Sky AK47, Aug 14, 2009 IP
  6. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #6
    Hii!

    So i should use this right ? What i will got ? Each tag own record, or deleting ever " , " from this.. I really don't get it :D

    $tags = $_POST[tags];
    
    $tags1 = implode(",", $tags);
    
    $sql="INSERT INTO news (title, newstext, postdate,tags)
    VALUES
    ('$_POST[title]','$_POST[newstext]','$_POST[date]','" .$tags1. "')";
    PHP:
     
    zaay, Aug 14, 2009 IP
  7. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #7
    implode wont do anything as $_POST['tags'] isn't a array, so can you show me what is passed on to $_POST['tags']?
    And you have a few PHP errors, wanted to fix them but I want this to be sorted out first.
     
    Last edited: Aug 14, 2009
    Sky AK47, Aug 14, 2009 IP
  8. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #8
    IF i get you right you have a list of posts.
    And every post has some tags :
    Example :
    PoSt 1 tags : max,big,cars
    Post 2 tags: huge,cars
    Post 3 tags : no, matter.

    And you want to get all posts with tag cars :
    Then you do it like i said and in that while you will have the datas of that posts.
    That's the thing when you use like.
     
    codebreaker, Aug 15, 2009 IP
  9. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #9
    Yes for search script i can use like, which will search for specific tag.. But what about if i want to show list of tags. So i select from database all tags. In your example i will got:
    max,big,cars, huge,cars,no, matter.

    $tag = $row[tags]
    Let's say i want to link each tag to : tag.php?tag=" .$tag. "

    That will give me max,big,cars like ONE tag.. so i will got tag.php?tag=max,big,cars

    I want EACH tag as own record.. So i can have tag.php?tag=max;tag.php?tag=big;tag.php?tag=cars

    And then i will check for all news with that tag. Because tags i insert into input field when writing news, are ALL ONE .. that's wrong .. I want all tags before " , " as own record for each news.
     
    zaay, Aug 15, 2009 IP