2017-02-15 3 views
0

que je veux faire sous forme d'édition, la valeur déjà utilisateur est sélectionné des moyens que je veux afficher sous forme d'édition de la valeur de la case est cochée, alors je suis d'essayer comme çaComment faire la valeur de case cochée dans le tableau

<?php 
include("dbconfig.php"); 
$sql = "SELECT * FROM residential_propertytype WHERE status !='1'"; 
$result = mysql_query($sql); 
while($pr_type = mysql_fetch_array($result)) 
{ 
$propty[] = $pr_type; 

} 
echo "<pre>"; 
var_dump($propty); 
echo "</pre>"; 
foreach($propty as $res) 
     { 
     $checked = in_array($res['id'], 5) ? 'checked' : ''; 
     ?> 

     <input type="checkbox" value="<?php echo $res['id']?>" <?php echo $checked; ?>><?php echo $res['propertyName']?> 
     <?php 
     } 
?> 

Voir ci-dessous var_dump (de propty de $) Je reçois réponse comme ça

array(10) { 


[0]=> 
    array(8) { 
    [0]=> 
    string(1) "1" 
    ["id"]=> 
    string(1) "1" 
    [1]=> 
    string(9) "Apartment" 
    ["propertyName"]=> 
    string(9) "Apartment" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [1]=> 
    array(8) { 
    [0]=> 
    string(1) "2" 
    ["id"]=> 
    string(1) "2" 
    [1]=> 
    string(9) "Villament" 
    ["propertyName"]=> 
    string(9) "Villament" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [2]=> 
    array(8) { 
    [0]=> 
    string(1) "3" 
    ["id"]=> 
    string(1) "3" 
    [1]=> 
    string(16) "Individual House" 
    ["propertyName"]=> 
    string(16) "Individual House" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [3]=> 
    array(8) { 
    [0]=> 
    string(1) "4" 
    ["id"]=> 
    string(1) "4" 
    [1]=> 
    string(9) "Row House" 
    ["propertyName"]=> 
    string(9) "Row House" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [4]=> 
    array(8) { 
    [0]=> 
    string(1) "5" 
    ["id"]=> 
    string(1) "5" 
    [1]=> 
    string(5) "Villa" 
    ["propertyName"]=> 
    string(5) "Villa" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [5]=> 
    array(8) { 
    [0]=> 
    string(1) "6" 
    ["id"]=> 
    string(1) "6" 
    [1]=> 
    string(4) "Plot" 
    ["propertyName"]=> 
    string(4) "Plot" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [6]=> 
    array(8) { 
    [0]=> 
    string(1) "7" 
    ["id"]=> 
    string(1) "7" 
    [1]=> 
    string(13) "Builder Floor" 
    ["propertyName"]=> 
    string(13) "Builder Floor" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [7]=> 
    array(8) { 
    [0]=> 
    string(1) "8" 
    ["id"]=> 
    string(1) "8" 
    [1]=> 
    string(9) "Penthouse" 
    ["propertyName"]=> 
    string(9) "Penthouse" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [8]=> 
    array(8) { 
    [0]=> 
    string(1) "9" 
    ["id"]=> 
    string(1) "9" 
    [1]=> 
    string(10) "Farm House" 
    ["propertyName"]=> 
    string(10) "Farm House" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
    [9]=> 
    array(8) { 
    [0]=> 
    string(2) "10" 
    ["id"]=> 
    string(2) "10" 
    [1]=> 
    string(16) "Studio Apartment" 
    ["propertyName"]=> 
    string(16) "Studio Apartment" 
    [2]=> 
    string(0) "" 
    ["reg_date"]=> 
    string(0) "" 
    [3]=> 
    string(1) "0" 
    ["status"]=> 
    string(1) "0" 
    } 
} 

en allant rendre la valeur de case cochée J'obtiens l'erreur Avertissement: in_array() attend le paramètre 2 soit tableau, entier donné h omment résoudre ce problème

+0

Vérifiez http://php.net/manual/fr/function.in-array.php. Le deuxième paramètre devrait être le tableau. –

+0

L'avertissement vous indique comment résoudre ce problème. Vous devez passer un 'array', mais vous passez' 5' qui est un entier. – BizzyBob

+0

comment puis-je passer s'il vous plaît mettre à jour votre réponse –

Répondre

0

Vous devez définir, probablement ce que vous vouliez dire à propos 5 ..

Il devrait être comme: $ array_list = array (1, 2, 3, 4, 5);

$checked = in_array($res['id'], $array_list) ? 'checked' : ''; 

Sur votre exemple, vous passez 5 au lieu d'un array

0

Vous connaissez la valeur et la clé du tableau que vous voulez faire correspondre, pourquoi utiliser in_array()?

$checked = ($res['id'] == 5) ? 'checked' : ''; 
0

Dans votre code de formulaire HTML, la case à cocher (s) que vous voulez faire un tableau doivent tous être le même nom thign wtih un support à l'extrémité. De cette façon, les cases à cocher sélectionnées formeront un tableau dans le tableau $ _POST

<form method=post action=yourstuff.php> 
Pick your favorite pizza toppings - 
<input type=checkbox name=topping[] value="pepperoni"> Pepperoni <br /> 
<input type=checkbox name=topping[] value="ex. chz"> Extra cheese <br /> 
<input type=checkbox name=topping[] value="black olives"> Black olives <br /> 
<input type=submit> 
</form>