You say the code doesnt give the correct results - what results are you expecting?
As you have typed it, there is a syntax error, you should have put:
if (is_int($dataval)) {
with two closing brackets.
Also, the statements where you use ".=", will add $dataval to the end of the current contents of $valSQL. Is this what you want?
Otherwise, I have tried a similar example, and it gives what I think is the right result ... but its difficult to know quite what your problem is!