Précédent   Forum des professionnels en informatique > PHP > Outils
Outils Forum d'entraide sur les outils pour développeurs PHP : EDI, installation, administration... Avant de poster : FAQ outils, toutes les FAQ PHP et les comparatifs
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 16/03/2007, 13h32   #1
Membre éclairé
 
Avatar de hisy
 
Inscription : novembre 2004
Messages : 373
Détails du profil
Informations forums :
Inscription : novembre 2004
Messages : 373
Points : 300
Points : 300
Par défaut Générateur de classe

Re-Bonjour,

Chaque nouvelle table, une nouvelle classe et pas mal de saisie ...
Alors que pour ma part, la pluspart des classes sont structurées pareil ...

Alors avant que je me lance dans un script de génération du Classes depuis une table mySQL, je voudrais m'assurer que ça n'existe pas déjà en Free quelque part ...

Merci pour vos infos
__________________
Take it HiSy
hisy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/03/2007, 13h55   #2
Membre éclairé
 
Avatar de hisy
 
Inscription : novembre 2004
Messages : 373
Détails du profil
Informations forums :
Inscription : novembre 2004
Messages : 373
Points : 300
Points : 300
Oki, j'en ai trouvé un ...

Je test et je vous (me ?) tiens au courant ...

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
 
<?
class MysqlConnect {
	var $host; // Server Name
	var $user; // User Name
	var $password; // Password
	var $database; // DataBase Name
	var $idConnexion; // Connexion id
	var $isConnected; // True or False
	var $listdbArray; // Array
 
	var $table; // Default Table
 
	var $query;
	var $result;
	/********************************
	/  Constructor
	*********************************/
 
	function MysqlConnect() {
		$this->host = "";
		$this->user = "";
		$this->password = "";
		$this->database = "";
		$this->idConnexion = FALSE;
		$this->isConnected = FALSE;
		$this->listdbArray = array();
	}
 
	/********************************
	/  Connexion to MySQL Server
	*********************************/
 
	function doConnect () {
		if ($this->isConnected == FALSE) {
			$this->idConnexion = @mysql_connect($this->getHost(), $this->getUser (), $this->getPassword());
			if (!$this->idConnexion) {
		    		$this->MysqlConnexionErr();
				$this->isConnected = FALSE;
				return 0;
			}
 
			$selectDb = @mysql_select_db($this->getDatabase($this->idConnexion));
			if (!$selectDb) {
	    			$this->MysqlErr ('Unable to select this database : <b>'.$this->getDatabase().'</b>');
				$this->isConnected = FALSE;
				$this->closeDb ();
				return 0;
			}
		}
		$this->isConnected = TRUE;
		//return $this->idConnexion;
	}
 
	/********************************
	/ MySQL DataBase Lists
	*********************************/
 
	function MysqlListsDb () {
		if ($this->isConnected == TRUE) {
			$listsdb = @mysql_list_dbs($this->idConnexion);
			$dbNumRows = @mysql_num_rows($listsdb);
			if ($dbNumRows > 0) {
	    			//$this->printMsg ($dbNumRows);
				$i=0;
				while($i<$dbNumRows) {
					$this->listdbArray [$i] = @mysql_tablename ($listsdb, $i);
					print '<a href="'.$_SERVER["PHP_SELF"].'?db='.$this->listdbArray [$i].'">'.$this->listdbArray [$i].'</a><br>';
					$i++;
				}
			} else {
				$this->printMsg ('No database ...');
			}
			@mysql_free_result ($listsdb);
		}
	}
 
	/********************************
	/ MySQL DataBase Table Lists
	*********************************/
 
	function MysqlListsTable ($strDb='') {
		//var_dump(isset($_REQUEST['db']));
		if (isset($_REQUEST['db'])) {
			$listTable = mysql_list_tables($this->getDatabase(), $this->idConnexion);
			$tableNumRows = mysql_num_rows($listTable);
			if ($tableNumRows>0) {
				print '<hr>';
				print 'Nombre de table disponible dans la base <b>'.$this->getDatabase().' : '.$tableNumRows.'</b> : <b style="color:blue;">'.$row.'</b><br>';
				print 'Tables disponibles :<br>';
			    	$i=0;
				while($i < $tableNumRows) {
					#						#
					# Call Create Class Method to generate a class for each table in data base	#
					#						#
					//$this->create_class (mysql_tablename($listTable, $i));
					print '<ul><li><a href="'.$_SERVER["PHP_SELF"].'?db='.$_REQUEST['db'].'&table='.mysql_tablename($listTable, $i).'">'.mysql_tablename($listTable, $i).'</a></li></ul>';
					$i++;
				}
			} else {
				$this->printMsg ('Aucune table n\'est disponible dans cette base de donnée...');
			}
			@mysql_free_result ($listTable);
		}
	}
 
	/********************************
	/ MySQL DataBase Table Fields
	*********************************/
	function MysqlListsFields () {
		if (!$this->table == '') {
			$qry = $this->MysqlSelect ($this->getTable());
			$rs = $this->MysqlQuery ($qry);
			$row = @mysql_num_fields($rs);
			$nbCol = 3;
			$resteRowCol = $row % $nbCol;
			$nbLigne = ($row - $reste) / $nbCol;
			print $resteRowCol. ' => '.$nbLigne;
			if ($row > 0) {
				print '<hr>';
				print 'Nombre de champ disponible dans la table <b>'.$this->getTable().'</b> : <b style="color:blue;">'.$row.'</b><br>';
				print 'Champs disponibles :<br>';
				$i=0;
				$j=1;
				print '<table border=0>';
				while($obj = mysql_fetch_field ($rs)) {
 
					print ' <tr>';
					print '  <td>'.$obj->name.'</td>';
					if (($i+$j) == 3) {
						print ' </tr>';
					} else {
 
					print ' </tr>';
 
					//print '<ul><li> '.$j.' name : '.$obj->name.' =>  type : ' $obj->type.' =>  length : '. mysql_field_len ($rs, $i).'</li></ul>';
					$i++;
					$j++;
					}
				}
				print '</table>';
				print '<hr>';
			} else {
				$this->printMsg ('Aucun champ n\'est disponible dans cette table...');
			}
			@mysql_free_result ($rs);
		}
	}
 
 
	/********************************************************************************
	* Cette fonction crée automatiquement une class php
	* Donnez un nom de table du base donné MySQL
	* et le tour est joué....
	* Amusez vous bien :-)
	*********************************************************************************/
	# Method Create Class
	function create_class ($table_name='') {
		if (!empty($table_name)) {
			$out = '';
			$out .= '';
			$out = '&#60&#63'; // < ?
			$out .= "<br>";
			$out .= "#File Name : cls_".$table_name.".php";
			$out .= "<br>";
			$out .= "#Date : ".date('Y-m-d - H:m:s');
			$out .= "<br>";
			$out .= "#Author : Coumarane COUPPANE";
			$out .= "<br>";
			$out .= "<br>";
			$out .= 'class '.$table_name.' {';
			$out .= "<br>";
			$query = $this->MysqlSelect ($table_name);
			$result = $this->MysqlQuery ($query);
			$num_rows = mysql_num_fields ($result);
			$i=0;
			if ($num_rows > $i) {
				$num_fields = mysql_num_fields ($result);
				# Variables
				for ($k=0; $k<$num_fields; $k++) {
					$k_fields =  mysql_fetch_field ($result, $k);
					if ($k_fields->primary_key == 1) {
						$idtab = $k_fields->name;
					}
					$out .= 'var $'.$k_fields->name.';';
					$out .= "<br>";
				}
 
				# Construct
				$out .= "<br>";
				$out .= "# Construct";
				$out .= "<br>";
				$out .= 'function '.$table_name. ' ()  {';
				$out .= "<br>";
				for ($k=0; $k<$num_fields; $k++) {
					$k_fields =  mysql_fetch_field ($result, $k);
					if ($k_fields->primary_key == 1) {
						$idtab = $k_fields->name;
					}
					$out .= '$this->'.$k_fields->name.' = "";';
					$out .= "<br>";
				}
				$out .= '}';
				$out .= "<br>";
 
				# Property SET
				$out .= "<br>";
				$out .= '# Property SET';
				$out .= "<br>";
				for ($j=0; $j<$num_fields; $j++) {
					$fields =  mysql_fetch_field ($result, $j);
					$out .= "<br>";
					$out .= 'function set_'.$fields->name. ' ($'.$fields->name.') {';
					$out .= "<br>";
					$out .= '$this->'.$fields->name.' = $'.$fields->name.';';
					$out .= "<br>";
					$out .= '}';
					$out .= "<br>";
				}
 
				# Property GET
				$out .= "<br>";
				$out .= '# Property GET';
				$out .= "<br>";
				for ($l=0; $l<$num_fields; $l++) {
					$l_fields =  mysql_fetch_field ($result, $l);
					$out .= "<br>";
					$out .= 'function get_'.$l_fields->name. ' () {';
					$out .= "<br>";
					$out .= 'return $this->'.$l_fields->name.';';
					$out .= "<br>";
					$out .= '}';
					$out .= "<br>";
				}
 
				#Function Insert Start ##################################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Insert';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_insert &#40&#41 &#123'; // &#123:{ &#40:( &#41:)
				$out .= '<br>';
				$out .= '$query = "INSERT INTO '.$table_name.' (<br>';
				$z = 1;
				for ($j=0; $j<$num_fields; $j++) {
					$fields =  mysql_fetch_field ($result, $j);
					if ($z == $num_fields) { $virgule = ''; } else {$virgule = ',';}
					$out .= $fields->name.$virgule.'<br>';
					//$out .= 'get_'.$fields->name;
					$z++;
				}
				$out .= ') VALUES (<br>';
				$w = 1;
				for ($j=0; $j<$num_fields; $j++) {
					$fields =  mysql_fetch_field ($result, $j);
					if ($w == $num_fields) { $virgule = ''; } else {$virgule = ',';}
					$out .= '\'".$this->get_'.$fields->name.' ()."\''.$virgule.'<br>';
					$w++;
				}
				$out .= ')";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Insert End ################################
 
				#Function Select Start #################################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Select';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_select &#40&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "SELECT * FROM '.$table_name.'";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Select End #################################
 
				#Function Fields Select Start #################################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Fields Select';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_fields_select &#40$fieldName, $data&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "SELECT * FROM '.$table_name.' WHERE ".$fieldName." = \'".$data."\'";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Fields Select End #################################
 
				#Function Update Start ###############################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Update';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_update &#40&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "UPDATE '.$table_name.' SET<br>';
				$y = 1;
				for ($j=0; $j<$num_fields; $j++) {
					$fields =  mysql_fetch_field ($result, $j);
					if ($y == $num_fields) { $virgule = ''; } else {$virgule = ',';}
					$out .= $fields->name.' = \'".$this->get_'.$fields->name.'( )."\''.$virgule.'<br>';
					$y++;
				}
				$out .= '";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Update End ##################################
 
				#Function Fields Update Start #############################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Fields Update';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_fields_update &#40$fieldName, $data&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "UPDATE '.$table_name.' SET ".$fieldName." = \'".$data."\'";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Fields Update End ##############################
 
				#Function Delete Start #################################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Delete';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_delete &#40&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "DELETE FROM '.$table_name.'";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				#Function Delete End #################################
 
				#Function Fields Delete Start #################################
				$out .= '<br>';
				$out .= '<br>';
				$out .= '# Function Fields Delete';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_fields_delete &#40$fieldName, $data&#41 &#123'; // &#123:{   &#40:(    &#41:)
				$out .= '<br>';
				$out .= '$query = "DELETE FROM '.$table_name.' WHERE ".$fieldName." = \'".$data."\'";';
				$out .= '<br>';
				$out .= '$result = mysql_query ($query);';
				$out .= '<br>';
				$out .= 'if (!$result) { return false;}';
				$out .= '<br>';
				$out .= 'return $result;';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				#Function Fields Delete End #################################
 
				#Function Table Nb Records Start ##############################
				$out .= '<br>';
				$out .= '# Function Table Nb Records';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_num_rows () {';
				$out .= '<br>';
				$out .= '	$result = $this->'.$table_name.'_select ();';
				$out .= '<br>';
				$out .= '	if (!$result) {';
				$out .= '<br>';
				$out .= '		$this->err_mysql () ;';
				$out .= '<br>';
				$out .= '		return false;';
				$out .= '<br>';
				$out .= '		}';
				$out .= '<br>';
				$out .= '		if (($rows = mysql_num_rows ($result)) < 0) {';
				$out .= '<br>';
				$out .= '		return false;';
				$out .= '<br>';
				$out .= '		}';
				$out .= '<br>';
				$out .= '	return $rows;';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				#Function Table Nb Records End ###############################
 
				# Function Table Loop Start  #################################
				$out .= '<br>';
				$out .= '# Function Table Loop';
				$out .= '<br>';
				$out .= 'function '.$table_name.'_loop () {';
				$out .= '<br>';
				$out .= '$result = $this->'.$table_name.'_select ();';
				$out .= '<br>';
				$out .= 'if (!$result) {';
				$out .= '<br>';
				$out .= '$this->err_mysql () ;';
				$out .= '<br>';
				$out .= 'return false;';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				$out .= '$rows = $this->'.$table_name.'_num_rows ();';
				$out .= '<br>';
				$out .= 'if ($rows > 0) {';
				$out .= '<br>';
				$out .= 'while ($obj = mysql_fetch_assoc ($result)) {';
				$out .= '<br>';
				$out .= '$tab_content [] = $obj;';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				$out .= 'return $tab_content; ';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				$out .= '}';
				$out .= '<br>';
				# Function Table Loop End  ##################################
 
				#Function Err MySQL Start #################################
				$out .= '<br>';
				$out .= '# Function Err MySQL';
				$out .= '<br>';
				$out .= 'function err_mysql () {';
				$out .= '<br>';
				$out .= '	print "Error : ".mysql_error(). " File : ".__FILE__;';
				$out .= '<br>';
				$out .= '}';
				#Function Err MySQL End #################################
 
				$out .= '<br>';
				$out .= '&#125'; // &#125:}
				$out .= '<br>';
				$out .= '&#63&#62'; // &#63:? // &#62:>
				$out .= '<br>';
				print $out;
			} else {
				print 'Nothing to display...';
			}
			mysql_free_result ($result);
		}
	}
 
	/********************************
	/  MySql Select
	*********************************/
	function MysqlSelect ($tablename, $idname='', $id='') {
		if ($idname != '' AND $id != '') {
			$this->query = 'SELECT * FROM '.$tablename.' WHERE '.$idname.' = "'.$id.'"';
		} else {
			$this->query = 'SELECT * FROM '.$tablename;
		}
		return ($this->query);
	}
 
 
	/********************************
	/  MySql Query
	*********************************/
 
	function MysqlQuery () {
		$this->result = @mysql_query ($this->query);
		if (!$this->result) {
	    		$this->MysqlErr('Vérifiez la requête : '.$this->query.'<br>');
			return 0;
		}
		return ($this->result);
	}
 
	/********************************
	/  Print message
	*********************************/
 
	function printMsg ($strMsg) {
		print '<font style="font-family:arial, helvetica; font-size:12px; color:blue;">';
		print $strMsg.'<hr>';
		print '</font>';
	}
 
	/********************************
	/  Close MySQL Server Connexion
	*********************************/
 
	function closeDb () {
		if ($this->isConnected == TRUE) {
		    	@mysql_close($this->idConnexion);
		}
		$this->isConnected = FALSE;
	}
 
	/********************************
	/  MySQL Server Connexion Erreur
	*********************************/
	function MysqlConnexionErr () {
		print '<font style="font-family:arial, helvetica; font-size:12px; color:red">';
		print 'Unable to connect to the SERVER : <b>'.$this->getHost().'</b><br>';
		print '</font>';
	}
 
	/********************************
	/  MySQL Server Erreur
	*********************************/
	function MysqlErr ($msg) {
		print '<font style="font-family:arial, helvetica; font-size:12px; color:red">';
		print $msg.'<hr>';
		print 'Erreur SQL : '.@mysql_error($this->idConnexion).'<br>';
		print 'Erreur no SQL : '.@mysql_errno($this->idConnexion).'<br>';
		print '</font>';
	}
 
	/********************************
	/  Property SET
	*********************************/
 
	function setHost ($strHost='') {
		if (empty($strHost)) {
			$this->host = 'localhost';
		} else {
			$this->host = $strHost;
		}
	}
 
	function setUser ($strUser='') {
		$this->user = $strUser;
	}
 
	function setPassword ($strPass='') {
		if (empty($strPass)) {
			$this->password = '';
		} else {
			$this->password = $strPass;
		}
	}
 
	function setDatabase ($strDbase='') {
		if (empty($strDbase)) {
			$this->database = 'mysql';
		} else {
			$this->database = $strDbase; 
		}
	}
 
	function setTable ($table='') {
		if (isset($_REQUEST['table'])) {
			$this->table = $table;
		} else {
			$this->table = '';
		}
		/*
		if (empty($table)) {
			$this->table = 'user';
		} else {
			$this->table = $table; 
		}
		*/
	}
 
 
	/********************************
	/  Property GET
	*********************************/
 
	function getHost () {
		return $this->host;
	}
 
	function getUser () {
		return $this->user;
	}
 
	function getPassword () {
		return $this->password;
	}
 
	function getDatabase () {
		return $this->database;
	}
 
	function getTable () {
		return $this->table;
	}
}
?>
__________________
Take it HiSy
hisy est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 21h17.


 
 
 
 
Partenaires

Hébergement Web