I have the following code: $model = $row['v_products_model']; $smallid = substr($model, 0, 4); There are 123 values for $smallid. I need to give a value to the variable $mpg based on the value of $smallid, like this: If $smallid = '5456' then $mpg = 'Mouse' If $smallid = '5434' then $mpg = 'Speakers' If $smallid = '5432' then $mpg = 'Cable' If $smallid = '5431' then $mpg = 'Adapters' etc, etc. I need someone help me in building the code. I'm not sure if switch is the answer or may be another implementation. Please help. I'm in a hurry with this. Thanks
You could use an array pretty easily. $mpg = array( 5456 => 'Mouse', 5434 => 'Speakers', 5432 => 'Cable', 5431 => 'Adapters' ); echo $mpg[$smallid]; PHP: You would need to create the array, but shouldn't be too difficult.
As you said there are 123 values for $smallid. I hope these values are fixed. Create one associative array for all 123 values like following: $mpqArr = array( 5456 => 'Mouse', 5434 => 'Speakers', 5432 => 'Cable', 5431 => 'Adapters' put all reamining also here as similar way ); $model = $row['v_products_model']; $smallid = substr($model, 0, 4); $mpg = $mpgArr[$smallid]; $mpg will have the exact value which you wanted. I hope you will able to write code now. Sheetal
Hi, switch( $task ) { case "5456": Mouse.php break; case "5434": Speakers.php break; case "5432": Cable.php break; case "5431": Adapters.php break; } Vishal
Hi, switch( $smallid ) { case "5456": Mouse.php break; case "5434": Speakers.php break; case "5432": Cable.php break; case "5431": Adapters.php break; } Vishal