关于mysql结果的php计数器

时间:2018-04-21 07:50:47

标签: php mysql loops foreach

我得到了这个简单的foreach循环,并且无法弄清楚计数器的问题在哪里。

我得到this之类的结果。如果满足条件,我试图将其放大。

$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";

$counter = 0;

$query = "SELECT  *  FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC"; // results give me numbers from 30 to 110. 
        $querydone = $db->query($query);
        while($row = $querydone->fetch_assoc()) {
            $every_id[$row['sifra']] = $row;
        }

            $firstday = new DateTime($tomorrows_date);
            $lastday = new DateTime($ends_date);

    for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
                        $result_day = $q->format("Y-m-d");

        $i = 0; // counter for every value from mysql 
        foreach ($every_id as $key => $value) { 

                $counter = ${$i++} = $value['sifra'];
            if($building >= $i) { 
                $valuesResult = "('$result_day','$counter')" . "<br />";
            } else {
                break; 
            }
            echo $valuesResult;
        }

    }

我哪里错了?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";

$counter = 0;

$query = "SELECT  *  FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT 80"; // results give me numbers from 30 to 110. 
        $querydone = $db->query($query);
        while($row = $querydone->fetch_assoc()) {
            $every_id = $row['sifra'];
        }

            $firstday = new DateTime($tomorrows_date);
            $lastday = new DateTime($ends_date);

    for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
                        $result_day = $q->format("Y-m-d");

        $i = 0; // counter for every value from mysql 
        foreach ($every_id as $key => $value) { 

                $counter = $i++;
            if($building >= $i) { 
                $valuesResult = "('$result_day','$counter')" . "<br />";
            } else {
                break; 
            }
            echo $valuesResult;
        }

    }
&#13;
&#13;
&#13;