dongwei1234 2017-02-25 09:35
浏览 77
已采纳

Php:如何将已保存在表格中的值显示在页面上作为选中状态?

I have a table in my app, contacts, where a user (user_id) has a list of contacts :

contact_auto_inc      user_id       contact_id
17                       2             7
18                       2             8
19                       2             9

I show these contacts, their corresponding names, with this code :

<form action="" method="POST">
    <?php
         //this code below will get the username of contacts
         // for $user_id. we get the 'contact_id'
         //values in the contacts table for $user_id, match those contact_ids to    the corresponding 
         //'user_ids' in the user table, and then show the 'usernames' for each of those user_ids
           $select_from_user_table = "SELECT  contacts.contact_id, user.username
         FROM contacts 
         INNER JOIN user
         ON contacts.contact_id=user.user_id WHERE contacts.user_id = '$user_id'";

        //get the result of the above
         $result2=mysqli_query($con,$select_from_user_table);

    //show the usernames, phone numbers
         while($row = mysqli_fetch_assoc($result2)) { ?>
         <input type='checkbox' name='check_contacts[]' value='<?=$row['contact_id']?>'> <?php echo $row['username'] ?> </br>

        <?php } ?>

    <!--<input type="submit" name = "create" value = "Create new Contact"></p> -->

    <!--</form> -->

<p><input type="submit" name = "Save" value = "Save"></p>
<p><input type="submit" name = "Delete" value = "Delete"></p>
<a href="exit.php">Exit</a>
</form>

So it looks like this :

image.

And if one of the boxes is checked and then saved that contact gets saved to a review_shared table like this :

<?php
//here we want to save the checked contacts to the review_shared table ;  that is,
//who the user wants to share reviews with
if(!empty($_POST['check_contacts'])) {
    foreach($_POST['check_contacts'] as $check) {

    $insert_review_shared_command = "INSERT INTO review_shared VALUES(NULL," .$_GET['id']. ", '$user_id','$check')";

        //we want to save the checked contacts into the review_shared table
        $insert_into_review_shared_table = mysqli_query($con,$insert_review_shared_command);

    }

}
$con->close();
?> 

But whenever I go back to the page, I still see :

enter image description here

How would I show contacts from the contacts table that are also in the review_shared table with a check in the corresponding check box ?

  • 写回答

1条回答 默认 最新

  • dongmeba4877 2017-02-25 09:44
    关注

    First get the data from review_shared and compare inside while if matches check else unchecked: Add checked="checked" to check-boxes inside while to check it.

         <?php
          $review_shared=array(1,2,3,4);//get contact_id in this from shared table
         while($row = mysqli_fetch_assoc($result2)) {
            if(in_array($row['contact_id'],$review_shared)){ ?>
                 <input type='checkbox' name='check_contacts[]' value='<?=$row['contact_id']?>' checked="checked"> <?php echo $row['username'] ?> </br>
             <?php  }else{?>
            <input type='checkbox' name='check_contacts[]' value='<?=$row['contact_id']?>' > <?php echo $row['username'] ?> </br>           
       <?php }}?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制