在PHP中增加格式化代码

时间:2017-01-29 19:54:19

标签: javascript php mysql

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

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

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

  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" />

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要从$ row [&#39; highest_id&#39;]增加一个数字($ i)。

额外的行将尝试将$ row [&#39; highest_id&#39;]解析为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++;
     }  

答案 1 :(得分:0)

尝试将变量解析为整数。

而不是使用$row['highest_id']      使用:$i = intval($row['highest_id'];