IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> If/elseif Help Needed Please
paulmo
post Nov 3 2009, 03:27 AM
Post #1


Member
**

Group: Members
Posts: 136
Joined: 7-January 08
From: maine usa
Member No.: 19,341



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!
CODE
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>";
}
Go to the top of the page
 
+Quote Post
justsomeguy
post Nov 3 2009, 05:37 AM
Post #2


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



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:

CODE
if (word is you)
{
  print personal note
}
elseif (word is "light")
{
  print "light" note
}

run "light" database query, print results

if (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:

CODE
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.
Go to the top of the page
 
+Quote Post
paulmo
post Nov 3 2009, 06:06 PM
Post #3


Member
**

Group: Members
Posts: 136
Joined: 7-January 08
From: maine usa
Member No.: 19,341



QUOTE (justsomeguy @ Nov 3 2009, 12:37 AM) *
Wow, has it been that long?
yes indeed, hope you are doing well. i seem to learn in a compulsive fashion.
QUOTE
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.
QUOTE
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.
Go to the top of the page
 
+Quote Post
justsomeguy
post Nov 3 2009, 06:22 PM
Post #4


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



Can you post the code you have now?
Go to the top of the page
 
+Quote Post
paulmo
post Nov 3 2009, 09:16 PM
Post #5


Member
**

Group: Members
Posts: 136
Joined: 7-January 08
From: maine usa
Member No.: 19,341



CODE
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
Go to the top of the page
 
+Quote Post
justsomeguy
post Nov 3 2009, 09:44 PM
Post #6


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



You still need the brackets for the while loops, as it is now it's only looping over the first line ($n++), so it would only display the last row in each result.
Go to the top of the page
 
+Quote Post
paulmo
post Nov 6 2009, 03:45 AM
Post #7


Member
**

Group: Members
Posts: 136
Joined: 7-January 08
From: maine usa
Member No.: 19,341



thanks jsg, i'll be experimenting this weekend and will report back here. regards, paulmo
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 23rd November 2009 - 04:39 AM