homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Extract values from array
can echo but need it to be stored in an individual variables
abushahin




msg:4400901
 3:26 pm on Dec 23, 2011 (gmt 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.

 

rocknbil




msg:4400936
 5:13 pm on Dec 23, 2011 (gmt 0)

What's the query? Select, insert, update, delete?

abushahin




msg:4400949
 5:51 pm on Dec 23, 2011 (gmt 0)

Rocknbil, it's going to do a select query.

Chico_Loco




msg:4401035
 9:59 pm on Dec 23, 2011 (gmt 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;

httpwebwitch




msg:4401123
 7:49 am on Dec 24, 2011 (gmt 0)

SELECT * from table WHERE column IN ('walk','talk')

abushahin




msg:4401174
 4:21 pm on Dec 24, 2011 (gmt 0)

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

if (!$SQL)

rocknbil




msg:4401192
 6:03 pm on Dec 24, 2011 (gmt 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 ()

abushahin




msg:4401200
 6:23 pm on Dec 24, 2011 (gmt 0)

Thanks a million!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved