2016-11-11 86 views
1

在此先感谢,我在表列中有三个薪水数据。我正在使用while循环在视图页面中显示三个薪水值。但我必须将这三个值传递给json中的三个变量,如{salary1:$ sal1,salary2:$ sal2,salary3:$ sal3}。如何单独循环三个薪金值到三个变量如何将数据库中的值传递给php中的json

My code as below: 

<table border="1"> 
     <caption><h2>View Registration</h2></caption> 
     <tr> 
      <th>Name</th> 
      <th>Designation</th> 
      <th>Email</th> 
      <th>Salary</th> 
     <tr> 
      <?php 

      include('common.php'); 
      $sql = mysql_query("select * from register"); 
      while($row = mysql_fetch_array($sql)) 
      { 

      ?> 
     <tr> 
      <td><?php echo $row['name']?></td> 
      <td><?php echo $row['designation']?></td> 
      <td><?php echo $row['email']?></td> 
      <td><?php 

      $salary = $row['salary'];  
      echo $salary; 


      ?></td> 
     <tr> 
      <?php 
      }   


      ?> 
     </table> 
+0

当你保存值id数据库,现在你可以使用json_decode,它返回数组,从这个数组,你可以访问这个 –

+0

使用'json_decode($ salary,TRUE)' – jitendrapurohit

+0

没有,只是如果我分开环三个薪金值分为三个变量是足够 –

回答

0

创建所有工资的数组,并json_encode呼应,阵列将在转换成JSON作为你想,

$sql = mysql_query("select * from register"); 

$salary =array(); 
while ($row = mysql_fetch_array($sql)) { 
    $salary[$row['name']] = $row['salary']; 
} 
echo json_encode($salary); 
+0

你的代码工作正常。取而代之的是salry1,salary2其实在json中我必须像这样{mohan:15000,Keshav:30000,vimal:35000}这样 –

+0

已经更新了员工的关键@Ragith托马斯 –

+0

的名字像这样的代码 “数据”:[{ “标签”: “莫汉”, “值”: “15000”, },{ “标签“: “凯沙夫”, “值”: “30000”, },{ “标签”: “VIMAL”, “值”: “35000”, }, –

1
<table border="1"> 
     <caption><h2>View Registration</h2></caption> 
     <tr> 
      <th>Name</th> 
      <th>Designation</th> 
      <th>Email</th> 
      <th>Salary</th> 
     <tr> 
      <?php 

      include('common.php'); 
      $sql = mysql_query("select * from register"); 
      $arraySalary=array();// declaring an array for json array 
      $loopCounter=1; 
      while($row = mysql_fetch_array($sql)) 
      { 

      ?> 
     <tr> 
      <td><?php echo $row['name']?></td> 
      <td><?php echo $row['designation']?></td> 
      <td><?php echo $row['email']?></td> 
      <td><?php 

      $salary = $row['salary']; 
      $arraySalary['salary'.$loopCounter]= $row['salary']; // making an array os diffrent index as salry1, salary2, salary3. 
      $loopCounter++; 
      ?></td> 
     <tr> 
      <?php 
      } 
      echo json_encode($arraySalary); // here you will get the salary json.  


      ?> 
     </table> 

请尝试一下。我已经检查过了。并且工作正常。

相关问题