0

I have values returned that way after a calculation to take the percentage.

     $school = $api->schools;
    
            foreach ($school as $key => $value){
    $name = $value->id;

    $data = DB::table('sys_users')
        ->where('schoolid','=',$name)
        ->get();

    $exist1 = count($data)*4;

    $datarecord = DB::table('sys_record')
        ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
        ->where('sys_users.schoolid','=',$name)
        ->get();

    $correct1 = count($datarecord);

            function porcentagem_nx($parcial,$total) {
                return ($parcial * 100) / $total;
            }

    $worldone[$value->id] = porcentagem_nx($correct1, $exist1);
    
    // Because you already used $data above, I chose a different name
    // for this array.
    $items[] = [
        "id"       => $value->id,
        "name"     => $value->name,
        "worldone" => $worldone,
    ];
}
    
            dd($items);

I will use it on the datatable so it is necessary for my manual tests to be like the one below, but dynamic.

$data = [
                [
                "id" => 1,
                "name" => "Brand",
                "worldone" => $worldone,
                ],
                [
                    "id" => 2,
                    "name" => "Just",
                    "worldone" => $worldone,
                ],
            ];

I'm having trouble bringing the information from the worldone variable out of the foreach and making the array dynamic for all array records in the variable $school.

3
  • Where are you getting the values for the ID and name elements of the $data array that you're trying to achieve? Are they coming from the $value object in the foreach loop? Commented Jul 18, 2020 at 5:52
  • @dhnwebpro $api->schools; is array with key and value. Commented Jul 18, 2020 at 6:01
  • It looks like porcentagem_nx() is being declared twice somewhere. Is it declared in a Controller in the same namespace? App\Http\Controllers\RelatoriosController.php and somewhere else? If you need a second declaration, perhaps you could rename it to something like porcentageem_nx2() and see if it clears up the error? Commented Jul 18, 2020 at 6:37

1 Answer 1

1

I may be wrong in this answer, but I'm going to take a stab at it. Let me know if I'm off base because I'm making some assumptions that may not be correct. The resulting value you want to get out of the foreach loop looks like this:

$data = [
    [
        "id"       => 1,
        "name"     => "name",
        "worldone" => $worldone,
    ],
];

So my suggestion is something like this for your foreach loop:

foreach ($school as $key => $value){
    $name = $value->id;

    $data = DB::table('sys_users')
        ->where('schoolid','=',$name)
        ->get();

    $exist1 = count($data)*4;

    $datarecord = DB::table('sys_record')
        ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
        ->where('sys_users.schoolid','=',$name)
        ->get();

    $correct1 = count($datarecord);

    $worldone[$value->id] = porcentagem_nx($correct1, $exist1);
    
    // Because you already used $data above, I chose a different name
    // for this array.
    $items[] = [
        "id"       => $value->id,
        "name"     => $value->name,
        "worldone" => $worldone,
    ];
}

The $items variable will have all of the items set up according to what you specified. I'm assuming that the value of id and name are in the $value object in your foreach loop. If this is wrong let me know and I can update the answer to show where they should be coming from.

You can loop through $items or call something like print_r($items[0]); and the output should give you something similar to the $data array above.

Sign up to request clarification or add additional context in comments.

Comments

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.