javascript - 在 PHP 中增加格式化代码

标签 javascript php mysql

我为从下拉菜单中选择的每个类别都有一个代码格式。 假设对于类别 A,要在文本框中检索的代码是“A - 1001”,对于类别 B,要在文本框中检索的代码是“B - 2003”。我的问题是,我无法将类别 A 的代码增加到“A - 1002”,例如,因为它已经被读取为字符串。

如何从数据库中检索格式化代码以增加其值?

这是我用于选择类别和检索代码的代码:

  Category:

    <script type="text/javascript">
    function GetSelected (selectTag) {
        var selIndexes = "";

        for (var i = 0; i < selectTag.options.length; i++) {
            var optionTag = selectTag.options[i];
            if (optionTag.selected) {
                if (selIndexes.length > 0)
                    selIndexes += ", ";
                selIndexes = optionTag.value;
            }
        }

        var info = document.getElementById ("viocode");
        if (selIndexes.length > 0) {
            viocode.innerHTML = selIndexes;
        }
        else {
            info.innerHTML = "There is no selected option";
        }
    }

  </script>
                <select option="single"  name= "viocat" id="viocat" onchange="GetSelected (this);" class = "form-control">
    <option>Choose category ...</option>
    <option value="

 <?php
 $con = ...

 $sql = "SELECTcategory, MAX(code) AS highest_id FROM tbl_name where category = 1";

 $result = mysql_query ($sql,$con);

 while($row = mysql_fetch_array($result))
 {
    $i = $row['highest_id'];
    $i++;
    echo "A - " .$i;
    $cat = 1;
 }  

 ?>">DlR</option>

 <option value=" 
 <?php
 $con =  ...

 $sql = "SELECT category, MAX(code) AS highest_id FROM tbl_name where category = 2";

 $result = mysql_query ($sql,$con);

while($row = mysql_fetch_array($result))
 {
    $i = $row['highest_id'];
    $i++;
    echo "B - " .$i;
    $cat = 2;
  }  

 ?>">B</option>

 <option value="
 <?php
 $con = ...

$sql = "SELECT category, MAX(code) AS highest_id FROM tbl_name where category = 3";

$result = mysql_query ($sql,$con);

while($row = mysql_fetch_array($result))
{
    $i = $row['highest_id'];
    $i++;
    echo "C - " .$i;
    $cat = 3;
}  

?>">C</option>
</select>

这是要显示格式化代码的文本框的代码:

 Violation Code:
 <strong><text type= "text" id="viocode"  name="viocode" />

最佳答案

如果我理解正确的话,您需要从 $row['highest_id'] 增加一个数字 ($i)。

额外的一行将尝试将 $row['highest_id'] 解析为 int,然后您的代码使用它,并且 while 循环的最后一行将 $i 递增 1。

while($row = mysql_fetch_array($result))
     {  
        $i = intval($row['highest_id']);
        //$i = $row['highest_id'];
        echo "A - " .$i;
        $cat = 1;
        $i++;
     }  

关于javascript - 在 PHP 中增加格式化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41925278/

相关文章:

php - 在插入之前将字符串转换为 mysql 日期格式

php - 无法使用 crontab 运行 shell 脚本

php - 只需单击它们即可更新表格行

javascript - React.js onMouseOver 中的两个函数

javascript - 比较输入和数字

php - 如何使自动完成在 foreach php 循环中工作? (vscode,智能感知)

MySql 使用引用键查找树结构

php - 如何在codeigniter中现有项目中将mysql数据库转换为mongodb

javascript - CKEDITOR 的 insertHtml 不工作,除了在 setTimeout 中

javascript - 在 "\n"和 "<br>"之间切换