php foreach循环值在echo上给出错误的值

时间:2016-03-05 20:09:51

标签: php

我有这个PHP代码,它使用sql值来回显一个默认选择值的select标签。我有来自sql的M_class的值为1到10。但是所选择的值总是10.如何解决这个问题?

<?php foreach( $results as $row ){
       $tmpmclass = $row['M_class'];
       //start of giving Mclass values

            if ($tmpmclass = '1') {
            $mdmclass1 = "<option value='1' selected='selected'>V1</option>";
            } else {
            $mdmclass1 = "<option value='1' >V1</option>";
            }
            if ($tmpmclass = '2') {
            $mdmclass2 = "<option value='2' selected='selected'>V2</option>";
            } else {
            $mdmclass2 = "<option value='2' >V2</option>";
            }
            if ($tmpmclass = '3') {
            $mdmclass3 = "<option value='3' selected='selected'>V3</option>";
            } else {
            $mdmclass3 = "<option value='3' >V3</option>";
            }
            if ($tmpmclass = '4') {
            $mdmclass4 = "<option value='4' selected='selected'>V4</option>";
            } else {
            $mdmclass4 = "<option value='4' >V4</option>";
            }
            if ($tmpmclass = '5') {
            $mdmclass5 = "<option value='5' selected='selected'>V5</option>";
            } else {
            $mdmclass5 = "<option value='5' >V5</option>";
            }
            if ($tmpmclass = '6') {
            $mdmclass6 = "<option value='6' selected='selected'>V6</option>";
            } else {
            $mdmclass6 = "<option value='6' >V6</option>";
            }
            if ($tmpmclass = '7') {
            $mdmclass7 = "<option value='7' selected='selected'>V7</option>";
            } else {
            $mdmclass7 = "<option value='7' >V7</option>";
            }
            if ($tmpmclass = '8') {
            $mdmclass8 = "<option value='8' selected='selected'>V8</option>";
            } else {
            $mdmclass8 = "<option value='8' >V8</option>";
            }
            if ($tmpmclass = '9') {
            $mdmclass9 = "<option value='9' selected='selected'>V9</option>";
            } else {
            $mdmclass9 = "<option value='9' >V9</option>";
            }
            if ($tmpmclass = '10') {
            $mdmclass10 = "<option value='10' selected='selected'>V10</option>";
            } else {
            $mdmclass10 = "<option value='10' >V10</option>";
            }
       //end of giving Mclass values
       //making the mclass string
       $mmdmclass = "
       <select name='mclass' >
                              $mdmclass1
                              $mdmclass2
                              $mdmclass3
                              $mdmclass4
                              $mdmclass5
                              $mdmclass6
                              $mdmclass7
                              $mdmclass8
                              $mdmclass9
                              $mdmclass10
                            </select>
       ";
       //end of making  mclass string
     echo $mmdmclass;
   }
 ?>

0 个答案:

没有答案