Multiple Array with Variables - Help

Hello,

I’ve been searching for some time and can’t figure out how to create an array with multiple variables. Anyways I’m trying to monitor Twitter and want to have a few keywords being scanned if something is mentioned. So, I’ve got a script that works fine but I cant figure out how to make it work with more than one keyword. The code is below, thanks for looking.

You can see my comment in the code, basically I want to have the below loop be able to look/find multiple items at the same time. I’m guessing there is a way just not sure how to.

Thanks.

[php]

<?php include_once('config.php'); $opts = array( 'http'=>array( 'method' => "POST", 'content' => 'track='.WORDS_TO_TRACK1, // Here is where I want to be able to have multiple keywords found, so for example have a WORDS_TO_TRACK2 ) ); $db = mysql_connect('localhost', 'username', 'password!'); mysql_select_db('database', $db); $context = stream_context_create($opts); while (1){ $instream = fopen('https://'.TWITTER_USERNAME.':'.TWITTER_PASSWORD.'@stream.twitter.com/1/statuses/filter.json','r' ,false, $context); while(! feof($instream)) { if(! ($line = stream_get_line($instream, 20000, "\n"))) { continue; }else{ $tweet = json_decode($line); //Clean the inputs before storing $id = mysql_real_escape_string($tweet->{'id'}); $text = mysql_real_escape_string($tweet->{'text'}); $screen_name = mysql_real_escape_string($tweet->{'user'}->{'screen_name'}); $followers_count = mysql_real_escape_string($tweet->{'user'}->{'followers_count'}); //We store the new post in the database, to be able to read it later $ok = mysql_query("INSERT INTO tweets (id ,text ,screen_name ,followers_count, created_at) VALUES ('$id', '$text', '$screen_name', '$followers_count', NOW())"); if (!$ok) {echo "Mysql Error: ".mysql_error();} flush(); } } } ?>

[/php]

Any ideas anyone?

Thanks!

Well, I am not sure about your code, but, one thing is that you should loop thru the search variables, not just do a while(1). Or, do you mean you want to search one-time with multiple inputs? Not clear on what you are asking of us.
Multiple arrays mean you have more than one array…
Multidimensional arrays mean you have multiple depths to the array… (IE: Array1[3][5] for a 3x5 array.)

Searching google or twitter can be done with multiple inputs such as “Dog and Cat” or just “Dog Cat”…
Or did you mean looping thru your inputs, “Dog”, “Cat” and calling your search using one then the other?

Let us know what your are asking for…

Hi ErniAlex,

Sorry I didn’t explain it properly. Is what I’m looking to do is looping thru my inputs. So one input will be “dog” and anthor input will be “cat”.

I’ve read that with the twitter Stream API that you can only have 1 username connect to search muiltple words. So if I have the one input of “dog” and the other input of “cat” i know the content varialbe I beleive will need to look like dog, cat.

So I need something that will build any inputs and put them in a comma seperated list. So the content varilible will be dog, cat, etc1, etc2 and so on.

Hopefully that a little more clear.

Thanks!

Yes, now I understand your question.

Well, I have never done anything with Twitter streams, but, it appears that your question is basically how to create the “WORDS_TO_TRACK1” variable which will become your “dog,cat,etc1” data.

First, how are you getting the dog, cat, etc1 from the user. I am assuming that you have a form that calls the code you posted. You would need to allow for multiple inputs on the posting page to do that. Either have separate fields for several entries to be made. (first one type in cat, second dog, 3rd etc1, etc…) Or just one input fields which you would parse for spaces and enter commas into them instead. (If you use this way, you would first strip all special characters, turn double-spaces into single ones and lastly replace spaces with commas.) There are tons of string functions in PHP which can be used to do both of these versions.

Let me know if I didn’t understand what you needed or if this helps, what else you may need help with.
Good luck…

Yes, I’ve currently got a form that stores items to a database. I’m have a dynamic list with an add button and input field. When the user inputs the keyword it is stored into the database.

What would be the best way to pull these multiple keywords from the database and into the WORDS_TO_TRACK1 ?

Well, perhaps we are getting a little further.

You have a field that allows input. You store this into your database. You want to retrieve this field and add it into your query to twitter. Correct?

If so, what can be typed into the field from the user? Do you mean they type in their words that are needed to be searched on? So, they can type in “Dog Cat” and you want to pull that into a variable that is then searched on? If so, retrieve it from the database into your $WORDS_TO_TRACK1 variable. Just pass it to the query you posted earlier. If you have many different words to combine into your $WORDS_TO_TRACK1 variable, that would depend on how you are storing all of the words. If all search words for a user are kept during their current session, then, you would have to parse thru each word adding it to the $WORDS_TO_TRACK1 variable. like: $WORDS_TO_TRACK1=$word1 . " " . $word2, etc… and the final output would be used to do the search.

I see to create a context using your words, you use:
$context = stream_context_create($opts);
Where the $opts is actually the words you created

I was under the assumption that the stream_context was a ‘header’ used when you open a stream.
If so, then this is not how you scan tweets for content. You would have to check with Twitter’s PHP API site to see how it is done.

And, here is an interesting one I found that gives the code to locate tweets based on input words. It also has a nice little display of the tweets found and some safety code to change colors if there are a lot of items found. http://blog.corunet.com/twitter-alerts-using-twitter-streaming-api/
Not sure if it will help or make things worse, but, I thought it worth looking at…
Hope one of these helps…

Yes, I want to retrieve the fields and add it to the query for twitter.

really anything can be typed into each field, whether its ‘@soandso’ or ‘#somehastag’ or ‘any key word’ (without the tics ’ of course )

And yes, I wan’t it do do like you said " If all search words for a user are kept during their current session, then, you would have to parse thru each word adding it to the $WORDS_TO_TRACK1 variable. like: $WORDS_TO_TRACK1=$word1 . " " . $word2, etc… and the final output would be used to do the search."

So really my $WORDS_TO_TRACK1 would need to contain this for example “@soandso, #somehashtag, any key word”

They comma , separates searches from what I gathered from the twitter API.

As for that link, thats the base code I’m trying to work off of. I just need a way for it to pull in the many fields from the database and add it to the $WORDS_TO_TRACK1 variable.

Again, Thanks for helping.

Sorry, have been busy… I reread out posts and it appears you just want to know how to read your database and put it into a variable. Here is some PHP code that would do it. Of course, this is just vague, you will have to match it up to your database and add the connection code. Hopefully, this is what you are looking for…
[php]
//Assuming database connection is already set up…

// connect to your database, get links data
$databaseID = mysql_connect( $hostname, $username, $password );
@mysql_select_db($dbname);

// set up query to select all user inputs from database
$query = "SELECT * FROM userinputs where userid=‘someone’ "; //just a guess, change to your setup
$results = mysql_query ( $query, $databaseID);

// loop through the results, creating the search variable
$search = “”;
while( $row = mysql_fetch_row($results) ) {
$search .= $row[1] . “,”;
}
$search = substr($search,0,strlen($search)-1); //Remove last comma…

//Close database connection…
mysql_close($databaseID);
[/php]
This will open your database, read all of the user inputs into a variable adding a comma between each…
Hope this works… Good luck…

Wow, that is what I’m looking for, thank you!

I’ll try and test it out.

Thanks ErnieAlex!

Glad I could help. Let us know how it works out for you. And, you can post your final code if you wish to help others… CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service