每行中的编辑按钮应根据设备名称执行代码

时间:2014-08-16 07:15:28

标签: php mysql

在我的程序中,我从数据库中获取数据并以表格形式显示以及编辑和删除按钮。用于此的代码如下......

$sql = "SELECT * FROM temp_notification order by id asc Limit $start, $perpage"; 
        $res = mysql_query($sql) or die(mysql_error());
        $numRows = mysql_num_rows($res);
         if ((mysql_num_rows($res)) > 0){
?>
<table border='2' cellpadding="18">
  <tr>
    <!--<th bgcolor='green'><font color='white'>#</font></th>-->   
    <th bgcolor='green'><font color='white'>Title</font></th>
    <th bgcolor='green'><font color='white'>Message</font></th> 
    <th bgcolor='green'><font color='white'>Device</font></th>

  </tr>
<?php
            $i = 0;
            while($row = mysql_fetch_array($res)){
            ?>  
        <tr>              
            <td><center><Strong><?php echo $row['title']; ?></Strong></center></td>
            <td><center><Strong><?php echo $row['message']; ?></Strong></center></td>           
            <td><center><Strong><?php echo $row['device']; ?></Strong> &nbsp;&nbsp;&nbsp;
            <td><center><Strong><a href="principal_notifi_all.php?id=<?php echo $row['id'];?>">Send</a></Strong></center></td>
            <td><form method='POST' action="notification_edit.php?id=<?php echo $row['id'];?>"><input type='hidden' name='tempId' value='".$row["device"].
            "'/><input type='submit' name='submit-btn' value='View/Update Details' /></form></td>
            <td><center><Strong><a href="delete_notification.php?id=<?php echo $row['id'];?>">Delete</a></Strong></center></td>

        </tr>

我将设备字段中存储的值设置为android和ios.Now,具体取决于我要链接到特定页面的设备名称..如果设备名称是android我应该给aherf链接到1.php,如果它是ios它应该链接到2.php。我使用的代码如下......但它不能正常工作..Plz help ...

notification_edit.php

<?php

require_once('db/connection.php');
$id = $_GET['id'];
if(isset($_POST["tempId"])){ 
     //pass data using post then update. Here's where I keep getting only the latest record regardless of selected record from previous page
    require_once('db/connection.php');

//$device=$_POST['device']; 
$query=mysql_query("SELECT * FROM temp_notification WHERE id='$id'");
//$row=mysql_num_rows($query);
$role = mysql_fetch_array($query);

    if ($role['device'] == 'ios'){
        header("Location : notification_all_ios.php");
            exit();

    }

elseif($role['device'] == 'android')
{
header("Location : notification_all.php");
exit();
}
else{
echo "error";}
}
?>

1 个答案:

答案 0 :(得分:0)

首先是您的格式化代码

<td>
   <form method='POST' action="notification_edit.php?id=<?php echo $row['id'];?>">
       <input type='hidden' name='tempId' value='".$row["device"]."'/>
       <input type='submit' name='submit-btn' value='View/Update Details' />
   </form>
</td>

现在:有很多方法可以实现这一目标。

直接方式 [完全跳过notification_edit.php]

$action = '';
if($row['device'] == 'ios'){
  $action = 'notification_all_ios.php';
}else if($row['device'] == 'android'){
  $action = 'notification_all_android.php';
}else{
  $action = 'notification_all.php';
}

您的代码

<td>
   <a href="<?php echo $action ?>">View Notification</a>
</td>

表格方式

<td>
   <form method='POST' action="notification_edit.php?id=<?php echo $row['id'];?>">
       <input type='hidden' name='tempId' value='".$row["device"]."'/>
       <input type='submit' name='submit-btn' value='View/Update Details' />
   </form>
</td>

其他档案

<?php

require_once('db/connection.php');
$id = $_GET['id'];
if(isset($_POST["tempId"])){ 
  require_once('db/connection.php');
  $device=$_POST['device']; 
  //you already have $device so you do not need a query here
  //$query=mysql_query("SELECT * FROM temp_notification WHERE id='$id'");
  //$role = mysql_fetch_assoc($query);
  //do a print_r of $role
  //print_r($role);
  //if it has an index 'device' with value 'ios' or 'android' this should work
  //if ($role['device'] == 'ios'){
  //other wise do this
  if ($device == 'ios'){
    header("Location : notification_all_ios.php");
  }else if($device == 'android'){
    header("Location : notification_all.php");
  }else{
    echo "error";
  }
}
?>