在 php 中出现增量问题
Having increment issue in php
我希望第二列递增,但它采用默认计数器值 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;
?;
像这样在 foreach 之前使用 $counter= 1;
:-
$counter= 1;
foreach ($pdo->query($sqlUsers, PDO::FETCH_ASSOC) as $row) {
在你的情况下每次计数器值都是 1
将你的 counter
设置在 foreach 之外
$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++;
}
我希望第二列递增,但它采用默认计数器值 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;
?;
像这样在 foreach 之前使用 $counter= 1;
:-
$counter= 1;
foreach ($pdo->query($sqlUsers, PDO::FETCH_ASSOC) as $row) {
在你的情况下每次计数器值都是 1
将你的 counter
设置在 foreach 之外
$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++;
}