After using Explode() how can i search the different output elements in a query?


#1

Hello Guys!

Seriously, spending 5 hours on this is frustrating me. ;_; I’ve been coding for years but i’m not the most advanced and sometimes find it hard.

So i have a text input box named “topicname” - It’s part of a search engine where players can enter as many words as they’d like, and results relating to those words will display.

Anyway, i used $=explode(" ",$); to define the elements into one variable that i can search with a select query using LIKE i assume? I’m clueless.

So am i even doing this right? Separating the elements individually to then search? Not sure how one would go about this.

I just need to select from one table called ‘thread’, and display the titles of all outcomes. :3

Ehlle


#2

Basically, this is an example of explode…

$pizza = “piece1 piece2 piece3 piece4 piece5 piece6”; // a fulll pizza with 6 pieces

$pieces = explode(" ", $pizza); // 6 pieces broken up into an array

echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
etc…

Simple to use. You can parse thru the “pieces” using a for loop , let’s say $i $pieces[$i]… Whatever is needed.
Hope that helps…


#3

Could you show me the example of the loop / select query? That’s what my original question was lovely. ^^ :3

Ehlle :smiley:


#4

Well, any FOR or WHILE loop would work. This is a sample for learning:
[php]

<?PHP $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; // a fulll pizza with 6 pieces $pieces = explode(" ", $pizza); // 6 pieces broken up into an array for ($i = 0; $i < count($pieces); ++$i) { print $pieces[$i]; } ?>

[/php]
Now, here is the same code altered to work with a form, “topicname” field and database table “thread”.
Hopefully this is what you want. It basically just goes one by one thru the array creating a query…
[php]
// Get the posted line of words to search with
$topics = $_POST[‘topicname’];

// Break down the topics list into separate words
$words = explode(" ", $topics);

// Loop thru all words…
for ($i = 0; $i < count($words); ++$i) {
$query = “SELECT * FROM thread WHERE topic = '” . $words[$i] . “’”;
// Here run the query and display the results…
}
?>
[/php]
The above is based on a table called “thread” and a field in the table called “topic”.
You did not explain much about your table or fields, so that is my guess.
But, untested, something like that should work for you… Good luck.


#5

Thanks so much ^^ exactly what I needed and il test this in the morning :3 I knew what I wanted but didn’t know how to write it. xD


#6

Oh, one more thing, just FYI…

When you create a query, it looks like something like this:

SELECT * FROM thread WHERE topic = ‘someword’

But, in my example, the $query=“some query”, was blocked in double-quotes.
So, the SELECT sample above would be inside double-quotes and the topic inside single-quotes.
This usually confuses beginners and newer PHP programmers. I just wanted to point out that there
are, in fact, two double-quotes around my example and it has single-quotes around the topic word.
It is sometimes hard to see a single-quote next to a double-quote.

Hope that helps and does not mix you up further… LOL… Good luck, let us know the results…


#7

Okay, i’m getting there! I am not a newcomer to php either, i’ve been coding a couple of years. I’ve never done explode() implode() functions though until recently. xD

So here’s my code…

[php]
if (isset($_POST[‘searchforum’])){

$topics = ($_POST[‘topicname’]);

$words = explode(" ", $topics);

for ($i = 0; $i < count($words); ++$i) {

 $squery = mysql_query("SELECT * FROM thread WHERE title LIKE '%$words[i]%'");
 while($output = mysql_fetch_array($squery)){
 
 echo $output['title']; ?><br><br><?
 
 
 }
 
 }
 
  }[/php]

Basically, i need the words to be searched for ‘LIKE’ other words in the table. Thing is it never only picks up the words i need, but every field in the table. :3 How can i get this to work? Basically searching all fields for the words selected to be within each field in the DB.


#8

… After i tested some, i changed LIKE ‘%$words%’ to LIKE ‘%ferret%’, and it works perfectly? I assume it’s an error with the explode(); working with the different strings. Any suggestions?


#9

Hmmmm…

‘%$words[i]%’" ( What’s the ‘i’ )???

Try:

‘%$words[$i]%’" you want the index using the variable $i not just i…

Hope that was it… Good luck!