简体   繁体   中英

PHP foreach executing code multiple times

At the bottom of this code you'll see an 'Accept Offer' button, when I click on that another piece of code gets executed as you can see on the bottom of this post.

For example this project has 3 bidders, so 3 times bidder_id and writer_bid so I use 'foreach' and load it in divs, works fine, but now I need to store those variables in a database, which technically works but it doesn't store the bids from the row I pull them from, it just takes the data from the last row, that is if I place the code at the bottom of this thread in my header.

However when I put it inside the loop it executes three times, I saw that when I got an error message that I had to close 3 times cause there are 3 rows in the database table that I pull the data from.

How can I prevent this, and either have it load once when the code is inside the foreach loop, or have it pull the correct writer_bid and bidder_id to store.

                <div class="WB-Bottom-block lefts">
                <?php $getBidders=" AND project_id=$project_id"; $bidders=getBidder($getBidders); foreach($bidders as $bidder) {
                    $bidder_id=$bidder['writer_id'];
                    $writer_bid=$bidder['writer_bid'];
                ?>

                <div class="findwriters_boxes">

                    <div class="findwriters_right">
                        <div style="float:right;margin-top:6px;width:170px;">
                            <input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
                            <input type="hidden" name="bidder_id" id="bidder_id" value="<?php echo $bidder_id; ?>" />
                            <input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
                        </div>
                    </div>
                </div><?php } ?>

Below the code that needs to be executed and that results in issues, whether I place it inside the foreach loop, or inside the header instead.

As you can see I tried to store it in input fields so that it stays there so the header can pull it on refresh of the page / click of the button.

<?php if(isset($_POST['todo']) && $_POST['todo']=='submit_project') {
$balance=get_client_balance_info($current_user->ID);
$writer_bid=$_POST['writer_bid'];
$bidder_id=$_POST['bidder_id'];

if($balance >= $_POST['writer_bid']) {
    global $wpdb;
    $sql3="UPDATE `wp_project` SET `writer_id` = '".$bidder_id."' WHERE `id` =". $project_id;
    $wpdb->query($sql3);
    $sql4="UPDATE `wp_project` SET `price` = '".$writer_bid."' WHERE `id` =". $project_id;
    $wpdb->query($sql4);
    $sql5="UPDATE `wp_project` SET `status` = '2' WHERE `id` =". $project_id;
    $wpdb->query($sql5);
    $success_msg="You accepted a bid, the money will be deducted from your account.";
}

else $fail_msg="Your balance is not sufficient.";

I think you should make a form for each div that you are adding right now you are putting the bidder_id in the different inputs but the same name.
So it will get the last inputs, maybe it's better to specify the inputs with the row id or to separate the forms or make the input names as array.
I hope this helps you.

I fixed it with the help of Diar Selimi like this:

<div style="float:right;margin-top:6px;width:170px;">
<form action="" name="frmeditor" method="post" id="frmeditor" >
    <input type="hidden" name="todo" id="todo" value="submit_project"  />
    <input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
    <input type="hidden" name="writer_id" id="writer_id" value="<?php echo $writer_id; ?>" />
    <input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</form>

Before that my form and value="submit_project" tags were scattered all over the place!

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