PHP foreach循环为结果添加数字序列

时间:2014-07-02 15:57:21

标签: php mysql foreach

我正在尝试为每个返回的结果生成一个数字序列,如1,2,3,4等等。我已经搜索到所有并且没有找到答案所以我希望有人可以在这里帮助我。以下是代码。

输出应该看起来像

<input type="hidden" name="item_number_1" id="item_number_293" size="3" style="width: 60px;" class="validate[required]"  value="293" />
<input type="hidden" name="item_name_1" id="item_name_293" style="width: 320px;" class="validate[required]" value="Fish"  />    
<input type="hidden" name="quantity_1" id="quantity_293" style="width: 60px;" class="validate[required,custom[number]]" value="100" />
<input type="hidden" name="amount_1" id="amount_293" style="width: 60px;" class="validate[required,custom[number]]"  value="$1,850.00"  />

<input type="hidden" name="item_number_2" id="item_number_314" size="3" style="width: 60px;" class="validate[required]"  value="314" />
<input type="hidden" name="item_name_2" id="item_name_314" style="width: 320px;" class="validate[required]" value="Dogs"  />    
<input type="hidden" name="quantity_2" id="quantity_314" style="width: 60px;" class="validate[required,custom[number]]" value="1" />
<input type="hidden" name="amount_2" id="amount_314" style="width: 60px;" class="validate[required,custom[number]]"  value="$1.00"  />

<input type="hidden" name="item_number_3" id="item_number_315" size="3" style="width: 60px;" class="validate[required]"  value="315" />
<input type="hidden" name="item_name_3" id="item_name_315" style="width: 320px;" class="validate[required]" value="Cats"  />    
<input type="hidden" name="quantity_3" id="quantity_315" style="width: 60px;" class="validate[required,custom[number]]" value="5" />

function generatePayPalCheckout($user_id) {
    include $_SESSION['db'];
    try {
        $sql = "SELECT * FROM cart 
            WHERE user_id = :user_id ORDER BY cartid DESC";
        $s = $pdo->prepare($sql);
        $s->bindValue(':user_id', $user_id);
        $s->execute();
    } catch (PDOException $e) {
        moduleError($e->getMessage());
        exit();
    }
    $results = $s->fetchAll();

    if ($results) {
        foreach ($results as $result) {
            $username = userDeets($result['user_id']);
            $product =  getProductDeets($result['productid']);
            $getuseremail = $product['user_id'];
            $productowneremail = userDeets($getuseremail);

            $titlefix = str_replace(' ', '-', $product['title']);
            $titlefix = strtolower($titlefix);
            $titlefix = preg_replace("/[^a-z0-9_-\s]+/i", "", $titlefix);
            $cartqty = $result['qty'];
            $productsubtotal = $result['qty'] * $product['price'];
            $qtydiff = $cartqty - $product['qty'];
            $qtyrequested = $result['qty'];
            $counter = 0 + 1;
            ?>
                <input type="hidden" name="item_number_<?php echo $counter; ?>" id="item_number_<?php echo $product['id']; ?>" size="3" style="width: 60px;" class="validate[required]"  value="<?php echo $product['id']; ?>" />
                <input type="hidden" name="item_name_<?php echo $counter; ?>" id="item_name_<?php echo $product['id']; ?>" style="width: 320px;" class="validate[required]" value="<?php echo $product['title']; ?>"  />    
                <input type="hidden" name="quantity_<?php echo $counter; ?>" id="quantity_<?php echo $product['id']; ?>" style="width: 60px;" class="validate[required,custom[number]]" value="<?php echo $result['qty']; ?>" />
                <input type="hidden" name="amount_<?php echo $counter; ?>" id="amount_<?php echo $product['id']; ?>" style="width: 60px;" class="validate[required,custom[number]]"  value="<?php echo "$".number_format($productsubtotal, 2, '.', ','); ?>"  />
    <?php } } } 

非常感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

将您的计数器放在foreach循环

之外
$counter = 0 + 1;
每次循环时

都会返回1 ..

$counter = 0;
foreach ($results as $result) {

然后

$counter++;

答案 1 :(得分:0)

您的计数器将始终:1

$counter = 0 + 1;

你可以这样做:

if ($results) {
$i = 0;
    foreach ($results as $result) {
        $username = userDeets($result['user_id']);
        $product =  getProductDeets($result['productid']);
        $getuseremail = $product['user_id'];
        $productowneremail = userDeets($getuseremail);

        $titlefix = str_replace(' ', '-', $product['title']);
        $titlefix = strtolower($titlefix);
        $titlefix = preg_replace("/[^a-z0-9_-\s]+/i", "", $titlefix);
        $cartqty = $result['qty'];
        $productsubtotal = $result['qty'] * $product['price'];
        $qtydiff = $cartqty - $product['qty'];
        $qtyrequested = $result['qty'];

        $i++;           

        $counter = $i;
        ?>
            <input type="hidden" name="item_number_<?php echo $counter; ?>" id="item_number_<?php echo $product['id']; ?>" size="3" style="width: 60px;" class="validate[required]"  value="<?php echo $product['id']; ?>" />
            <input type="hidden" name="item_name_<?php echo $counter; ?>" id="item_name_<?php echo $product['id']; ?>" style="width: 320px;" class="validate[required]" value="<?php echo $product['title']; ?>"  />    
            <input type="hidden" name="quantity_<?php echo $counter; ?>" id="quantity_<?php echo $product['id']; ?>" style="width: 60px;" class="validate[required,custom[number]]" value="<?php echo $result['qty']; ?>" />
            <input type="hidden" name="amount_<?php echo $counter; ?>" id="amount_<?php echo $product['id']; ?>" style="width: 60px;" class="validate[required,custom[number]]"  value="<?php echo "$".number_format($productsubtotal, 2, '.', ','); ?>"  />
<?php } } } 

使用变量 i 作为计数器