PHPNewbie55 Posted March 20, 2009 Share Posted March 20, 2009 I cannot figure out why the ELSE in this code will not work... I thought ELSE was default but in this case the else { print "<b>No Match Found For:</b> ".$tempsku.""; } will not work no matter what I try... The if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } works just fine so I thought that the else was default "if" the isset was NOT set... and I have tried printing the $intable but it is empty... if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } Here is the rest of that code..... $Mquery = mysql_query("SELECT * FROM products WHERE id!='' $sequery limit 1"); while($getproductlink = mysql_fetch_array($Mquery)) { $intable = "".$getproductlink['sku'].""; if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } mysql_query("UPDATE temp SET msku='".$getproductlink['sku']."' category='".$getproductlink['category']."' subcategory='".$getproductlink['subcategory']."' productgroup='".$getproductlink['productgroup']."' WHERE sku='".$tempsku."'"); $chk = mysql_fetch_array(mysql_query("SELECT * FROM skumatch WHERE temp_sku='".$tempsku."'")); $old = $chk['temp_sku']; if(!$old) { mysql_query("INSERT INTO skumatch (`main_sku`, `temp_sku`) VALUES ('".$intable."', '".$tempsku."')"); } } I have tried just about everything I can think of... what ELSE should I do..?? LOL Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/ Share on other sites More sharing options...
Dan_Mason Posted March 20, 2009 Share Posted March 20, 2009 Maybe try this? <?php if (isset($intable)): print "".$intable." - <b>Matched To</b> - ".$tempsku.""; else: print "<b>No Match Found For:</b> ".$tempsku.""; ?> Im not 100% sure but i dont think you needed the Parenthesis around The Print statement. Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789202 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 That doesn't work it doesn't print anything... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789215 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 <?php if (isset($intable))? print "".$intable." - <b>Matched To</b> - ".$tempsku.""; else: print "<b>No Match Found For:</b> ".$tempsku.""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789217 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 correct way cheeked. <?php $intable="redarrow"; $tempsku="php"; if (isset($intable)){echo" $intable - <b>Matched To</b> - $tempsku";}else{ echo "<b>No Match Found For:</b> $tempsku";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789223 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 That doesn't work either.... It's weird.. it should either do the ISSET thing ALL the time or NEVER Here is the entire code... maybe it is something else that is effecting it.. $sequery = ""; $query = $q; $tempsku = $t; $kws = preg_split("/\s+/", addslashes(strip_tags($query))); $kws2 = addslashes(strip_tags($query)); while (list(,$kw) = each($kws)) { $kwchc = strlen($kw); if ($kw) { $valid = 1; $sequery .= " and (name like '%$kw%' or sku like '%$kw%')"; } } $Mquery = mysql_query("SELECT * FROM products WHERE id!='' $sequery limit 1"); while($getproductlink = mysql_fetch_array($Mquery)) { $intable = "".$getproductlink['sku'].""; // the else...... if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } ////////// mysql_query("UPDATE temp SET msku='".$getproductlink['sku']."' category='".$getproductlink['category']."' subcategory='".$getproductlink['subcategory']."' productgroup='".$getproductlink['productgroup']."' WHERE sku='".$tempsku."'"); $chk = mysql_fetch_array(mysql_query("SELECT * FROM skumatch WHERE temp_sku='".$tempsku."'")); $old = $chk['temp_sku']; if(!$old) { mysql_query("INSERT INTO skumatch (`main_sku`, `temp_sku`) VALUES ('".$intable."', '".$tempsku."')"); } } Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789235 Share on other sites More sharing options...
kickstart Posted March 20, 2009 Share Posted March 20, 2009 Hi You have assigned a value (and whatever $getproductlink['sku'] is you aer assigning a pair of blank strings to it). Do you want to check for empty instead? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789258 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 Yes basically I am trying to check if $intable = "".$getproductlink['sku'].""; is empty... I have tried: if (isset($intable if (!isset($intable if ($intable = "" if ($intable == "" if ($intable if (!$intable And nothing works.... it's weird... or maybe I just don't understand completely.. LOL Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789261 Share on other sites More sharing options...
MasterACE14 Posted March 20, 2009 Share Posted March 20, 2009 try... <?php if(strlen(isset(!empty($intable))) > 0) { echo "Not Empty!"; } Regards, ACE Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789263 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 MasterACE14 - Nope that doesn't work either... kickstart - The value (and whatever $getproductlink['sku'] has to be there what that does is break down the name into individual words and searches the database for the individual words in the string instead of the entire string.. this return more relevant results.. Those values will never be blank... It is the results of the query I am checking to see if it is empty or not... $intable = "".$getproductlink['sku'].""; ----------- It seems that if the $intable is empty it just dies... so it wouldn't continue on in order to ever reach the if(isset($intable)) But I can't see why... I search the database and if there are no results then the $intable should be blank... I did put a print "".$getproductlink['sku'].""; right ABOVE the $intable = "".$getproductlink['sku'].""; so it is EMPTY... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789278 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 No it doesn't die... I just put some stuff at the top and bottom and that prints no matter what even after the queries so it isn't dieing.. It just won't do anything if the $intable is empty.... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789283 Share on other sites More sharing options...
Kalland Posted March 20, 2009 Share Posted March 20, 2009 Hi You have assigned a value (and whatever $getproductlink['sku'] is you aer assigning a pair of blank strings to it). Do you want to check for empty instead? All the best Keith I think you should check whether or not it's empty like this as Keith suggested: if (empty($intable)) {} else {} Since you have this line: <?php $intable = "".$getproductlink['sku'].""; ?> if(isset($intable)) will always be true... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789292 Share on other sites More sharing options...
MasterACE14 Posted March 20, 2009 Share Posted March 20, 2009 add error_reporting(E_ALL); to top of page, see if anything turns up then. Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789296 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 I already have: ini_set ("display_errors", "1"); error_reporting(E_ALL); at the top and there are no errors... the page is just blank if the $intable is empty... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789299 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 There are two different queries... The first Which takes the $query and breaks it down into individual words... and creates the $sequery $sequery = ""; $query = $q; $tempsku = $t; $kws = preg_split("/\s+/", addslashes(strip_tags($query))); $kws2 = addslashes(strip_tags($query)); while (list(,$kw) = each($kws)) { $kwchc = strlen($kw); if ($kw) { $valid = 1; $sequery .= " and (name like '%$kw%' or sku like '%$kw%')"; } } That query only creates the $sequery below... There is nothing wrong with that part... The queries is created regardless because the query wouldn't be created if there wasn't a record for the query to get data from... Then there is the second query which uses the $sequery from above.... $Mquery = mysql_query("SELECT * FROM products WHERE id!='' $sequery limit 1"); while($getproductlink = mysql_fetch_array($Mquery)) { $intable = "".$getproductlink['sku'].""; // the else...... if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } ////////// mysql_query("UPDATE temp SET msku='".$getproductlink['sku']."' category='".$getproductlink['category']."' subcategory='".$getproductlink['subcategory']."' productgroup='".$getproductlink['productgroup']."' WHERE sku='".$tempsku."'"); $chk = mysql_fetch_array(mysql_query("SELECT * FROM skumatch WHERE temp_sku='".$tempsku."'")); $old = $chk['temp_sku']; if(!$old) { mysql_query("INSERT INTO skumatch (`main_sku`, `temp_sku`) VALUES ('".$intable."', '".$tempsku."')"); } } Now if there are results the $intable will contain data... If there are no results then $intable will be empty... So I just don't get why the if(isset($intable)) won't work or any of the other examples I have tried.. if (empty($intable if (isset($intable if (!isset($intable if ($intable = "" if ($intable == "" if ($intable if (!$intable and all I get is a blank page if it is empty... Keep in mind that I have print "<html><head><title>Process</title></head><body>"; at the top and print "</body></html>"; at the bottom That prints no matter what... so it isn't dieing... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789312 Share on other sites More sharing options...
kickstart Posted March 20, 2009 Share Posted March 20, 2009 Hi isset will return true if the variable is set. Even if you have just assigned "" to it then you have set it so it will return true. As such the code you have at the moment will always put out the "matched" message as $intable has been set. If $getproductlink['sku'] contains blanks (rather than being empty) then empty will not work either, nor will checking it for being "". Try changing a few lines as follows:- $intable = trim($getproductlink['sku']); // the else...... if(empty($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789317 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 WOW that doesn't work either.... I am stumped.. two pots of coffee and about 6 hours later... LOL I mean it works.. because if it is empty then it isn't supposed to really do anything any way... I am just puzzled why the else doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789327 Share on other sites More sharing options...
kickstart Posted March 20, 2009 Share Posted March 20, 2009 Hi Does it put out anything at all? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789330 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 I think it time to drink that coffee then, echo out all the variables in question right throw the whole code as this is becoming a hard task. var_dump the variables in question and post the results. example. <?php $word="redarrow"; echo var_dump($word); ?> string( "redarrow" Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789333 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 It puts out the print "html"; at the top and bottom... Here is the entire page... ini_set ("display_errors", "1"); error_reporting(E_ALL); require("settings.php"); $conn = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword"); if (!$conn) { die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error()); } mysql_select_db("$mysqldb", $conn); $sequery = ""; $query = $q; $tempsku = $t; print "<html><head><title>Match SKU: ".$tempsku." - ".$query."</title></head><body>"; $kws = preg_split("/\s+/", addslashes(strip_tags($query))); $kws2 = addslashes(strip_tags($query)); while (list(,$kw) = each($kws)) { $kwchc = strlen($kw); if ($kw) { $valid = 1; $sequery .= " and (name like '%$kw%' or sku like '%$kw%')"; } } $Mquery = mysql_query("SELECT * FROM products WHERE id!='' $sequery limit 1"); while($getproductlink = mysql_fetch_array($Mquery)) { $intable = "".$getproductlink['sku'].""; if(isset($intable)) { print "".$intable." - <b>Matched To</b> - ".$tempsku.""; } else { print "<b>No Match Found For:</b> ".$tempsku.""; } mysql_query("UPDATE temp SET msku='".$getproductlink['sku']."' category='".$getproductlink['category']."' subcategory='".$getproductlink['subcategory']."' productgroup='".$getproductlink['productgroup']."' WHERE sku='".$tempsku."'"); $chk = mysql_fetch_array(mysql_query("SELECT * FROM skumatch WHERE temp_sku='".$tempsku."'")); $old = $chk['temp_sku']; if(!$old) { mysql_query("INSERT INTO skumatch (`main_sku`, `temp_sku`) VALUES ('".$intable."', '".$tempsku."')"); } } print "</body></html>" I would just scrap the whole thing and start over.. but I would probably end up with something similar... and I have got to figure out why this else just refuses to work... I have used this many times in the past and it hasn't failed me... It isn't logical because it is an IF ELSE type of thing... and it does the IF part fine... just not the ELSE... LOL Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789335 Share on other sites More sharing options...
kickstart Posted March 20, 2009 Share Posted March 20, 2009 Hi In that case it would seem that the SELECT hasn't found a matching record. echo out the SQL and try it manually in phpmyadmin or something like that. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789341 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 I tried: echo var_dump($intable); and it doesn't echo anything if the $intable is empty... That is what I am talking about it is empty so the ELSE should work.. I have tried just about everything and this particular else won't work... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789345 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 Kickstart - exactly the SELECT didn't find anything so the $intable is empty and the else should print... But it doesn't... no errors, no dieing, no nothing... That script isn't huge and it's not that complicated.. but that else just refuses to work.. LOL... Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789352 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 what about using the good old mysql_num_rows() way Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789355 Share on other sites More sharing options...
PHPNewbie55 Posted March 20, 2009 Author Share Posted March 20, 2009 OK... I just got some different results... I put a $intable = ""; at the top Now the else works but it doesn't print what it should... All that shows up is the html at the top and bottom and string(0) "" That prints where the else { print "<b>No Match Found For:</b> ".$tempsku.""; } should print... Any ideas..?? Quote Link to comment https://forums.phpfreaks.com/topic/150281-solved-else-not-working/#findComment-789359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.