Welcome to WebmasterWorld Guest from 54.145.208.64

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)

5+ Year Member



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)

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



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

5+ Year Member



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

WebmasterWorld Senior Member 10+ Year Member



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)

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



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

5+ Year Member



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

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

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



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)

5+ Year Member



Thanks a million!