14 Replies - 1021 Views - Last Post: 30 April 2012 - 07:40 AM Rate Topic: -----

#1 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

can someone please help me explain this code better

Posted 28 April 2012 - 06:33 AM

I have written this code for a search query on my website as a project but I want to explain it a bit better than I am doing, could someone please help me do this please:

$qry = "SELECT * FROM files  WHERE " ;
  $kwarr = preg_split('/ /', $search, -1, PREG_SPLIT_NO_EMPTY) ;
  $firsttime = 0 ;
  foreach ( $kwarr as $kw ) {
    if ($firsttime == 1 ) { $qry = $qry . " OR "; }
    $qry = $qry . " ( FileName LIKE '%".$kw."%' ) OR ( KeyWords LIKE '%".$kw."%' ) " ;
    $firsttime = 1; 


I have said: The array is started off as 0, then for each search term entered if the term is equal to what is held in the filename or keywords field of the database then return the results, the array is then 1 and the loop continues until all search terms have been searched for.

is this right?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: can someone please help me explain this code better

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 08:46 AM

Try outputting your query after it's built. Does it look right?
Was This Post Helpful? 0
  • +
  • -

#3 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 08:53 AM

View PostCTphpnwb, on 28 April 2012 - 09:46 AM, said:

Try outputting your query after it's built. Does it look right?


the query works fine I just want to be able to explain how it works better
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:07 AM

Are you not understanding preg_split, or the loop? The loop is awkward because $firsttime should be $notfirsttime since that's what it's testing.
Was This Post Helpful? 0
  • +
  • -

#5 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:12 AM

View PostCTphpnwb, on 28 April 2012 - 10:07 AM, said:

Are you not understanding preg_split, or the loop? The loop is awkward because $firsttime should be $notfirsttime since that's what it's testing.


yeah the loop is what im trying to understand, so if its not the first time then loop?

although im not really sure about what the no_empty bit of preg_split is..

This post has been edited by Addio569: 28 April 2012 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:21 AM

If you don't understand the loop how did you write it? Copy/paste programming will not get you anywhere.
  $notfirsttime = 0 ; // Initialize notfirsttime to false because it's the first time
  foreach ( $kwarr as $kw ) { // Loop 
    if ($notfirsttime == 1 ) { // if it is not the first time through the loop add the " or "
    	$qry = $qry . " OR "; 
    }
    $qry = $qry . " ( FileName LIKE '%".$kw."%' ) OR ( KeyWords LIKE '%".$kw."%' ) " ; // add more to query
    $notfirsttime = 1; // It will no longer be the first time, so set notfirsttime to 1. This line should be in the if statement above.
   }

Study the above, and try to never write a line of code without knowing what it does and why you're typing it.
Was This Post Helpful? 1
  • +
  • -

#7 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:25 AM

View PostCTphpnwb, on 28 April 2012 - 10:21 AM, said:

If you don't understand the loop how did you write it? Copy/paste programming will not get you anywhere.
  $notfirsttime = 0 ; // Initialize notfirsttime to false because it's the first time
  foreach ( $kwarr as $kw ) { // Loop 
    if ($notfirsttime == 1 ) { // if it is not the first time through the loop add the " or "
    	$qry = $qry . " OR "; 
    }
    $qry = $qry . " ( FileName LIKE '%".$kw."%' ) OR ( KeyWords LIKE '%".$kw."%' ) " ; // add more to query
    $notfirsttime = 1; // It will no longer be the first time, so set notfirsttime to 1. This line should be in the if statement above.
   }

Study the above, and try to never write a line of code without knowing what it does and why you're typing it.


my lecturer helped me write it, I sort of understood but it was confusing me. so the loop continues until there is nothing left to search for? can you tell me why when i search for 3 or 4 things it brings back everything from the database?

thanks

This post has been edited by Addio569: 28 April 2012 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:45 AM

It's better that you explain why it should only look for 3 or 4 things. Have you read about LIMIT?
Was This Post Helpful? 0
  • +
  • -

#9 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 09:57 AM

View PostCTphpnwb, on 28 April 2012 - 10:45 AM, said:

It's better that you explain why it should only look for 3 or 4 things. Have you read about LIMIT?


no, when I search for 3 or 4 terms it brings back everything from my database! have I got to limit the amount it brings back then?
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 10:01 AM

Why do you think it might only find 3 or 4 items? You can use LIMIT, but that won't guarantee that you get the results you want. What if the data you want is numbers 100 and 101 of your current query? You need to understand the query you're writing and whether or not it will return what you're looking for.
Was This Post Helpful? 0
  • +
  • -

#11 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 10:20 AM

View PostCTphpnwb, on 28 April 2012 - 11:01 AM, said:

Why do you think it might only find 3 or 4 items?


it doesn't it returns everything! i understand how LIMIT works but I cant see the relevance of it.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 28 April 2012 - 10:38 AM

View PostAddio569, on 28 April 2012 - 01:20 PM, said:

View PostCTphpnwb, on 28 April 2012 - 11:01 AM, said:

Why do you think it might only find 3 or 4 items?

it doesn't it returns everything!

I understand that. The question is: What is it about your query that causes it to return everything? Is your query specific enough to return precisely what you're looking for? It appears not.
Was This Post Helpful? 0
  • +
  • -

#13 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: can someone please help me explain this code better

Posted 29 April 2012 - 05:13 AM

View PostCTphpnwb, on 28 April 2012 - 11:38 AM, said:

View PostAddio569, on 28 April 2012 - 01:20 PM, said:

View PostCTphpnwb, on 28 April 2012 - 11:01 AM, said:

Why do you think it might only find 3 or 4 items?

it doesn't it returns everything!

I understand that. The question is: What is it about your query that causes it to return everything? Is your query specific enough to return precisely what you're looking for? It appears not.


i dont understand? my query works, are you saying it only allows you to search for 2 terms?
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,492
  • Joined: 08-August 08

Re: can someone please help me explain this code better

Posted 29 April 2012 - 06:18 AM

If your query worked you wouldn't have a question to ask: you're asking why it gets more results than you expect so therefore it is not working the way you want. So now you need to look at the query the way it appears to the mysql server and determine why it gets more results than you expect.
Was This Post Helpful? 0
  • +
  • -

#15 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: can someone please help me explain this code better

Posted 30 April 2012 - 07:40 AM

0 == first time.
1 == second time.
1 == third time
1 == ...
Something funky happens when word or word or word or word or word or word is in either field with increased probability of any word being in either field.
Just a guess.

$qry = $qry . " ( FileName LIKE '%".$kw."%' ) OR ( KeyWords LIKE '%".$kw."%' ) " ; // Is just a string concatenation assignment.

No search is in the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1