Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Give items in foreach ID on name

    Here is what I try to do:
    - I read a RSS feed in and put the items in separate arrays. (done)
    Now here comes the trick:
    - When an item contains a certain word it gets an ID.
    - This has to be putted into a database. (done)

    Like this:

    RSSfeed:
    [item1]
    [item2]
    [item3]

    When [item1] contains the word "word1" ID array value is 1.
    When [item2] contains the word "word2" ID array value is 2.
    ...

    This gets into the database:
    - INSERT INTO table (itemname, itemlink, ID) VALUES($name, $link, $id)

    How do I scan for the word and set the ID value?
    Last edited by Pcfr43k; 01-22-2010 at 09:45 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Are you sure you want to create the ID yourself, or let MySQL automatically
    create the incremental ID? If you do it yourself, there's a possibility of duplicating
    an ID. If MySQL has an "auto-increment" ID, it will never use the same one twice.

    Let us know your thoughts on that part first.

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    It had to become a second ID

    AutoID - Itemname - ExtraID

    So it has to be something like this:

    - Read RSSfeed
    - Foreach place in arrays
    - if array[1] contains "item1", $ID1 = "1"
    - INSERT INTO table (item1, ID1) VALUES($array1, $ID1)
    - In database: (autoID, Item1, ID1)

    This extra ID is for categorizing.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    So the RSS words are in an array ...
    How about the other words that it is compared to?
    How many of those are there, and is that also an array?

  • #5
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    There are 26 words for comparing.
    These can be read from a database and put in an array, or already set in arrays.
    The words for comparing are static and won't change.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Below is my little test.

    If you only have 26 words, I don't think a nested loop will be a problem.
    But how many items will be in the RSS array? If there are hundreds or
    thousands, then it might be a problem. But this seems the simplest to me.

    Maybe someone has a preg_match idea that is more efficient?

    Here's what I came up with real quick:
    You may get some ideas from it.
    PHP Code:
    <?php

    // The list of words to compare
    $words = array("apple","orange","peach","lime","lemon","banana");

    // The RSS array ... for testing.
    $rss = array("this is banana test1","this is a test2","this is an apple test3","this is a lime test4","this is apple");

    foreach(
    $words as $item1){
    $i=0;
        foreach(
    $rss as $item2){
          if(
    stristr($item2,$item1)){
          echo 
    "<b>".$item1."</b> was found in RSS string (".$i.") ".$item2."<br />";
          }
          
    $i++;
        }
    }
    ?>
    Here is the output:
    apple was found in RSS string (2) this is an apple test3
    apple was found in RSS string (4) this is apple
    lime was found in RSS string (3) this is a lime test4
    banana was found in RSS string (0) this is banana test1




    .

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    function match_num$string$word )
    {
        
    preg_match'/\b' preg_quote$word'/' ) . '(\d+)\b/i'$string$matches );
        return 
    array_pop$matches );

    PHP Code:
    var_dumpmatch_num'kbluhm is a loser''is' ) ); // NULL 
    PHP Code:
    var_dumpmatch_num'kbluhm is2 a loser''is' ) ); // string(1) "2" 
    Something like that?

  • #8
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    This is perfect!
    Here I can go further with.
    I'll post my script here later for use of other scripts.


  •  

    Tags for this Thread

    Posting Permissions

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