Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
hinksta
Worker
Worker



Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Mon Dec 18, 2006 11:11 am Reply with quote

This runs ok but if the ELSE statement is called, nothing shows up.
I guess I'm missing something but can't see it.
Code:
$result = $db->sql_query('SELECT keyword,keyword_link,club FROM '.$prefix."_mysearch WHERE keyword = '$search'"); 

while ($row = $db->sql_fetchrow($result))
{
   $keyword_link = $row['keyword_link'];

   if (isset($keyword_link) && $keyword_link != '')
   {
     echo "<div class=\"headerbox\">Football News - $keyword_link</div><br />";
    } else {
    echo "<div class=\"headerbox\">Football News - $search</div><br />";
   }
}
 
View user's profile Send private message Visit poster's website
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Mon Dec 18, 2006 1:12 pm Reply with quote

I think you might need another right parenthesis around keyword_link in the isset and then another right one to set off the whole isset test.

Something like:

if ((isset($keyword_link)) && ($keyword_link != ''))

might work. If keywork_link can't ever be null in the table then you might not even need to test for it not being equal to null. Or, with the caveat that I don't know you application, but how about just echoing out the "Football news", search and keyword link for each record. That way you'd know where there is a missing link.
 
View user's profile Send private message Visit poster's website
hinksta







PostPosted: Mon Dec 18, 2006 2:01 pm Reply with quote

Unfortunately no change, the second else does print and will work if I break the first.
 
fkelly







PostPosted: Mon Dec 18, 2006 3:45 pm Reply with quote

I'm not sure I quite follow you.

You need to KNOW what the value of keyword_link is. To eliminate the need to fool with parentheses you could just test:

if (isset($keyword_link))

note that you need two parentheses (or is it parenthes?) just for this ... one for the if condition and one for the isset function. Then echo it out together with the search term. Then maybe you could have an extra if that test for the value of null. You SHOULD be able to combine the two tests like you did or like I suggested but when things get flaky the best way to diagnose what's going on is to break it down into separate tests.
 
gregexp
The Mouse Is Extension Of Arm



Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol

PostPosted: Mon Dec 18, 2006 6:47 pm Reply with quote

Try this:

if (isset($keyword_link) AND ($keyword_link != ''))
{
echo "<div class=\"headerbox\">Football News - $keyword_link</div><br />";
} else {
echo "<div class=\"headerbox\">Football News - $search</div><br />";
}
}
It should work, as This is how my tests and if statements are always setup.
I put the entire cond in paranthesis, and seperate every sub-cond with surrounding paranthesus as well.

Using AND is something that just helps me when Im thinking about code. && will work just fine.

_________________
For those who stand shall NEVER fall and those who fall shall RISE once more!! 
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
hinksta







PostPosted: Mon Dec 18, 2006 7:15 pm Reply with quote

Still no joy, I think I may have the if all wrong.
What i'm asking is:
if keyword = $search
print $keyword_link
else
print $search
 
fkelly







PostPosted: Mon Dec 18, 2006 8:39 pm Reply with quote

Been a long day, but looking at that SQL, the way you have the quotes doesn't look good to me. Especially at the end it looks like you have $search in single quotes, which means it won't be interpreted.

To test all this you could check numrows and see how many are being returned. You could also just echo out the records in the while loop without doing any tests and see exactly what values, if any are being returned. Then I think, you might need to do something about that SQL.
 
hinksta







PostPosted: Tue Dec 19, 2006 3:55 am Reply with quote

if I echo both in the while loop I get 1 return on both only if search = keyword (I get keyword_link on the first echo and keyword or $search on the second)
nothing returns if $search does not = keyword.
if I echo $search outside the while loop it returns $search
 
hinksta







PostPosted: Tue Dec 19, 2006 4:31 am Reply with quote

Got it with both outside the while loop
Code:
$result = $db->sql_query("SELECT keyword,keyword_link FROM ".$prefix."_mysearch WHERE keyword = '$search'"); 

while ($row = $db->sql_fetchrow($result))
{
   $keyword_link = $row['keyword_link']; }
if ($keyword_link)
{
echo "<div class=\"headerbox\">Football News - $keyword_link</div><br />";
}
else{
echo "<div class=\"headerbox\">Football News - $search</div><br />";
}
 
fkelly







PostPosted: Tue Dec 19, 2006 7:58 am Reply with quote

I don't know. It looks to me that with that latest code, if you had say, 10 records you'd run thru them all in the while loop and $keyword_link would be set equal to the last record when the while loop terminates. Then you'd do the if test once and if $keyword link equals 1 (a kind of implicit switch) you echo $keyword_link and if not you'd echo $search. No reason that shouldn't work inside the while loop. I also noticed a subtle little change in your quotes in the SQL. If you want to see all your records you'd have to make the tests run inside the loop.

What are you setting keyword_link to? A "1" or a url or "true" or what?
 
hinksta







PostPosted: Tue Dec 19, 2006 9:26 am Reply with quote

Search is any search term
keyword is a list of possible search terms
keyword_link is a set of replacement words for keyword
All that will be printed is the replacement words if any or the original search words if not
 
fkelly







PostPosted: Tue Dec 19, 2006 11:24 am Reply with quote

Is this resolved? I wouldn't think it is but you are the judge. Smile

If not, why not eliminate the SQL from the equation and just run some tests with manual variable assignments so you absolutely know what the variables are going into the logical conditions and can test your logic that way?

Code:
$keyword_link = 'abc';

          $search = 'def';
   if (isset($keyword_link) && $keyword_link != '')
   {
     echo "<div class=\"headerbox\">Football News - $keyword_link</div><br />";
    } else {
    echo "<div class=\"headerbox\">Football News - $search</div><br />";
   }


Then try it with the parentheses changes that Darklord and I suggested which were along the same lines. Then try it with the assignment of $keyword_link commented out so it's not set. Then try it with $keyword_link assigned a '' value. Note what happens in each case and when you have the logic right stick it back in the while loop. Even there echo out the values as you encounter them, something like 'echo $row['keyword_link']' before you assign it to the variable.
 
Gremmie
Former Moderator in Good Standing



Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA

PostPosted: Tue Dec 19, 2006 1:00 pm Reply with quote

Your if statement is fine in terms of parenthesis and operator precedence. It's probably your surrounding code as fkelly is suggesting.

One caveat on && versus AND. I got bit once because the precedence levels for these are NOT the same. I was using AND inside of an ? : and it did not work as I expected. So I switched back to using && and || instead of AND and OR...I am a C++ guy at heart and PHP's && and || works the same as in C++.
[ Only registered users can see links on this board! Get registered or login! ]

_________________
GCalendar - An Event Calendar for PHP-Nuke
Member_Map - A Google Maps Nuke Module 
View user's profile Send private message
hinksta







PostPosted: Tue Dec 19, 2006 8:35 pm Reply with quote

I'm a bit lost, the else works outside the loop but if I put it in the loop, the loop doesn't see $search
 
hinksta







PostPosted: Wed Dec 20, 2006 8:38 am Reply with quote

I just cannot get $search to be seen inside the IF when in the loop.
If I make a second IF as below it seems to work ok,
Any good?
Code:
echo "<div class=\"headerbox\">Football News - ";

$result = $db->sql_query('SELECT keyword,keyword_link FROM '.$prefix."_mysearch WHERE keyword LIKE '$search'");
while ($row = $db->sql_fetchrow($result))
{
   $keyword_link = $row['keyword_link'];
   if (isset($keyword_link) && (($keyword_link) != ''))
{
echo "$keyword_link";
}
}
if (isset($keyword_link) == '')
{
echo "$search";
}
echo "</div><br />";
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Thu Dec 21, 2006 8:53 am Reply with quote

Ok, let us start over with the original code attempt. You need to make sure $search is even valued before the SQL call. What do you get when you run this:

Code:
echo "Search Outside Loop = $search<br />";

$result = $db->sql_query('SELECT keyword,keyword_link,club FROM '.$prefix."_mysearch WHERE keyword = '$search'");
while ($row = $db->sql_fetchrow($result))
{
   echo "Search Inside Loop = $search<br />";
   $keyword_link = $row['keyword_link'];

   if ($keyword_link != '')
   {
      echo "<div class=\"headerbox\">Football News - $keyword_link</div><br />";
   } else {
      echo "<div class=\"headerbox\">Football News - $search</div><br />";
   }
}


Make sure you replace your code with what I have above and is the value of $search outside the initial loop what you expect it to be?

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
hinksta







PostPosted: Thu Dec 21, 2006 9:06 am Reply with quote

With that code I get the expected return from $search in and out of the loop but not after the else.

Correction:
If $search is in keyword I get the above. If $search is not in keyword I only get search outside the loop
 
montego







PostPosted: Thu Dec 21, 2006 9:25 am Reply with quote

That must simply be because your data never has an empty value for keyword_link field for the given criteria so the ELSE is never reached.
 
hinksta







PostPosted: Sat Dec 23, 2006 8:52 am Reply with quote

Is there a way i can tell it that it's empty if it cant find a match?
or should I try anothert way?
 
montego







PostPosted: Sun Dec 24, 2006 9:23 am Reply with quote

do the query in phpMyAdmin and see what that field has in it...
 
hinksta







PostPosted: Sun Dec 24, 2006 10:19 am Reply with quote

If the search word is present phpMyAdmin shows that row. If not it shows; MySQL returned an empty result set (i.e. zero rows). (Query took 0.0018 sec)
 
fkelly







PostPosted: Sun Dec 24, 2006 12:11 pm Reply with quote

Hinksta, we'd really like to help you get this resolved but this thread is kind of hard to follow. Just generally speaking, the echo statement is your best friend debugging this kind of problem. Echo the values of everything out ... what's in $row['search'] and row['keyword_link'] and what's in the values of the variables that you stuff those field contents in. If you have an if ... else construction, this stick in an echo right after the brace after the if and say something like "if that tests x is satisfied and my values are y and z" and then after the brace after the else put in a similar echo if you drop thru to that.

Also, like Montego said, you can use Phpmyadmin and one thing to do is to keep a phpmyadmin window open and browse the table and watch the values while you execute your program in the other window. That way you can more or less trace thru what your program is doing. Sometimes enlightenment comes that way.
 
Gremmie







PostPosted: Mon Dec 25, 2006 3:32 pm Reply with quote

Ah okay, I think maybe I see the problem.

You need to test how many rows you got before you enter the while loop. If you got 0 rows, your search failed. Otherwise you can do your found logic. Maybe something like this:

Code:


$result = $db->sql_query('SELECT keyword,keyword_link,club FROM '.$prefix."_mysearch WHERE keyword = '$search'");

if ($db->sql_numrows($result) > 0)
{
   // do your while loop or whatever your "found" logic is supposed to do
}
else
{
   // do your "not found" logic here
}
 
montego







PostPosted: Tue Dec 26, 2006 1:47 pm Reply with quote

Thanks guys! Good input.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©