We help IT Professionals succeed at work.

How do I fix this strpos statement?

lawrence_dev
lawrence_dev asked
on
How do I fix the STRPOS statement?  What am I doing wrong here?


if ($Brand="PREMIUM POWER" AND (strpos($ModelArray,'Latitude'|'Vostro'|'ChemBook'|'Inspiron'|'JetBook'|'Precision'|'Studio'|'XPS')) !== false) {
    $NewBrand="Dell";
    }  else if ($Brand="PREMIUM POWER" AND (strpos($ModelArray,'ThinkPad')) !== false) {
    $NewBrand="IBM";
	}  else if ($Brand="PREMIUM POWER" AND (strpos($ModelArray,'Presario'|'Pavilion')) !== false) {
    $NewBrand="HP";
    } else {
	$NewBrand=$row5[Manufacturer];
	}

Open in new window

Comment
Watch Question

Marco GasiFreelancer
Top Expert 2010

Commented:
Line 3 must be
else if ($Brand="PREMIUM POWER" AND (strpos($ModelArray,'ThinkPad') !== false)) {

Open in new window

and line 5 must be:
}  else if ($Brand="PREMIUM POWER" AND (strpos($ModelArray,'Presario'|'Pavilion') !== false)) {

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
There's a lot wrong with your code.

Firstly, a single = is an assignment - for compasrison your need a double ==

AND in your if statement should be &&

You can't use a single strpos call for multiple words - you need to loop through them. Create an array and loop through them one by one.

Author

Commented:
Chris,
Please advise how to correctly structure.  Thanks for your help!
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
No worries. can you show me what's in $modelarray. Is it actually an array as the name suggests

Author

Commented:
It is really not an 'array' per se.   It is pipe delimited.  Here it is:

ThinkPad i1700|ThinkPad i1720|ThinkPad i1721 etc

Thanks for your help!!
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Have a look through this. You can probably streamline it, but hopefully you'll get the general idea:

$NewBrand = null;

if ($Brand=="PREMIUM POWER") {
	if (is_null($NewBrand)) {
		$brandList = array('Latitude', 'Vostro','ChemBook','Inspiron','JetBook','Precision','Studio','XPS');
		foreach ($brandList as $brand) {
			if (strpos($ModelArray, $brand) !== false) {
				$NewBrand = "Dell";	
				break;		
			}
		}
	}
	
	if (is_null($NewBrand)) {
		$brandList = array('ThinkPad');
		foreach ($brandList as $brand) {
			if (strpos($ModelArray, $brand) !== false) {
				$NewBrand = "IBM";	
				break;		
			}
		}
	}
		
	if (is_null($NewBrand)) {
		$brandList = array('Presario','Pavilion');
		foreach ($brandList as $brand) {
			if (strpos($ModelArray, $brand) !== false) {
				$NewBrand = "HP";	
				break;		
			}
		}
	}
}	

if (is_null($NewBrand)) {
	$NewBrand = $row5[Manufacturer];
}

Open in new window

Author

Commented:
Thanks Chris!