Link to home
Start Free TrialLog in
Avatar of lawrence_dev
lawrence_dev

asked on

RELATED: How do I fix this error: Warning: strpos(): Empty needle on line 122

I had this question after viewing How do I fix this error:  Warning: strpos(): Empty needle on line 122.

How do I structure issets correctly including the IF/ELSE IF statements?

OLD CODE:  (Without Isset)

if (strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[22]) !==false) {
$Color = $data[23];
} elseif (strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[24]) !==false) { 
$Color = $data[25];
} elseif (strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[26]) !==false) { 
$Color = $data[27];
} elseif (strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[28]) !==false) { 
$Color = $data[29];
} elseif (strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[30]) !==false) { 
$Color = $data[31];
} else {
$Color = "";
}

Open in new window



NEW CODE  (with isset)  

if(isset($data[22])) {
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[22]) !==false)) {
$Color = $data[23];
}
}
if(isset($data[24])) {
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[24]) !==false)) {
$Color = $data[25];
}
}
if(isset($data[26])) {
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[26]) !==false)) {
$Color = $data[27];
}
}
if(isset($data[28])) {
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[28]) !==false)) {
$Color = $data[29];
}
}
if(isset($data[30])) {
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[30]) !==false)) {
$Color = $data[31];
}
}
if ($Color == "") {
$Color == "";
}

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I don't think you can duplicate the IF/ELSE IF structure when you have to test the $data[xx] variables first.  If all of the $data[xx] variables exist you end up with the last match.  You original code depended on the result of the 'strpos' comparison but that was resulting in the error when the data didn't exist.  Your NEW CODE should work.  Is it doing or not doing something you don't want?
In your original code, the conditional would end with the first true condition because of the elseif statements. In your new code, each true condition will overwrite the previous value of $Color and you will end up with the value of the last true condition. I would loop through the list of indices and break out of the loop when the first is found, then do the strpos conditional on the targeted index.
$indices = [22,24,26,28,30];
foreach ($indices as $index) { if (isset($test_array[$index]) ) break; }
if ((strpos('Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color',$data[$index]) !==false)) {
	$Color = $data[$index + 1];
}

Open in new window


I'm not sure what your intended purpose is for the final conditional statement which is equivalent of saying if $Color is empty then true:
if ($Color == "") {
$Color == "";
}

Open in new window

Maybe use else in your conditional statements?
Similar to Kim's idea, this will find the first matching data element.  But something "feels funny" about this approach to an array search.  Maybe if we could see the test data and have a little better narrative about the objective we could suggest a more mainstream design pattern.  With this design, if one of the array elements contained only the letter "C", that single letter is what would be assigned to the $Color variable.  Maybe that's right, but it seems like it's kind of a fringe case.
<?php // demo/temp_lawrence_dev.php
/**
 * https://www.experts-exchange.com/questions/28992213/RELATED-How-do-I-fix-this-error-Warning-strpos-Empty-needle-on-line-122.html#a41942366
 */
error_reporting(E_ALL);

// THE TEST DATA ARRAY IS MISSING FROM THE POST AT E-E
$data = [];

// THE SEARCH STRING
$key = 'Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color';

// THE DEFAULT VALUE OF VARIABLE WE WANT TO SET
$Color = "";

// THE ARRAY INDEXES WE WANT TO TEST
$ndxs = [ 22, 24, 26, 28, 30 ];

foreach ($ndxs as $ndx)
{
    if (!empty($data[$ndx]))
    {
        if (strpos($key, $data[$ndx]) !== FALSE)
        {
            $Color = $data[$ndx];
            break;
        }
    }
}


// SHOW THE WORK PRODUCT
var_dump($Color);

Open in new window

You may can use the PHP IF shorthand, like     (isset($data[22]) ? $data[22] : "")

if you want to see ALL $data[ ] array numbers for inspection and change you may can use -
$Color = "";
$search = 'Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color'.
'|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color';

   if ((strpos($search, (isset($data[22]) ? $data[22] : "")) !==false)) $Color = $data[23];

elseif ((strpos($search, (isset($data[24]) ? $data[24] : "")) !==false))  $Color = $data[25];

elseif ((strpos($search, (isset($data[24]) ? $data[26] : "")) !==false))  $Color = $data[27];

elseif ((strpos($search, (isset($data[24]) ? $data[28] : "")) !==false))  $Color = $data[29];

elseif ((strpos($search, (isset($data[24]) ? $data[30] : "")) !==false))  $Color = $data[31];

Open in new window

Avatar of lawrence_dev
lawrence_dev

ASKER

Slick812, That is a great solution and exactly what I looking for!!  I am still working with it to solve the empty needle issue.  

Warning: strpos(): Empty needle is still an error.

Here is more information regarding what my code looks like.  The download works perfect without the strpos().



$filename="Product_Catalog.txt";


//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle," ","\t"," ");    



//begin looping through the lines
while (($data = fgetcsv($handle, " ","\t"," ")) !== FALSE)
{


// loop all column values and escape special characters
 foreach ($data as $key => $value){
  $data[$key] = $value;
}

$Color = "";
$search = 'Color|Flashlight Color|Color/Finish|LED Color|Blade Color|Handle Color|Lens Color|Handle/Blade Color'.
'|Body Color|Light Color|LED Colors|Frame Color|Buckle Color|Lens Color / Frame Color|Snap Color|Gate Color|Lamp Color';

if ((strpos($search, (isset($data[22]) ? $data[22] : "")) !==false)) $Color = $data[23];

elseif ((strpos($search, (isset($data[24]) ? $data[24] : "")) !==false))  $Color = $data[25];

elseif ((strpos($search, (isset($data[26]) ? $data[26] : "")) !==false))  $Color = $data[27];

elseif ((strpos($search, (isset($data[28]) ? $data[28] : "")) !==false))  $Color = $data[29];

elseif ((strpos($search, (isset($data[29]) ? $data[30] : "")) !==false))  $Color = $data[31];

Open in new window

Any chance we could see the test data and the desired outputs?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SLICK812, you are awesome!  Worked Great!  I did have to remove a )  in "f!f!f")

THANKS!!  HAPPY NEW YEAR!!
Very well done!!
tanks fur pointz larryy, weres de cork screww?