Résultat requête au format JSON, afficher que les champs vide ou null
Hello,
J'essai de récupérer et d'afficher au format JSON que les champs vides ou NULL de ma DB.
Pour le moment tout les champs s'affiche :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
error_reporting(E_ALL);
require_once 'JSON.php';
$connect = mysql_connect('localhost','root','root') or die ("erreur de connexion");
mysql_select_db('xxxxx', $connect) or die ("erreur de connexion base");
$sqlData = "SELECT * FROM ui9me_users WHERE id = 442";
$result = mysql_query($sqlData);
$index = 0;
while ($index < mysql_num_fields($result))
{
$mysql = array();
$mysql['name'] = mysql_field_name($result, $index); // Name of the field
$mysql['type'] = mysql_field_type($result, $index); // Type of the field
$field = array();
$field['name'] = $mysql['name'];
$field['fieldLabel'] = ucfirst($mysql['name']);
switch ($mysqlField['type'])
{
case "int":
$field['xtype'] = 'numberfield';
break;
case "date":
$field['xtype'] = 'datefield';
break;
default:
$field['xtype'] = 'textfield';
break;
}
$data['items'][] = $field;
$index++;
}
while ($user = mysql_fetch_assoc($result))
{
$data['data'] = $user;
}
echo json_encode($data); |
J'obtient :
Code:
{"items":[{"name":"id","fieldLabel":"Id","xtype":"textfield"},{"name":"name","fieldLabel":"Name","xtype":"textfield"},{"name":"username","fieldLabel":"Username","xtype":"textfield"},{"name":"email","fieldLabel":"Email","xtype":"textfield"},{"name":"password","fieldLabel":"Password","xtype":"textfield"},{"name":"block","fieldLabel":"Block","xtype":"textfield"},{"name":"sendEmail","fieldLabel":"SendEmail","xtype":"textfield"},{"name":"registerDate","fieldLabel":"RegisterDate","xtype":"textfield"},{"name":"lastvisitDate","fieldLabel":"LastvisitDate","xtype":"textfield"},{"name":"activation","fieldLabel":"Activation","xtype":"textfield"},{"name":"params","fieldLabel":"Params","xtype":"textfield"},{"name":"lastResetTime","fieldLabel":"LastResetTime","xtype":"textfield"},{"name":"resetCount","fieldLabel":"ResetCount","xtype":"textfield"}],"data":{"id":"442","name":"","username":"admin","email":"xxxxx@mac.com","password":"a626c1e9cc18076ddd985a406b3426b7:ySmfoUtHkiTt8fo5Jr9aURAWta0uSDJH","block":"0","sendEmail":"1","registerDate":"2013-03-13 21:17:57","lastvisitDate":"2013-03-21 07:08:00","activation":"0","params":"","lastResetTime":"0000-00-00 00:00:00","resetCount":"0"}}
Alors que je cherche à avoir comme résultat : ( car dans ma base le champ "name" est vide )
Code:
{"items":[{"name":"name","fieldLabel":"Name","xtype":"textfield"}],"data":{"name":""}}
Le but et de traiter les données JSON avec un formulaire Extjs par la suite. ( d'ou le premier while pour récupérer nom du champ, type...)
J'ai essayé d'ajouter une condition dans les boucles while mais...
Merci.