Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Extract values from array

can echo but need it to be stored in an individual variables

     
3:26 pm on Dec 23, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 20, 2009
posts: 98
votes: 0


Hi, I have taken this example from www and have amended it to stem words for searching db, I need help extracting the values and then searching againgst my db.

require_once'class.stemmer.inc';
$title = "walking and talking";
$stop_words = array("the", "and", "a", "to", "of", "in", "i", "is","that", "it", "on", "you", "this", "for", "but",
"with", "are", "have", "be", "at", "or", "as", "was", "so", "if", "out", "not");
$stemmer = new Stemmer;
$words = explode(" ", $title);
foreach($words as $word) {
$stem = $stemmer->stem($word);
if(!in_array($stem, $stop_words)) {
$stem_words[] = $stem;
}
}
foreach ($stem_words as $k=>$v) {
echo "$v ";
}


now this echos walk talk which is correct but i need it to be in a variable or somehow need this to be inside an sql query i dont want to put the sql query inside the foreach as that will do multiple queries, is there any way i can achieve this?
any help appreciated.
5:13 pm on Dec 23, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


What's the query? Select, insert, update, delete?
5:51 pm on Dec 23, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 20, 2009
posts: 98
votes: 0


Rocknbil, it's going to do a select query.
9:59 pm on Dec 23, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2002
posts:810
votes: 0


foreach ($stem_words as $k=>$v) {
if (!$SQL)
{
$SQL.="field_name='$v'";
}
else
{
$SQL.=" OR field_name='$v'"
}
}

$SQL="SELECT * FROM db WHERE $SQL";

// Whatever code to run your $SQL query here;
7:49 am on Dec 24, 2011 (gmt 0)

Moderator from CA 

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4059
votes: 0


SELECT * from table WHERE column IN ('walk','talk')
4:21 pm on Dec 24, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 20, 2009
posts: 98
votes: 0


Thanks for the replies guys, just wondered what this is?

if (!$SQL)
6:03 pm on Dec 24, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


It means if the where statement has not been built, don't use or. Another way,

$where=null;


foreach ($stem_words as $k=>$v) {
// only add "or" if the where statement has already been started
if ($where) { $where .= ' or'; }
$where .=" field_name='$v'";
}

$select = "select * from table";
if ($where) { $select .= " where $where"; }

Note the spaces, important.
If you use these in combination with AND, the or's will need to be wrapped in parentheses ()
6:23 pm on Dec 24, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 20, 2009
posts: 98
votes: 0


Thanks a million!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members