Home > Mobile >  Variable transfer for loop to loop, or data error
Variable transfer for loop to loop, or data error

Time:02-25

I’m here for one thing right now that has an array called dayinfo. In which I want to name the item number, the item number can be found in the variable fixed interval.date_id. Do you know a solution for transferring data in the forban in this way? Although the problem may arise elsewhere in the code

My twig code:

   {% for fixintervall in days %}
        <tr>
            <th><p>
                {{ fixintervall.start_day  }}
                </p></th>

    {% for dayinfo in daysinfo %}
        {% for dayinfoti in dayinfo..fixintervall.date_id %}
            {% for dayin in dayinfoti %}
            <p>
                {{ dayin.start }} - {{ dayin.start2 }}
            </p>
            {% endfor %}
        {% endfor %}
    {% endfor %}

            <br>

        </tr>
        <br>
    {% else %}

Controller Code:

 #[Route('/cons/show/{id}', name: 'user_cons_show', methods: ['GET'])]
    public function show($id, ConsRepository $consRepository): Response
    {
        $showcon = array();
        $showconsintervall = array();
        $days = array();
        $dayid = array();
        $daysinfo = array();



        $con = $consRepository->find($id);
        $showconsintervall = $con->getIntervalls();

       foreach ($showconsintervall as $ex){
            $datumid = $ex->getId();
            $kezdodatum = $ex->getStart();
            $vegdatum = $ex->getEnd();
            $morepeople = $ex->getMore();
            $freetime = $ex->getFreeTime();
            $constime = $ex->getConsTime();
            $consulid = $id;

            /* Array in belüli array a cél az időpont listázás miatt */

            while($kezdodatum <= $vegdatum) {
                date_default_timezone_set($kezdodatum->getTimezone()->getName());
                if (date("Y-m-d",$kezdodatum->getTimestamp()) >= date('Y-m-d'))
                {
                    array_push(
                        $days, [
                        "start_day" => date("Y-m-d",$kezdodatum->getTimestamp()),
                        "date_id" => $datumid,
                    ]);

                    //logikai bukfenc

                    $start_a = date("H:i",$kezdodatum->getTimestamp());
                    $end_a = date("H:i",$vegdatum->getTimestamp());
                    $dat_a =  date("H:i",$constime->getTimestamp());
                    $up_a = date("H:i",$freetime->getTimestamp());


                    if (!array_key_exists($datumid, $daysinfo)){

                        $start = $start_a;
                        $end = $end_a;
                        $dat =  $dat_a;
                        $up = $up_a;
                        $start2 = $start;


                        $x = $datumid;
                        $x_data = array();

                       // echo "<br><br><br>Info:". $start2 ." - ". $end."<br>";
                        while ($start2 <= $end)
                        {


                            $start2_noft = (strtotime($start)   (strtotime($dat)) - strtotime('00:00:00'));
                            $start2 = date("H:i", $start2_noft);
                           // echo "Eleje:".$start." ".$start2." <br>";

                            if ($start2 <= $end){
                                array_push(
                                    $x_data,[
                                    "start" => $start,
                                    "start2" => $start2,
                                ]);
                            }

                            $start_noft = strtotime($start2)   strtotime($up) - strtotime('00:00:00');
                            $start = date("H:i", $start_noft);
                           // echo "Vége:".$start." ".$start2." <br>";
                        }

                        array_push(
                            $daysinfo,[
                            $x => $x_data
                        ]);
                    }
                }
                    $kezdodatum->modify(' 1 day');
                }

            }


        array_push(
            $showcon, [
            "user_id" => $con->getUserId()->getId(),
            "cons_id" => $id,
        ]);

       print_r($daysinfo);

        return $this->render('user/cons_show.html.twig', [
            'controller_name' => 'Consulens',
            'showcon' => $showcon,
            'days' => $days,
            'dayid' => $dayid,
            'daysinfo' => $daysinfo,
        ]);
    }

And the problem is, after all, that the data does not appear. At least the intervals are not

enter image description here

CodePudding user response:

The structure {% for x in y..z %} is used to loop over a sequence of numbers/letters as seen in the documentation

This will generate/compile to something like

<?php
    for($x = $y; $x < $z; $x  ) {
        echo $x;
    }

As you didn't provide much information about the array dayinfoti I'll assume the keys of dayinfoti will match fixintervall.date_id. If this is the case then you can just use this variable as the key of the array as you would do in PHP

{% for fixintervall in days %}
    ...
    ....
    {% for data in dayinfo[fixintervall.date_id]|default([]) %}
        ...
        ...
    {% endfor %}
{% endfor %}

The default filter will account for missing elements in the dayinfo
Basically, this removes the need to test if the key fixintervall.date_id isset in the array dayinfo before looping it


More about dynamic keys/variables

  • Related