Alien
Alien

Reputation: 724

how to explode mysql table data in php

In mysql table table data, one column has multiple value like this,

code  name
1,2,3 a
4     b

My desired output will be

code name
1    a
2    a
3    a
4    b

Here is my code:

$sql="SELECT * FROM hsc_sub group by sub_name order by sub_code";
$res = app_db_query($app,$sql);
while($row = mysqli_fetch_assoc($res)){
  //enter code here
  $subj[] = $row;
  $subj[] = explode(",",$row['sub_code']);
}

Upvotes: 2

Views: 1066

Answers (1)

B.Balamanigandan
B.Balamanigandan

Reputation: 4875

Within the ForEach Loop, we can achieve your requirement.

$subCode[];
$name[];


echo("Code");
echo("\t");
echo("Name");
echo("\n");

$sql="SELECT * FROM hsc_sub group by sub_name order by sub_code";
$res = app_db_query($app,$sql);
while($row = mysqli_fetch_assoc($res)){
  $nm = $row['sub_name']
  $sub[] = explode(",",$row['sub_code']);
  foreach($sub as $item)
  {
    $name.push($nm);
    $subCode.push($item);

    echo($item);
    echo("\t");
    echo($nm);
    echo("\n");
  }
}

Upvotes: 1

Related Questions