Trying to filter the results after a query. Need to find a string inside of a tinytext string. That looks like: accounting, ACT, algebra 1, algebra 2, American history, biology, calculus, economics, English, European history, geometry, grammar, literature, piano, proofreading, psychology, reading, SAT math, SAT reading, SAT writing, statistics, violin, vocabulary,

Code - subject string to find is $subject and the list to search is $row['SubjectList']

$result = mysql_query($query, $dbConn);
$i=0;
while ($row = @mysql_fetch_assoc($result)){
    $results[$i]['Name'] = $row['Name'];
    $results[$i]['Zip'] = $row['ZipCode'];
    $results[$i]['SubjectList'] = $row['SubjectList'];
    $i++;
}
share|improve this question

58% accept rate
feedback

2 Answers

up vote 1 down vote accepted

you are doing it completely the wrong way.

  1. Such a filtering should be done on the DB side, not in PHP.
  2. These subjects should not be stored like that but in separate fields in a relational table.
share|improve this answer
just trying to work with what I have... can't move to a relational table and also the query is super complicated and I've posted 2 on how to add this search to it, but no one has given a working solution... so i'm trying to filter with conditionals after the fact... they have to go into an array anyway. can you help with current problem? – Jeffrey Apr 10 '11 at 8:37
feedback

While I agree with Col. Shrapnels answer, in your specific case you are looking for the strpos function if you really want to do it in PHP

share|improve this answer
I've tried the strpos and also explode and they return lots of empty $results. – Jeffrey Apr 10 '11 at 8:44
Can you update your question with the information of what you have tried and what failed? – ChrisWue Apr 10 '11 at 8:51
figured it out at the db query... had to remove sprintf – Jeffrey Apr 10 '11 at 9:15
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.