php - Restrict duplication in single input field

532

I have this code below for assigning numbers to some rental carts(vehicle). I want the numbers to be different for every single cart i.e. every carts has its own unique number. Once the cart number is assigned then no other carts are allowed to have that number. The number is unique for every carts. I am not able to do this by myself, any help from your side will be highly appreciated. Thanks in advance.

// Adding Meta field in the meta container admin shop_order pages //
if ( ! function_exists( 'mv_licence_fields_callback' ) )
{
function mv_licence_fields_callback()
{
  global $post;

  //get order details //
  $order_id=$_GET['post'];
  $order = wc_get_order( $order_id ); 
  // Iterating through each "line" items in the order //
  $order_count=count($order->get_items());
  echo '<div >';
  $count=1;
  foreach ($order->get_items() as $item_id => $item_data) {
    $item_quantity = $item_data->get_quantity();
    for($i=1; $i<=$item_quantity; $i++){
      $driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$count, true ) ? get_post_meta( $post->ID, 'driver_cart'.$count, true ) : '';

      echo '
        <lable>Driver '.$count.' Licence</label>
      <div >
        <input type="file" name="driver_licence'.$count.'">';
      // assign cart number here 
      echo '<lable>Driver '.$count.' assigned cart NO#</label>
      <input type="text" name="driver_cart'.$count.'" value="'.$driver_assigned_cart.'">';

      echo '<img src="'.get_post_meta($post->ID, "driver_licence".$count, true).'" >';
      echo "<h5>Return Cart<h5>";
      if(get_post_meta($post->ID, "return_cart".$count, true)=='on'){
        echo "<span style='color:red;'>Cart Returned</span>";
      }else{
        echo '<input type="checkbox" name="return_cart'.$count.'"> Check if this cart is returned'. get_post_meta($post->ID, "return_cart".$count, true);
      }
      echo '<input type="hidden" name="product_id'.$count.'" value="'.$item_data->get_product_id().'">';
      echo '</div>';
      $count++;
    }
  }

  echo '<input type="hidden" name="prod_count" value="'.($count - 1).'">';
  echo "</div>"; } }
411

Answer

Solution:

You Can use the vairable $i as a counter inside the 2nd loop instead of defining a new variable $count, so the code will be as follow:

foreach ($order->get_items() as $item_id => $item_data) {
$item_quantity = $item_data->get_quantity();
for($i=1; $i<=$item_quantity; $i++){
    $driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$i, true ) ? get_post_meta( $post->ID, 'driver_cart'.$i, true ) : '';

    echo '
        <lable>Driver '.$i.' Licence</label>
    <div class="page_speed_1397330595">
        <input type="file"  name="driver_licence'.$count.'">';

    echo '<lable>Driver '.$i.' assigned cart NO#</label>
    //this field is for cart number //
    <input type="text"  name="driver_cart'.$i.'" value="'.$driver_assigned_cart.'">';   
575

Answer

Solution:

just add at the end of the loop

$count++ 

so for every loop it increment it by one

or

 $count = rand(0,100);

if you want to make it random numbers for every loop

on your'r code it will be as follows:

  $count=1;
 foreach ($order->get_items() as $item_id => $item_data) {
$item_quantity = $item_data->get_quantity();
for($i=1; $i<=$item_quantity; $i++){
$driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$count, 
true ) ? get_post_meta( $post->ID, 'driver_cart'.$count, true ) : '';

    echo '
        <lable>Driver '.$count.' Licence</label>
    <div class="page_speed_1397330595">
        <input type="file"  name="driver_licence'.$count.'">';

    echo '<lable>Driver '.$count.' assigned cart NO#</label>
    //this field is for cart number //
    <input type="text"  name="driver_cart'.$count.'" 
value="'.$driver_assigned_cart.'">';  
$count++ 
//or 
//$count = rand(0,100); 
} 

People are also looking for solutions to the problem: php - Sandbox PayPal approval link leads to dashboard instead of payment after logging in

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.