Janey
Janey

Reputation: 1310

PHP fill array every nth row

I'm trying to create an array that holds a marker on every 10th row, every 50th row and every 100th row. But my current code I've shown below is just marking every row after "count" = 10 as 10. Can anyone see what I'm doing wrong?

<?php

function randomString($length = 15) {
$rs = "";
$char = array_merge(range('A','Z'), range('a','z'), range('0','9'));
$max = count($char) - 1;
for ($i = 0; $i < $length; $i++) {
    $rand = mt_rand(0, $max);
    $rs .= $char[$rand];
}
return $rs;

}

$i = 0;
$times_to_run = 100;
$prize='';
$counter = 0;
// $array = array();
while ($i++ < $times_to_run)
{
    $counter++;

    if ($counter % 10 == 0) {
        $prize='10';
    }
    elseif ($counter % 50 == 0) {
        $prize='50';
    }
    elseif ($counter % 100 == 0) {
        $prize='100';
    } else {$prize=='';}


    $array[] = array(
        'count' => $i,
        'code1' => randomString(),
        'prize' => $prize
    );
}

// var_dump($array);

echo"<table>";
$codes = $array;
foreach ($codes as $code){
    echo'<tr>';
    echo'<td>'. $code['count']."</td>";
    echo'<td>'. $code['code1']."</td>";
    echo'<td>'. $code['prize']."</td>";
    echo'<tr>';
}
echo"<table>";

?>

so.. when "count" is 10, "prize" should be 10. Prize should then be empty until "count" = 20 and so on until "count" reaches 50 - "prize" should show 50. At the moment it is just doing what is shown in the image:

enter image description here

Upvotes: 0

Views: 266

Answers (3)

Sadikhasan
Sadikhasan

Reputation: 18600

If I understand correctly then you want to this

$prize= "";
if ($counter % 10 == 0) 
{
  $prize= floor($counter/10) * 10;
}

  $array[] = array(
    'count' => $i,
    'code1' => randomString(),
    'prize' => $prize
   );

OR

If you want to 10, 50, 100 then

for ($i=0;$i< $times_to_run;$i++)
{
   $prize="";
    if($i == 10 || $i == 50 || $i == 100) {
      $prize = $i;
    } 
}

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

The very first check:

if ($counter % 10 == 0) {
    $prize='10';
}

sets the $prize to 10 and nobody then resets it. Once you want the explicit set of rows be marked, mark them:

if ($counter == 10 || $counter == 50 || $counter == 100 ) {
  // set it
} else {
  // RESET IT — IMPORTANT
}

Another glitch with your code is that your first check:

if ($counter % 10 == 0) { ...

is evaluated to true for 50 and 100 as well, forcing second and third branches of if-else switch to become unreachable.

Upvotes: 1

Endacy
Endacy

Reputation: 227

replace your while loop

for($i=1;$i<=$times_to_run;$i++){
            if($i == '10' || $i == '50' || $i == '100'){
                $prize=$i;
            }else{
                $prize='';
            }
            $array[] = array(
                'count' => $i,
                'code1' => randomString(),
                'prize' => $prize
            );
        }

        echo "<pre>";print_r($array);

Upvotes: 1

Related Questions