i have problem about while loop. this my code,

error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); 

function multiexplode ($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

function my_replace($srch, $replace, $subject, $skip=1){
    $subject = explode($srch, $subject.' ', $skip+1);
    $subject[$skip] = str_replace($srch, $replace, $subject[$skip]);
    while (($tmp = array_pop($subject)) == '')
        ;

    $subject[]=$tmp;
    return implode($srch, $subject);
}

function replace_element($str,$search,$replace,$num) {
    $num = $num - 1;
    $pieces = explode(',',$str);
    if($pieces[$num] == $search) {
        $pieces[$num] = $replace;
    }
    return implode(',',$pieces);
} 

function returnFormat($template, $subject){
    preg_match_all("/\|/", $template, $matches, PREG_OFFSET_CAPTURE);
    foreach($matches[0] as $v) 
        $subject = substr_replace($subject, "|", $v[1], 1);

    return $subject;
}   

$replace = "|"; $pattern = "\|";

function combine_arrays( $array1, $array2 )
{
    $size = max( count($array1), count($array2) );
    $data = array();
    for ($i=0; $i < $size; $i++) { 
        $thisVal = ( $array1[$i] == 'JJJJ' OR $array2[$i] == 'JJJJ' ) ? 'JJJJ' : $array1[$i];
        $data[] = $thisVal;
    }       
    return $data;
}   

$s=mysql_query("select * from tab"); 
while ($j=mysql_fetch_array($s)) 
{ 
    $string=$j[1]; 
    $data=$j[2]; 
    $char = '3';
    $string = str_replace('|', '', $string);    
    $positions = array();
    $pos = -1; 
    $b=0; 
    while (($pos = strpos($string, $char, $pos+1)) !== false) 
    {
        {
            $positions[] = $pos+1; 
        }

        $result2 = implode(',', $positions);
        $res = explode(',',$result2); 
        $exp = multiexplode(array(",","|"),$data);
        $eyz= my_replace('|', ',', $data, 0);       
        $z=1; 
        $y=1;
        foreach ($res as $val) {
            $res2[$z]=$val; 
            $valz = $res2[$z];
            $z++; 
        }
        foreach ($exp as $value) {
            $exp2[$y]=$value; 
            $vals = $exp2[$valz];
            $y++; 
        }     
        $items = explode(',',$eyz); 
        $occurences = array_count_values($items);   
        $st=$occurences[$vals];
        $fix=replace_element($eyz,$vals,'JJJJ',$valz);
        preg_match_all("/$pattern/", $data, $matches, PREG_OFFSET_CAPTURE);
        foreach($matches[0] as $v) 
            $fix = substr_replace($fix, $replace, $v[1], 1);

        $arrayToCombine[] = multiexplode([","], $fix);
        $b++;
    }
    echo(implode(",",combine_arrays( $arrayToCombine[0], $arrayToCombine[1] )));
} //end while

i have a table, the name is tab, i have, 3 field, ID, STRING, DATA, and 2 content.

ID = 1
string = 101131110|101113110| 
data = 1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|
ID = 2
string = 101111310|101111310| 
data = 1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|

this script to change the order in the data field into JJJJ to the position in the string field that has the number 3, for ID 1 i get result : this is true

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024‌,1025,0000|

Now, i have problem about while loop.
The following are two examples of the output of the data
for ID 1 (true)

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024‌,1025,0000|

and
for ID 2 (false)

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024,‌1025,0000|

if only one field content, e.g only ID 1, this script works well, but if there are two or more content at field and if i use while loop like as my code the results echo: the second data, and others, to be the same as the first like the example above.

how to fix, can you tell me how I can solve this problem while loop.

the correct result should be a
for ID 1 (true)

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024‌,1025,0000|

and
for ID 2 (true)

1020,0000,2022,1023,1024,1025,JJJJ,1025,0000|1020,0000,2022,1023,1024,1025,JJJJ,1025,0000|

thanks in advance.

regards,

Recommended Answers

All 4 Replies

What exactly are you trying to achive? It's not clear to me. Your code however indicates that you must be overcomplicating things. Can you explain with a simpler example/flow of what the input is and what the output is?

Okay I will explain in more detail, I have a table : 'tab' which has fields and contents like this:

ID = 1
string = 101131110|101113110|
data = 1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|
ID = 2
string = 101111310|101111310|
data = 1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|

for ID 1 you can see the number 3 position : 101131110|101113110|
the next field, I change with JJJJ the position where the sequence position number 3 above, you notice that my bold
1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|

for try, delete line 93, and change line 49-53 with :
$string = "101131110|101113110|";
$data = "1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|";

you will get this output :

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024‌,1025,0000|

my syntax works well for 1 record, and the output results are correct.

problem when i would change a lot of other records, using the script at the beginning of the post, take from the database and create a loop with a while loop. with two examples : the correct result should be a

for ID 1 (true)

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024‌,1025,0000|

and
for ID 2 (true)

1020,0000,2022,1023,1024,1025,JJJJ,1025,0000|1020,0000,2022,1023,1024,1025,JJJJ,1025,0000|
Member Avatar for diafol

Your code seems very verbose for what you're trying to do.

<?php
//array to resemble data from DB
$records = array(
    array(  
        'id' => 1,
        'string' => '101131110|101113110|',
        'data' => '1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|'
    ),
    array(  
        'id' => 2,
        'string' => '101111310|101111310|',
        'data' => '1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|'
    )
);

function convertArray($records,$find='3',$replace='JJJJ')
{
    $output = array();
    foreach($records as $record){
        $str = str_replace("|","",$record['string']); //strip | from string for getting positions
        $pos = preg_match_all("/$find/",$str,$matches,PREG_OFFSET_CAPTURE); //store positions in matches[0][][1]
        $parts = preg_split('/(\,|\|)/', $record['data'], -1, PREG_SPLIT_DELIM_CAPTURE); //just split into values and delims
        foreach($matches[0] as $p) $parts[$p[1]*2] = $replace; //replace items (= positions) with JJJJ
        $output[] = implode($parts); //join the array
    }
    return $output;
}

$data = convertArray($records);

echo "<pre>";
print_r($data);
echo "</pre>";

$data = convertArray($records,1,'PIPPED');

echo "<pre>";
print_r($data);
echo "</pre>";
?>

I'd do something like that.

@diafol,
thank you, you really helped me, the problem is solved, I'll use that you write. thanks.

regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.