0

I'm trying to get data to html table from database. In the database there's a company table which consists of id, name and percentage columns.

The above data in the table is entered by me manually. I need to fill those the same values by retrieving from database.

And company table in the database looks like this.Database table

In my coding it's getting an error in the place of array formation. I tried to get all the names in to one array and print in the html table. I'm kindly requesting to have a look at my coding. Highly appreciated.

$sql = "SELECT name, percentage FROM company";
$result = $conn->query($sql);

$result = $conn->query($sql);
if ($result->num_rows > 0) {

    // output data of each row
    while($row= $result->fetch_assoc()) {
      $arr=str_split($row)

       //echo  $row;
       echo "<table> <tr> <td> $arr[1] </td> </tr> </table> ";

    }
  } else {
      echo "0 results";
  }

@jakumi Error is enter image description here. Line 18 is '$arr=str_split($row);'.

4
  • 1
    and which error would that be? Commented Sep 12, 2017 at 6:21
  • 1
    add ; in this line $arr=str_split($row) Commented Sep 12, 2017 at 6:21
  • @Jakumi I edited the question. Please have a look. Thanks! Commented Sep 12, 2017 at 6:28
  • see my answer below Commented Sep 12, 2017 at 6:31

1 Answer 1

1

str_split expects a string, $row however already is an array. you access fields of each row by just $row['name'] for example.

so

echo '<table>';
while($row= $result->fetch_assoc()) {
    echo '<tr><td>'.$arr['name'].'</td><td>'.$arr['percentage'].'</td></tr>'; 
}
echo '</table>';

updated for answering a specific comment:

$percentages = [];
$namesToPercentages = [];
$collection = [];
while($row = $result->fetch_assoc()) {
    $percentages[] = $row['percentage']; 
    // this produces [0 => 0, 1=>1.1, 2=>1.2, ...]

    $namesToPercentages[$row['name']] = $row['percentage'];
    // this produces ['Base_price' => 0, 'A' => 1.1, ...]

    $collection[]= $row;
    // this produces [['name' => 'Base_price', 'percentage'=>0], [...],...]
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hey @Jakumi what I need to do is retrieve the names in database table to html table rows as I mentioned in the above image. If I use $row['name'] it will print all the names at once. There's no way to split them by name by name. That's why I planned to create a array. Do you any solution to do this? Thanks!
for your query it should not post all the names at once
You're a Legend. It's working. You helped me last time too. Thanks heaps. God bless you champ @Jakumi!
@RyanOscar don't know how you want your array to be structured, provided some cases in my answer.
Thanks @Jakumi <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.