For loop problem

$find =
Array ( [0] => 5 [1] => 55 [2] => 105 [3] => 155 )

Every time $i==$find[$j] it should give:

"<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"".$style[$i]."\\">".$english_text[$i]."</span></td>\
";

otherwise this:

echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"\\">".$english_text[$i]."</span></td>\
";

The problem is it’s printing $i twice when $j is included:

<td id="td_1" style="border: 1px solid black; font-size: 14px;"><span id="span_1" style="">b</span></td>
<td id="td_2" style="border: 1px solid black; font-size: 14px;"><span id="span_2" style="">r</span></td>
<td id="td_3" style="border: 1px solid black; font-size: 14px;"><span id="span_3" style="">a</span></td>
<td id="td_4" style="border: 1px solid black; font-size: 14px;"><span id="span_4" style="">f</span></td>
<td id="td_5" style="border: 1px solid black; font-size: 14px;"><span id="span_5" style="">i</span></td>
<td id="td_6" style="border: 1px solid black; font-size: 14px;"><span id="span_6" style="display: block; font-weight: bold; color: sienna; ">t</span></td>
<td id="td_6" style="border: 1px solid black; font-size: 14px;"><span id="span_6" style="">t</span></td>
<td id="td_7" style="border: 1px solid black; font-size: 14px;"><span id="span_7" style="">b</span></td>

print_r($find);
for($i=0; $i<strlen($english_text); $i++){
	$num=$i+1;
	for($j=0; $j<count($find); $j++){
		if($i==$find[$j]){
			echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"".$style[$i]."\\">".$english_text[$i]."</span></td>\
";
		}else{
			continue;
		}
	}
	echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"\\">".$english_text[$i]."</span></td>\
";
	if($num % $skiprow==0 && $i>0){
		echo "\
</tr>\
<tr>\
";
	}
}

It looks like the problem is your code will print the $i line regardless of whether or not it finds a $j. We just need to add code to tell it not to print $i style whenever it finds a $j:

I added in some dummy information so that I could test the change.


<?php

$find[0] = 5;
$find[1] = 55;
$find[2] = 105;
$find[3] = 155;

$style[0] = "style0";
$style[1] = "style1";
$style[2] = "style2";
$style[3] = "style3";
$style[4] = "style4";
$style[5] = "style5";
$style[6] = "style6";

$skiprow = 100;

$english_text = "brafitb";

print_r($find);

for($i=0; $i<strlen($english_text); $i++){

    $num=$i+1;
    $jecho = false;
    
    for($j=0; $j<count($find); $j++){
        if($i==$find[$j]){
            echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"".$style[$i]."\\">".$english_text[$i]."</span></td>\
";
            $jecho = true;
        }else{
            continue;
        }
    }

    if (!$jecho){
        echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"\\">".$english_text[$i]."</span></td>\
";
    }

    if($num % $skiprow==0 && $i>0){
        echo "\
</tr>\
<tr>\
";
    }

} 
?>

I see no reason for the following code:


        }else{

            continue;

        }


The 2nd line will be printed each iteration of the outer loop, because there’s no condition before it.
Use some flag to be set to true when $i=$find[$j]. Initialize it at the beginning of each iteration of the outer loop, and check that it’s true before printing that second line:



print_r($find);

for($i=0; $i<strlen($english_text); $i++){
[COLOR="Red"]    $found=false;[/COLOR]
    $num=$i+1;

    for($j=0; $j<count($find); $j++){

        if($i==$find[$j]){

            echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"".$style[$i]."\\">".$english_text[$i]."</span></td>\
";
[COLOR="Red"]            $found=true;[/COLOR]
        }
        //}else{

        //    continue;

        //}

    }
[COLOR="Red"]    if (!$found)[/COLOR]
      echo "<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;\\"><span id=\\"span_".$num."\\" style=\\"\\">".$english_text[$i]."</span></td>\
";

    if($num % $skiprow==0 && $i>0){

        echo "\
</tr>\
<tr>\
";

    }

} 



$find = array(
  'a',
  'b',
  'e',
  'f',
  'i'
);

$string = implode('', range('a', 'j'));

foreach(str_split($string) as $index => $value){
  printf(
    '<td id="td_%d" style="border: 1px solid black; font-size: 14px;">
      <span id="span_%d" class="%s">%s</span>
    </td>' . PHP_EOL,
    $index,
    $index,
    in_array($value, $find) ? 'found' : null,
    $value
  );
}


/*
  <td id="td_0" style="border: 1px solid black; font-size: 14px;">
    <span id="span_0" class="found">a</span>
  </td>
  <td id="td_1" style="border: 1px solid black; font-size: 14px;">
    <span id="span_1" class="found">b</span>
  </td>
  <td id="td_2" style="border: 1px solid black; font-size: 14px;">
    <span id="span_2" class="">c</span>
  </td>
  <td id="td_3" style="border: 1px solid black; font-size: 14px;">
    <span id="span_3" class="">d</span>
  </td>
  <td id="td_4" style="border: 1px solid black; font-size: 14px;">
    <span id="span_4" class="found">e</span>
  </td>
  <td id="td_5" style="border: 1px solid black; font-size: 14px;">
    <span id="span_5" class="found">f</span>
  </td>
  <td id="td_6" style="border: 1px solid black; font-size: 14px;">
    <span id="span_6" class="">g</span>
  </td>
  <td id="td_7" style="border: 1px solid black; font-size: 14px;">
    <span id="span_7" class="">h</span>
  </td>
  <td id="td_8" style="border: 1px solid black; font-size: 14px;">
    <span id="span_8" class="found">i</span>
  </td>
  <td id="td_9" style="border: 1px solid black; font-size: 14px;">
    <span id="span_9" class="">j</span>
  </td>
*/