Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/150281-solved-else-not-working/
Share on other sites

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.

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."')");
}
}

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

 

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...

 

 

 

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...

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...

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

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(8) "redarrow"

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

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...

:)

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..??

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.