Jump to content

If/elseif Help Needed Please


paulmo

Recommended Posts

in the example below, text searches are not being filtered through each if/else statement, so that, for example, when "good dog" is searched against db, i'm getting echoes from both the echo_light (echo) and echo_dark (echod) tables, when all that should be matching and echoed would be the echo_light table entry (since 'good' is in user string in this example). if i searched "bad dog" all i'd want to echo would be matched row from the echo_dark(echod) table, etc. any help greatly appreciated. justsomeguy wrote/helped me with this last winter and i'm just getting back to it. Thanks!

if (in_array(strtolower($word), array('you'))) {echo "You seem to be addressing me, {$post_name}</br></br>";}elseif (in_array(strtolower($word), array('great', 'good', 'fun', 'wonderful', 'beautiful', 'love', 'happy', 'exciting', 'joy',))) {echo "You're exuding positive energy. </br></br>";}$sql = ' ';$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%' OR a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}if (in_array(strtolower($word), array('desperate','anxious', 'panic', 'distressed', 'negative', 'stress', 'stressed', 'dark', 'sad', 'depressed', 'hurt', 'bleak', 'gloom', 'bad', 'lonely', 'alone', 'alien',))) {echo "Your tone seems grim or distressing, {$post_name}.";}$sqld = ' ';$sqld = "SELECT a.id, a.subject, b.echod FROM terms AS a, echo_dark AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%' OR a.subject LIKE '%$word%')";$result = mysql_query($sqld) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echod']));	echo "<br>";}

Link to comment
Share on other sites

Wow, has it been that long?Strip out the excess code and just look at the structure. You'll notice that you always run the query on the second table. In fact, you always run the query on the first table also. The if statements are only printing a line of text, not controlling the database work. This is what your code structure looks like:

if (word is you) {  print personal note}elseif (word is "light") {  print "light" note}run "light" database query, print resultsif (word is "dark") {  print "dark" note}run "dark" database query, print results

Notice how the 2 database parts aren't part of the if structure. You probably want a structure like this:

if (word is you) {  print personal note}elseif (word is "light") {  print "light" note  run "light" database query, print results}elseif (word is "dark") {  print "dark" note  run "dark" database query, print results}

Also, your queries contain this:a.subject LIKE '%$word%' OR a.subject LIKE '%$word%'That's the same condition written twice, you can leave out the OR and just write that condition once.

Link to comment
Share on other sites

Wow, has it been that long?
yes indeed, hope you are doing well. i seem to learn in a compulsive fashion.
You'll notice that you always run the query on the second table. In fact, you always run the query on the first table also.
yes, user array match gets sent to a.id and a.subject first, then those id's, depending on light or dark array match, get sent to either echo or echod (dark) table, where the matching id row is echoed to user.
a.subject LIKE '%$word%' OR a.subject LIKE '%$word%'
ok, reduced this to a.subject LIKE '%$word%'have also deleted extra { }s around each database part so structure matches your suggestion. now, only the personal 'you' message is working. not getting any database interaction now, whereas before i was getting too much (echoing both echo and echod when only one should be echoed). i know this isn't much to go on. if you can suggest, thanks, if not i'll try to troubleshoot more.
Link to comment
Share on other sites

if (in_array(strtolower($word), array('you'))) {echo "You seem to be addressing me, {$post_name}. </br></br>";}elseif (in_array(strtolower($word), array('great', 'good', 'fun', 'wonderful', 'beautiful', 'love', 'happy', 'exciting', 'joy',))) {echo "You're exuding positive energy. </br></br>";$sql = ' ';$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) $n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}elseif (in_array(strtolower($word), array('desperate','anxious', 'panic', 'distressed', 'negative', 'stress', 'stressed', 'dark', 'sad', 'depressed', 'hurt', 'bleak', 'gloom', 'bad', 'lonely', 'alone', 'alien',))) {echo "Your tone seems grim or distressing, {$post_name}.";$sqld = ' ';$sqld = "SELECT a.id, a.subject, b.echod FROM terms AS a, echo_dark AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sqld) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) $n++;echo nl2br(stripslashes($row['echod']));	echo "<br>";}

gracias

Link to comment
Share on other sites

  • 1 month later...

am back at it, jsg.the 'good' and 'bad' statements from the script are echoing to user, but only echo_light table row (from db) is echoing, and its echoing for both 'good' and 'bad' matches. the echo_dark table row is not echoing ('bad music' for example...the arts are one of my subject rows...just returns the 'good music' statement).

if (in_array(strtolower($word), array('great', 'good', 'wonderful', 'beautiful', 'love', 'happy', 'joy',))) {echo "You are exuding a feeling of positive energy, {$post_name}! </br></br>";}

getting mysql undefined index 'echo' error, so this seems to be the line that's tripping things up. VV

$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}	elseif (in_array(strtolower($word), array('bad', 'dark', 'evil', 'kill', 'maim', 'destroy', 'hate', 'war', 'murder',))) {echo "You are feeling grim at the moment, {$post_name}. </br></br>";}$sql = "SELECT a.id, a.subject FROM terms AS a, echo_dark AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";	}

thanks in advance for continuing help.

Link to comment
Share on other sites

now, both 'echo' and 'echod' rows are being echoed. one or the other needs to be echoed, depending on the 'good' or 'bad' array. code only executing (as it is) with if, not elseif statements. with elseif, whether i remove brackets or put them in, either get parse error or code doesn't work at all. thanks

if (in_array(strtolower($word), array('great', 'good', 'wonderful', 'beautiful', 'love', 'happy', 'joy'))) {echo "You are exuding a feeling of positive energy, {$post_name}! </br></br>";}$sql = ' ';$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}	if (in_array(strtolower($word), array('desperate','anxious', 'panic', 'distress', 'negative'))){echo "You are feeling grim at the moment, {$post_name}. </br></br>";}$sql = ' ';$sql = "SELECT a.id, a.subject, b.echod FROM terms AS a, echo_dark AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;echo nl2br(stripslashes($row['echod']));	echo "<br>";

Link to comment
Share on other sites

This goes back to post 2, but this is how you have your code structured:

if (check for good){  print good}search database for goodprint results for goodif (check for bad){  print bad}search database for badprint results for bad

In other words, the only thing the if statements control is whether or not the initial message gets printed, not anything to do with searching in the database or printing the results. The database code always runs regardless of what happens with the if statements, the database code is not inside any if statement. This is how it should be structured:

if (check for good){  print good  search database for good  print results for good}if (check for bad){  print bad  search database for bad  print results for bad}

Now the database parts are within the if statements instead of outside.

Link to comment
Share on other sites

yes jsg i previously tried your bracket suggestions. i am understanding how the db section needs to be part of the if statement.here is what i just tried, which results in no echoes from db. for brevity i'll show 'good' section only and variations. thanks-- version 1:

if (in_array(strtolower($word), array('great', 'good', 'wonderful', 'beautiful', 'love', 'happy', 'joy'))) {echo "You are exuding a feeling of positive energy, {$post_name}! </br></br>";// removed bracket here$sql = ' ';$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) { //keep this one$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}   }

(not that it makes a difference, but i tried closing brackets side-by-side on same line as well.)version 2:

if (in_array(strtolower($word), array('great', 'good', 'wonderful', 'beautiful', 'love', 'happy', 'joy'))) {echo "You are exuding a feeling of positive energy, {$post_name}! </br></br>";// remove bracket$sql = ' ';$sql = "SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE a.id=b.id AND (a.subject LIKE '%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) //remove bracket$n++;echo nl2br(stripslashes($row['echo']));	echo "<br>";}

neither version echoes a row.

Link to comment
Share on other sites

The first one should work correctly, the inner brackets are needed for the while loop. The second version would keep incrementing the counter but would only display the last row. Both of them should show at least one row if it was returned though. If you're not seeing any rows, print out the SQL query you build and try to run it on the database directly.

Link to comment
Share on other sites

unless i'm something, i can't replicate 'good' or 'bad music' in sql query because my script filters the 'good' or 'bad' part before the text string goes to the db table 'terms' (in other words, good and bad are in script not db tables). but i ran this in sql, and it found no results:

SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE (a.id=b.id AND a.subject LIKE 'music')

in testing i ran simple queries that did return results.this has got me thinking: should i just do all text processing via database? like set up more tables instead of using script to handle the good, bad? but then there would be a table sequence, like string first goes to good/bad tables, then if bad, goes to terms table to match string subject keyword, then from there goes to corresponding id on echo_dark table where the message echoes back to user. if no good/bad match from first tables, then string goes to terms table for keyword match, and echoes back "please elaborate on baseball" (or whatever). ?? thanks

Link to comment
Share on other sites

It's probably going to be easiest to do most of the processing with PHP. You could even use less tables in fact, you could store everything in one table and have columns to identify which searches the record should show up for.But, this query:SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE (a.id=b.id AND a.subject LIKE 'music')will only return records where the subject is exactly "music". If you want to return results that contain music, you need to use the % wildcard character, e.g.:SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE (a.id=b.id AND a.subject LIKE '%music%')That will return all records which contain "music". This, for example, would only return records which start with "music":SELECT a.id, a.subject, b.echo FROM terms AS a, echo_light AS b WHERE (a.id=b.id AND a.subject LIKE 'music%')

Link to comment
Share on other sites

you mean more info in script and less in tables? as i've followed your mysql statement, it's pretty cool and i wish it would work for less tables... 1 table= process, 5 rows= good, bad, keyword, bad_echo, good_echo

$sql = "SELECT * FROM process WHERE MATCH good AGAINST ('%$word%')";

if match there, go to keyword column for match which = id of good_echo, echo message to user. if no good match, then select from process where match bad, etc. if no match on either good or bad, then keyword match, if match there, echo "please elaborate on $row(['keyword'])if this is the right track, please help guide me setting up. thanks!!

Link to comment
Share on other sites

for whatever reason that query is not working. so i'm picking up on your idea to do this in one table. this will simplify things. please help with query: applying to table 'release' fields below, $%word% gets matched against good, if match, $%word% gets matched against keyword, if match, echo same row in echo_good to user (keyword row id = echo_good row id).same process for match on bad (except echo_bad).if no match against either good or bad, but match on keyword, echo message to user "please elaborate on (keyword)"PRIMARY BTREE Yes No id 3 Agood FULLTEXT No No good 0bad FULLTEXT No No bad 0keyword FULLTEXT No No keyword 0echo_good FULLTEXT No No echo_good 0echo_bad FULLTEXT No No echo_bad 0thanks in advance for code or direction to get me started.

Link to comment
Share on other sites

thanks--please help me along on this one:

$sql = ' ';$sql = "SELECT * FROM release WHERE MATCH good AGAINST ('%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;$sql = "SELECT * FROM release WHERE MATCH keyword AGAINST ('%$word');$result = mysql_query($sql) or die (mysql_error ());//parse error here V V$sql = "SELECT * FROM release WHERE keyword.id = echo_good.id AND (keyword.id LIKE '%$word%');$result = mysql_query($sql) or die (mysql_error ());while($row = mysql_fetch_assoc($result)) {$n++;nl2br(stripslashes($row['echo_good']));	echo "<br>";	}}	$anymatches=mysql_num_rows($result); if ($anymatches == 0) { $sql = "SELECT * FROM release WHERE MATCH bad AGAINST ('%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;$sql = "SELECT * FROM release WHERE MATCH keyword AGAINST ('%$word');$result = mysql_query($sql) or die (mysql_error ());$sql = "SELECT * FROM release WHERE keyword.id = echo_bad.id AND (keyword.id LIKE '%$word%');$result = mysql_query($sql) or die (mysql_error ());while($row = mysql_fetch_assoc($result)) {$n++;nl2br(stripslashes($row['echo_bad']));	echo "<br>";	}}	if ($anymatches == 0) { $sql = "SELECT * FROM release WHERE MATCH keyword AGAINST ('%$word%')";$result = mysql_query($sql) or die (mysql_error ());$n=0;while($row = mysql_fetch_assoc($result)) {$n++;nl2br(stripslashes($row['keyword']));	echo "Please elaborate on ($row['keyword']))";	}}

Link to comment
Share on other sites

thanks, some syntax errors. now getting this:

Message: Good waterYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release WHERE MATCH good AGAINST ('%water%')' at line 1
which is referring to first line of query:
$sql = "SELECT * FROM release WHERE MATCH good AGAINST ('%$word%')";

so my query's not executing, and the complete string "good water" does not seem to be getting searched (see %water%).$word variable originated like this; $message= $get_search= $word string should be same words as $post_message.

$message = mysql_real_escape_string($message); //sql injection$message = strip_tags($message); $pre_filter=trim($message);$get_search=explode(" ",$pre_filter);$post_message = stripslashes($post_message);echo "Message: {$post_message}</br></br>";$where = '';$common = array('they', 'this', 'that', 'them', 'those', 'your',);foreach ($get_search as $word){  $word = trim($word);

big thanks for continued help on query and variable!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...