0

I'm trying to insert a new predefined data after 6th line while fetching result from mysql in php. After that, I want to insert a predefined text after every 5th line.

while($row=mysql_fetch_array($result))
{

if((($i%6)== 0)&&($i!=0))
{

$emparray['category'] = "PREDEFINED DATA";
array_push($json_response,$emparray);

}

//Data from the database
$emparray['category'] = ucwords($row["tag"]);
array_push($json_response,$emparray);
++$i;
}

This is my code that I had managed but it gives me "PREDEFINED DATA" after every 6th line. What I want is that after 6th line message should be shown after every 5th line.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jack Stern
  • 395
  • 1
  • 4
  • 16
  • have you tried `$i % 5 == 0` instead of `$i % $6` ? – 4br3mm0rd Jun 19 '16 at 07:53
  • @EmmanuelO Sorry for the mistake it is not $6 it is 6 and yes I had tried 5 too but after first 6 line I want the message after every 5th line. int 5 too doesn't give me the desired output. – Jack Stern Jun 19 '16 at 08:20
  • 1
    Try `$i % 5 == 1 && $i`. – vp_arth Jun 19 '16 at 08:42
  • **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jun 19 '16 at 09:17
  • So you want "separator" as line 7 (after 6 rows), 13 (after next 5),19 (after next 5), 25... and so on? – shudder Jun 19 '16 at 09:41

2 Answers2

2

If you want the value of key ['category'] of the associative array $emparray to be assigned as predefined data instead of the fetched result $row['tag'] at the 6th, 11th, 17th place and so on, the below code would give you the desired result.

$i=1;
while($row=mysql_fetch_array($result)){
   if(($i%6)==0){
     $emparray['category'] = "PREDEFINED DATA";
   }
   else{
//Data from the database
     $emparray['category'] = ucwords($row["tag"]);
   }
   array_push($json_response,$emparray);
   $i++;
}

If you want to add predefined data to the 6th, 11th, 17th place, and push the 6th fetched result $row['tag'] to the 7th place, you should do it like this.

$i=1;
while($row=mysql_fetch_array($result)){
   if(($i%6)==0){
     $emparray['category'] = "PREDEFINED DATA";
     array_push($json_response,$emparray);
     $i++;
   }
//Data from the database
     $emparray['category'] = ucwords($row["tag"]);
     array_push($json_response,$emparray);
     $i++;
}

If you want predefined data to be added at the 6th, 11th, 17th place, in addition to the 6th, 11th, 17th value of the fetched result $row["tag"], you are approaching this wrongly as you can't assign two values to a key, you would need to make $emparray['category'] a subarray i.e. $emparray['category'][] instead or concatenate the two before assigning it to the key.

SML
  • 1,235
  • 6
  • 17
1

Please set value of $i = 1 before loop. And $i % 5 == 0 in if condition.

$i = 1;
while($row=mysql_fetch_array($result))
{

if((($i%5)== 0)&&($i!=0))
{

$emparray['category'] = "PREDEFINED DATA";
array_push($json_response,$emparray);

}

//Data from the database
$emparray['category'] = ucwords($row["tag"]);
array_push($json_response,$emparray);
++$i;
}
bdtiger
  • 501
  • 4
  • 10