douchao1957
2018-06-11 06:01
浏览 36
已采纳

如果已经在输入值中,如何回显值?

I have some values in a custom field:

save_post = "1200" 

Or it could be, since I need a list eventually:

save_post = "1200, 1460, 1334"

Now when I load the page, I get these values and I set them in an input field, and I also add the current value id from the current page:

$allPosts = $userPosts.", ".$postid;

Where $userPostsis the single value or the value list from the custom field, and $postid is the current page id I want to add.

The result is:

<input type="text" value="1200, 23138, 23138, 23138">

I would always get duplicate values each time I hit the update submit button as the page refreshes itself:

<form id="saveId" action="" method="POST" class="" autocomplete="off">
  <input type="text" name="save_post_value" value="<?php echo $userPosts.", ".$postid;  ?>"> 
  <button  type="submit" class="save_post btn btn-danger">Update</button>
</form>

How can I check if a value is already in the input and if so, don't echo it?

A way would be to have them in an Array and then output in the input field the unique array, not sure if there is a shorter way.

Trying:

$allPosts = array($userPosts.", ".$postid); 
$allPosts = array_unique($allPosts); 

<input type="text" name="save_post_value" value="<?php foreach ($allPosts as $value) { echo $value; } ?>">

Also:

$allPosts = array($userPosts.", ".$postid); 
$allPosts = array_unique($allPosts); 
$allPosts = explode(", ", $allPosts);
<input type="text" name="save_post_value" value="<?php echo $allPosts; ?>"

And tried with implode() too:

$allPosts = array($userPosts.", ".$postid); 
$allPosts = array_unique($allPosts); 
$allPosts = implode(", ", $allPosts);
<input type="text" name="save_post_value" value="<?php echo $allPosts; ?>"

图片转代码服务由CSDN问答提供 功能建议

我在自定义字段中有一些值:

  save_post  =“1200”
   
 
 

或者它可能是,因为我最终需要一个列表:

  save_post =“  1200,1460,1334“
   
 
 

现在当我加载页面时,我得到了这些,并将它们设置在中 输入字段,我还从当前页面添加当前值id

  $ allPosts  = $ userPosts。“,”。$ postid; 
   
 
 

其中 $ userPosts 是自定义字段中的单个值或值列表 , $ postid 是我想要添加的当前页面ID。

结果是:

   &lt; input type =“text”value =“1200,23138,23138,23138”&gt; 
   
 
 

每次点击更新时,我总会得到重复的值 页面刷新时提交按钮:

 &lt; form id =“saveId”action =“”method =“POST”class =“”autocomplete  =“off”&gt; 
&lt; input type =“text”name =“save_post_value”value =“&lt;?php echo $ userPosts。”,“。$ postid;  ?&gt;“&gt; 
&lt; button type =”submit“class =”save_post btn btn-danger“&gt;更新&lt; / button&gt; 
&lt; / form&gt; 
   
  
 

如何检查输入中是否已有值,如果是,请不要回显它?

一种方法是将它们放在中 数组,然后在输入字段 unique array 中输出,不确定是否有更短的路径。

尝试:

  $ allPosts = array($ userPosts。“,”。$ postid); 
 $ allPosts = array_unique($ allPosts); 
 
&lt; input type  =“text”name =“save_post_value”value =“&lt;?php foreach($ allPosts as $ value){echo $ value;  }&gt;“&gt; 
   
 
 

另外:

  $ allPosts = array($ userPosts。”,  “。$ postid); 
 $ allPosts = array_unique($ allPosts); 
 $ allPosts = explode(”,“,$ allPosts); 
&lt; input type =”text“name =”save_post_value“value =”&lt  ;?php echo $ allPosts;  ?&gt;“
   
 
 

并尝试使用 implode()

   $ allPosts = array($ userPosts。“,”。$ postid); 
 $ allPosts = array_unique($ allPosts); 
 $ allPosts = implode(“,”,$ allPosts); 
&lt; input type =“text  “name =”save_post_value“value =”&lt;?php echo $ allPosts;  ?&gt;“
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douke6424 2018-06-11 06:21
    已采纳

    This is very basic example, but I think that this can be useful for your needs:

    <?php
    // Input data
    $userPosts = '19000, 23138, 23138';
    $postid = '23138';
    
    // With array
    $userPosts = str_replace(' ', '', $userPosts);
    if (empty($userPosts)) {
        $a = array();
    } else {
        $a = explode(',', $userPosts);
    }   
    $a = array_unique($a, SORT_STRING);
    if (in_array($postid, $a) === false) {
        $a[] = $postid;
    }
    $userPosts = implode(', ', $a);
    echo 'Result using array: '.$userPosts.'</br>';
    ?> 
    

    UPDATE:

    It is possible to use a function. Check for empty posts is made using empty().

    <?php
    function getUniquePosts($xposts, $xid) {
        $xposts = str_replace(' ', '', $xposts);
        if (empty($xposts)) {
            $a = array();
        } else {
            $a = explode(',', $xposts);
        }   
        $a = array_unique($a, SORT_STRING);
        if (in_array($xid, $a) === false) {
            $a[] = $xid;
        }
        $xposts = implode(', ', $a);
        $xposts = ltrim($xposts, ",");
    
        return $xposts;
    }
    
    $userPosts = '19000, 23138, 23138';
    $postId = '23138';
    echo getUniquePosts($userPosts, $postId).'</br>';
    ?> 
    

    Then when loading form you can try with this:

    ...
    $a = array_unique($a, SORT_STRING); 
    ...
    update_user_meta($user_id, 'save_post', getUniquePosts($a, $user_id));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhi4470 2018-06-11 07:08

    Here is my code on checking duplicate values after submission:

    $userPosts = '19000, 23138, 23138';
    $postid = '23138';
    $pattern = "/(?:^|\W)".$postid."(?:$|\W)/";
    
    if(preg_match($pattern, $userPosts, $matches))
    {
        print 'There is a duplicate '.rtrim($matches[0] , ",");
    }
    

    Basically I reuse Zhorov's variables but on his approach he put it in an array, and then check if that array contains the submitted value, mine is almost the same as his approach but instead of putting it in an array; I use regex to determine is the value existed in the string.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题