Php 限制单个输入字段中的重复
我有下面的代码,用于为一些租赁车(车辆)分配编号。我希望每辆车的编号都不同,即每辆车都有自己的唯一编号。一旦分配了购物车编号,则不允许其他购物车具有该编号。每个推车的编号都是唯一的。我自己无法做到这一点,任何来自你方的帮助将不胜感激。提前谢谢Php 限制单个输入字段中的重复,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有下面的代码,用于为一些租赁车(车辆)分配编号。我希望每辆车的编号都不同,即每辆车都有自己的唯一编号。一旦分配了购物车编号,则不允许其他购物车具有该编号。每个推车的编号都是唯一的。我自己无法做到这一点,任何来自你方的帮助将不胜感激。提前谢谢 // Adding Meta field in the meta container admin shop_order pages // if ( ! function_exists( 'mv_licence_fields_callback' ) ) {
// 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 class="driver_licences">';
$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 style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
<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).'" style="max-width:400px">';
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>"; } }
//在元容器管理商店订单页面中添加元字段//
如果(!function_存在('mv_license_fields_callback'))
{
函数mv_license_fields_callback()
{
全球$员额;
//获取订单详细信息//
$order\u id=$\u GET['post'];
$order=wc\u get\u order($order\u id);
//迭代顺序中的每个“行”项//
$order\u count=count($order->get\u items());
回声';
$count=1;
foreach($order->get\u items()作为$item\u id=>$item\u数据){
$item_quantity=$item_data->get_quantity();
对于($i=1;$iID,$driver\U cart'.$count,true)?获取\U post\U meta($post->ID,$driver\U cart'.$count,true):“”;
回声'
驾驶执照。$count。'
';
//在此指定购物车编号
回显“司机”。$计数。分配的购物车编号#
';
echo'ID,“驾驶执照”。$count,true)。“'style=“最大宽度:400px”>”;
回声“返回车”;
如果(获取帖子元($post->ID,“返回购物车”。$count,true)=='on'){
回显“购物车返回”;
}否则{
回显“检查此购物车是否已返回”。获取购物车元($post->ID,“返回购物车”。$count,true);
}
回声';
回声';
$count++;
}
}
回声';
回声“;}”
只需在循环末尾添加
$count++
因此,对于每个循环,它都会增加1
或
$count = rand(0,100);
如果你想让它成为每个循环的随机数
您的r代码将如下所示:
$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 style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
<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);
}
$count=1;
foreach($order->get\u items()作为$item\u id=>$item\u数据){
$item_quantity=$item_data->get_quantity();
对于($i=1;$iID,$driver\u cart.)$count,
正确)?获取帖子元($post->ID,“司机车”。$count,正确):“”;
回声'
驾驶执照。$count。'
';
回显“司机”。$计数。分配的购物车编号#
//此字段用于购物车编号//
';
$count++
//或
//$count=兰特(0100);
}
您可以使用VARABLE$i作为第二个循环中的计数器,而不是定义新的变量$count,因此代码如下所示:
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 style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
<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.'">';
foreach($order->get\u items()作为$item\u id=>$item\u数据){
$item_quantity=$item_data->get_quantity();
对于($i=1;$iID,$driver\u cart.$i,true)?获取\u post\u meta($post->ID,$driver\u cart.$i,true):“”;
回声'
驾驶执照“$i.”
';
回显“司机”。$i.“分配的购物车编号”#
//此字段用于购物车编号//
';
您能再次检查我的问题吗?我已经用完整的信息编辑了代码。我的代码在为不同的购物车分配号码时工作正常,但在为购物车分配号码时存在重复,这是我不想要的。