Jump to content

Text Join Help


paulmo

Recommended Posts

It's a little hard to read the code without indenting, but it looks like you're running both queries no matter what. The respective queries should go inside the if statements that apply to them. e.g., instead of this:

if (in_array(strtolower($word), array('great', 'good'))) {echo "You're happy. </br></br>";} // end of if$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>";}

You would do this:

if (in_array(strtolower($word), array('great', 'good'))) {echo "You're happy. </br></br>";$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>";}} // end of if

Link to comment
Share on other sites

Post the code you're using now and what issues you're having. About the indenting, obviously it's a lot easier to read indented code than otherwise. Compare the code I posted with this, obviously it's a lot easier to see the structure of the code and understand what it's doing. You don't have to think about where the if ends, or where the while loop ends, it's just easy to see where it ends.

if (in_array(strtolower($word), array('great', 'good'))) {  echo "You're happy. </br></br>";  $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>";  }} // end of if

Link to comment
Share on other sites

ok, thanks. getting matches on exact phrases, like "good horse" echoes the good statement in application and field row match. BUT it's also returning the "echo_dark" row, when that should only be returned if "bad" or "sad" was in string. and if i write "that's a really good horse," no matches are happening.

if (in_array(strtolower($word), array('great', 'good'))) {   echo "You are happy. </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('sad', 'bad'))) {   echo "You feel bad.</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

The problem isn't with this code, you can change the phrase on the top and see what it does.

<?php$words = explode(' ', 'that\'s a really good horse');foreach ($words as $word){  if (in_array(strtolower($word), array('great', 'good'))) {	echo "You are happy. </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('sad', 'bad'))) {	echo "You feel bad.</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>";	} */  }}?>

If you're seeing something that prints both responses, it's because you have one "good" word and one "bad" word in the phrase.

Link to comment
Share on other sites

If you're seeing something that prints both responses, it's because you have one "good" word and one "bad" word in the phrase.
i hear what you're saying but unfortunately that's not the case; the string 'good horse' is matched against the application array, then if it's 'good' the string should be getting matched to 'terms' (where 'horse' is matched), then 'echo_light' field match only. 'good horse' does not have 'bad' or 'sad' in it, and bad/sad's corresponding table, echo_dark, has no 'good' in it. so i'm flummoxed as to why both table field matches on 'horse' are returning. it would seem to me that the query is not working, and it's just returning both table fields. but that's hard to imagine since the queries are specific to the the 'good' or 'bad' tables. or perhaps the if statements somehow are not filtering the application matches (like either 'good' or 'bad'). like if 'good' is in the string, then only the echo_light field is returned. any guidance is appreciated. thanks for replying again.
Link to comment
Share on other sites

First off, trust that the code works. The queries and if statements are doing exactly what you're telling them to do, guaranteed. The basic code is straightforward:

<?php$words = explode(' ', 'that\'s a really good horse');foreach ($words as $word){  if (in_array(strtolower($word), array('great', 'good'))) {	echo "You are happy. </br></br>";  }  if (in_array(strtolower($word), array('sad', 'bad'))) {	echo "You feel bad.</br></br>";  }}?>

The basic code is two if statements, each checking if a value is in an array. It might find both, if the phrase is "good sad" then it will match both if statements (it's not making sure it only matches one, so it could match both). The database doesn't even come into this part, it's only using in_array. But, like I said, the code above is not the problem, that code is pretty basic. If there's a problem with the code it's in a part that you haven't posted.

Link to comment
Share on other sites

ok, i trust that the if statements work. so, the problem must be in the query, insofar as both echo_light and echo_dark table fields (echo and echod) are being returned, when only one or the other should be returned, depending on the array words in the application page. and i'm not even searching 'good bad' in string; more like 'good horse', which should only return related echo_light table field 'echo'.

$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%')";

Link to comment
Share on other sites

What I'm saying is I don't think the probem is in any of the code that you posted in post 30. I think theres an issue in some other code, like the code that loops through the words or something else. The reason the query is not the problem is because if the word is not "sad" or "bad", then it's not going to run the query at all.

Link to comment
Share on other sites

  • 3 weeks later...

i'm posting the code here in hopes you'll see why it's not executing properly with the database tables. i'll illustrate the problem based on the example phrases 'good music' 'bad music' 'music bad' 'music good', hypothetically submitted user submit strings. first problem is that the app or query is "reading" only the last word of string, so that 'good music' returns the echo_light and echo_dark table fields (echo and echod, more about that in a minute), but does not echo the 'positive energy' statement in app, which is my goal: to have the app statement echoed, followed by the table field matches. if 'music good' is entered, then only the app statement is echoed, and not the field matches. second problem is that (as mentioned above), the good and bad (as it were) if statements in app are not filtering the string properly; both table fields are being returned when just the echo_light (good) or echo_dark (bad etc) table fields would be returned if the string matches the app statement. those app statements are like a filter that would send the string--after it's matched against "terms" table first--to either the echo_light table OR echo_dark table, but not both.

$message = mysql_real_escape_string($message);$message = strip_tags($message); $name = trim ($name); $name = strip_tags($name); //remove code$name = mysql_real_escape_string($name);$pre_filter=trim($message);$get_search=explode(" ",$pre_filter)$post_message = stripslashes($post_message);?><?php$common = array('they', 'this', 'that', 'them', 'those', 'your');foreach ($get_search as $word){  $word = trim($word);  $skip = false;    if (strlen($word) < 4)  {	if (!in_array(strtolower($word), array('air', 'sea', 'key', 'bee', 'key', 'fat', 'red',)))	  $skip = true;    }  if (in_array(strtolower($word), $common))	$skip = true;}if (in_array(strtolower($word), array('you'))) {echo "You seem to be addressing me, {$post_name}; the Release Center might better serve you if you elaborate. </br></br>";}elseif (in_array(strtolower($word), array('great', 'good', 'fun', 'wonderful', 'beautiful', 'love', 'happy', '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 below is for 'dark' query$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

1. I see 3 syntax errors in arrays that have an extra comma.2. This code doesn't do anything useful:

foreach ($get_search as $word){  $word = trim($word);  $skip = false;    if (strlen($word) < 4)  {	if (!in_array(strtolower($word), array('air', 'sea', 'key', 'bee', 'key', 'fat', 'red',)))	  $skip = true;    }  if (in_array(strtolower($word), $common))	$skip = true;}

It sets a variable called $skip, but that's all it does. $skip doesn't get used anywhere else.3. All of the code after the previous loop uses a variable called $word. $word gets set every time through the loop, so after the loop ends $word will only be set to the last word.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...