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; ?>"
  • 写回答

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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?