第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 PHP 中插入選擇 2 標(biāo)記 j查詢數(shù)組

在 PHP 中插入選擇 2 標(biāo)記 j查詢數(shù)組

PHP
慕工程0101907 2022-09-24 16:06:50
我已經(jīng)看到許多關(guān)于這個(gè)問(wèn)題的堆棧溢出線程,但我無(wú)法實(shí)現(xiàn)這一點(diǎn),所以再次發(fā)表了這篇文章。我正在使用我想使用PHP在SQL中插入標(biāo)簽的地方。贊 [select2 jQuery pluginData, Data, Data]我嘗試做我通過(guò)谷歌學(xué)到的東西,好吧,我在這方面是專業(yè)化的,我是PHP的新手。請(qǐng)幫助'我如何在數(shù)據(jù)庫(kù)中插入這個(gè),其中顯示兩個(gè)單詞之間,如上所述,我在,Like'我的代碼是if(isset($_POST['submit'])) {    $name = "Your_Name";    $email = "Your_Email";    $stmt = $con->prepare("INSERT INTO `test` (`name`, `email`, `color_name`) VALUES (':name',':email',':color')");    foreach ($data as $row)    {        ':name' = $name;        ':email' = $email;        ':color' = 'MYStatus'; //I want mention here Select2 Tags Data and insert in DB where [, (space)] every two words. like [Green, Red, Blue, Pink]         $stmt->execute();    }  }<!DOCTYPE html><html><head><link rel="stylesheet" type="text/css" href="http://shashani-humanth.github.io/Notebook-AdminPanel/js/select2/select2.css"></head><body><form class="bs-example form-horizontal" id="formid" method="POST" onSubmit="return validate();">    <div class="form-group col-sm-6">      <div>        <input type="hidden" name="selectname[]" id="select2-tags" style="width:260px" value="brown"/>      </div>    </div>    <div class="form-group">        <div class="col-lg-offset-2 col-lg-10">            <button type="submit" name="submit" class="btn btn-sm btn-default" id="submit">Save and Publish</button>        </div>    </div></form><script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/jquery.min.js"></script>  <script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/bootstrap.js"></script><script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/select2/select2.min.js"></script><script type="text/javascript" language="javascript">    if ($.fn.select2) {      $("#select2-option").select2();      $("#select2-tags").select2({        tags:["Red", "Green", "Blue", "Pink"],        tokenSeparators: [",", " "]}      );    }</script></body></html>
查看完整描述

1 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

您的表單可能按如下方式發(fā)送:


Array

(

    [selectname] => Array

        (

            [0] => brown,Green,Blue

        )


)

因此,既然您希望將其格式化為 ,那么您可以和/或僅使用:brown, Green, Blueexplode()implode()str_replace()


# Exploding, imploding

$tags = implode(", ", explode(",", $_POST['selectname'][0]));


# String replace would be

$tags = str_replace(',', ', ', $_POST['selectname'][0]);

如果您嘗試拆分該字符串以單獨(dú)插入每個(gè)標(biāo)記,則應(yīng)在逗號(hào)上使用,然后循環(huán) .explode()explode()


我可能會(huì)在那里使用刪除空白空間,以防萬(wàn)一。另外,如果你想確保它們的格式都相同,你可能想確保每個(gè)單詞都有一個(gè)大寫(xiě)字母作為第一個(gè)字母(你的默認(rèn)值棕色全部變低,重置時(shí)第一個(gè)字母大寫(xiě))。trim()ucwords()


如果執(zhí)行方法 1,則可以應(yīng)用一個(gè),如果在分解字符串上使用:ucfirst()trim()array_map()


# A full combination of functions

$tags = implode(", ", array_map(function($v){

    return ucfirst(trim($v));

}, explode(",", $_POST['selectname'][0])));

會(huì)給你字符串:


Brown, Green, Blue

編輯:


由于您實(shí)際上是按行存儲(chǔ)的,因此可以使用 :select


function getColorTags($name, $con)

{

    # Query by name, just return the color_name field though

    $query = $con->prepare("SELECT `color_name` FROM `test` WHERE `name` = ?");

    # Execute the query with bind value

    $query->execute([$name]);

    # Loop the results

    while($result = $query->fetch(\PDO::FETCH_ASSOC)) {

        # Store the tags

        $row[] = $result['color_name'];

    }

    # Return the tags or an empty array

    return (isset($row))? $row : [];

}

使用方法:


# Fetch the tags by name, make sure to inject the database connection

$tags = getColorTags('Some Name', $con);

# Print out the array

print_r($tags);

編輯 #2


要在同一頁(yè)面上同時(shí)執(zhí)行此操作,您只需使用:json_encode()


<?php

function insertTags($data, $email, $name, $con)

{

    $stmt = $con->prepare("INSERT INTO `test` (`name`, `email`, `color_name`) VALUES (?,?,?)");


    foreach ($data as $string) {

        # Explode and filter the tags from the js

        $arr = array_filter(array_map('trim', explode(',', $string)));

        # Loop the "selectname"

        foreach($arr as $tag) {

            # Execute all the rows

            $stmt->execute([$name, $email, $tag]);

        }

    }

}

# This fetches using the email as the primary key

function getColorTags($email, $con)

{

    # Query by name, just return the color_name field though

    $query = $con->prepare("SELECT `color_name` FROM `test` WHERE `email` = ?");

    # Execute the query with bind value

    $query->execute([$name]);

    # Loop the results

    while($result = $query->fetch(\PDO::FETCH_ASSOC)) {

        # Store the tags

        $row[] = $result['color_name'];

    }

    # Return the tags or an empty array

    return (isset($row))? $row : [];

}


# Pull these out so you can use them in general

$name = "Your_Name"; // Assuming this is from the session

$email = "Your_Email"; // Assuming this is from the session

# Here is where you insert

if(isset($_POST['submit'])) {

    # This will insert your tags separately on new rows

    insertTags($_POST['selectname'], $email, $name, $con);

    # This will pull them back from the database (helpful if they already have some in there)

    $tags = getColorTags($email, $con);

}

# Here you check if there are tags already generated after submission,

# if not, then pull them.

if(!isset($tags))

    $tags = getColorTags($email, $con);

?><!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" type="text/css" href="http://shashani-humanth.github.io/Notebook-AdminPanel/js/select2/select2.css">

</head>

<body>

<form class="bs-example form-horizontal" id="formid" method="POST" onSubmit="return validate();">

    <div class="form-group col-sm-6">

        <div>

            <input type="hidden" name="selectname[]" id="select2-tags" style="width:260px" value="brown"/>

        </div>

    </div>


    <div class="form-group">

        <div class="col-lg-offset-2 col-lg-10">

            <button type="submit" name="submit" class="btn btn-sm btn-default" id="submit">Save and Publish</button>

        </div>

    </div>

</form>


<script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/jquery.min.js"></script>

  <script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/bootstrap.js"></script>

<script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/select2/select2.min.js"></script>


<script type="text/javascript" language="javascript">

    if ($.fn.select2) {

        $("#select2-option").select2();

        $("#select2-tags").select2({

            // Now use this native function to echo the array back to JS

            tags: <?php echo json_encode($tags) ?>,

            tokenSeparators: [",", " "]}

        );

    }

</script>


</body>

</html>


查看完整回答
反對(duì) 回復(fù) 2022-09-24
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)