简体   繁体   中英

Display MySQL records multiple times based on quantity and concatenate count

I am creating a gift card function where customers can purchase multiple quantities of gift cards with different denominations. For example, they can buy 1 x $50 gift card and / or 3 x $100 gift cards, etc, where 3 is the gcard_qty and $100 gift card is the gcard_name . Each denomination is also linked to a product_id .

The shopping cart function stores each product_id and its gcard_qty as one row in a MySQL database, along with a unique code and some other data (price etc). However when I send the gift card details via email, I need ALL the gift cards to display with their own unique code. This means 3 x $100 gift cards need to be displayed three times, with an amended code, so that they can be redeemed separately.

Here is what I have so far to loop through the records and display each row:

while($row = mysql_fetch_array($result))
{
    $gcid = $row["gcard_id"];
    for ($i = 0; $i < count($gcid); $i++) {
        $name = $row["product_name"];
        $price = $row["product_price"];
        $cardcode = $row["gcard_id"];
        $detailsofgiftcards .= $name . " <strong>Code: " . $cardcode . "</strong><br />";
    }
}

On echoing $detailsofgiftcards , naturally I only get one unique code per gift card value as below:

Gift Card $100.00 Value Code: 2000478282

Gift Card $50.00 Value Code: 2000478283

What I want is to automatically add a dash and a digit onto the end of the unique code for each individual card and display each card as a separate product, such as:

Gift Card $100.00 Value Code: 2000478282-1

Gift Card $100.00 Value Code: 2000478282-2

Gift Card $100.00 Value Code: 2000478282-3

Gift Card $50.00 Value Code: 2000478283-1

If there is only one gift card of that value, display code and -1 , otherwise display multiple records of the same row and concatenate with incremented values.

I think I need to start with something like:

 foreach($rows as $row) {
     for($i = 0; $i < $row['gcard_qty']; $i++) {
         // ???
     }
 }

And then I get completely lost.

If I got you data structure naming right, then you can do:

$gcid = $row["gcard_id"];
for ($i = 0; $i < count($gcid); $i++) {
    $name = $row["product_name"];
    $price = $row["product_price"];
    $cardcode = $row["gcard_id"];

    $quantity = $row["gcard_qty"];
    for ($j = 1; $j <= quantity; ++$j) {
        $detailsofgiftcards .= 
            $name . " <strong>Code: " . $cardcode . "-$j </strong><br />";
    }
}

This should suffice:

while($row = mysql_fetch_array($result)){
    $name = $row["product_name"];
    $price = $row["product_price"];
    $cardcode = $row["gcard_id"];
    $quantity = $row["gcard_qty"];
    for ($cid = 1; $cid <= $quantity; ++$cid) {
        $detailsofgiftcards .= 
            $name . " <strong>Code: " . $cardcode . "-$cid </strong><br />";
    }
}

Your inner loop was having an error. You're using count($gcid) where gcid is the id field. so it will always run once and terminate. Change it to quantity as I've done in the code.

My understanding is that you want to add new products to the database with the extra "-X" codes. Looks like other answers just help you echo all "-X", which might be more what you want...

foreach($rows as $row) { 
     for($end_id = 1; $end_id <= $row['gcard_qty']; $end_id++) { 
        $new_cardcode = $row['gcard_id'];
        $new_cardcode .= '-'.$end_id;

        if($end_id === 1){
            //update 'gcard_id' of the current row
        } else {
            //create a new product entry in database with all the same values as
            //$row except with the new cardcode
        }
    }
}

The main difference from what you had is that we're starting the for loop index on 1 instead of 0 so that we can use it in the $new_cardcode string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM