【问题标题】:Having increment issue in php在php中有增量问题
【发布时间】:2015-11-16 04:42:49
【问题描述】:

我希望第二列增加,但它采用默认计数器值 1。我希望它为 1、2、3 等。

这是我的代码....

$sqlUsers = 'SELECT `user_id`, `name` FROM `users` ORDER BY `name`;'; // "outer" SQL statement to get a list of user names and IDs

// prepared statement
// NOTE: SUM(`sale_price`) AS s
$sqlPurchases = 'SELECT `transaction`,`date`,SUM(`sale_price`) AS s '
              . 'FROM `purchases` WHERE `user_id` = :id GROUP BY `transaction`;';
$purchStmt = $pdo->prepare($sqlPurchases);

echo '<h3>User Purchases</h3>', '<hr />', PHP_EOL;
echo '<table border=2>', PHP_EOL;

// run query() to get list of user IDs and names
foreach ($pdo->query($sqlUsers, PDO::FETCH_ASSOC) as $row) {

    // execute the prepared statement using $row data to get user_id
    $purchStmt->execute(array(':id' => $row['user_id']));
    $counter= 1;

    echo '<tr><th>', $row['name'], '</th><th>' . $counter++ . '</th><td>';

    echo '<table border=1>', PHP_EOL;
    echo '<tr><th>Transaction</th><th>Date</th><th>Amount</th></tr>', PHP_EOL;

    // fetch result
    while($result = $purchStmt->fetch(PDO::FETCH_ASSOC)) {
        echo '<tr>';
        echo '<td>', $result['transaction'], '</td>', PHP_EOL;
        echo '<td>', $result['date'], '</td>', PHP_EOL;
        // Echos SUM(`sale_price`) AS s
        echo '<td align="right">', $result['s'], '</td>', PHP_EOL;
        echo '</tr>';
    }

    echo '</table>', PHP_EOL;
    echo '</td></tr>', PHP_EOL;
}

echo '</table>', PHP_EOL;

?;

Here's the output result

【问题讨论】:

    标签: php increment


    【解决方案1】:

    在 foreach 之前使用 $counter= 1; :-

    $counter= 1;
    foreach ($pdo->query($sqlUsers, PDO::FETCH_ASSOC) as $row) {
    

    在你的情况下,每次计数器值为 1

    【讨论】:

    • 谢谢你,我会试一试并给你反馈。再次感谢
    【解决方案2】:

    在 foreach 之外设置您的 counter

     $counter=1
        foreach ($pdo->query($sqlUsers, PDO::FETCH_ASSOC) as $row) {
    
                // execute the prepared statement using $row data to get user_id
                $purchStmt->execute(array(':id' => $row['user_id']));
    
    
                echo '<tr><th>', $row['name'], '</th><th>' . $counter. '</th><td>';
    
                echo '<table border=1>', PHP_EOL;
                echo '<tr><th>Transaction</th><th>Date</th><th>Amount</th></tr>', PHP_EOL;
    
                // fetch result
                while($result = $purchStmt->fetch(PDO::FETCH_ASSOC)) {
                    echo '<tr>';
                    echo '<td>', $result['transaction'], '</td>', PHP_EOL;
                    echo '<td>', $result['date'], '</td>', PHP_EOL;
                    // Echos SUM(`sale_price`) AS s
                    echo '<td align="right">', $result['s'], '</td>', PHP_EOL;
                    echo '</tr>';
                }
    
    
    
                  echo '</table>', PHP_EOL;
                    echo '</td></tr>', PHP_EOL;
        $counter++;
                }
    

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2011-09-10
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      相关资源
      最近更新 更多