|
 |
|
ผมจะสร้างหน้า create table ไม่รู้จะใช้คำสั่ง create table ยังไงเพราะข้อมูลที่ได้มาจะต้องลูปออกมา |
|
 |
|
|
 |
 |
|
ลองทำเป็น อาเรย์หลายมิติดูสิคับ
$table = array('col_name' => array('type'=>'int', 'length'=>11, 'null'= FALSE));
ตย. เท่านั้นนะครับ ลองออกแบบดีๆ คงพอจะจัดการง่ายคับ
|
 |
 |
 |
 |
Date :
2010-07-19 21:04:59 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
for($x=1;$x<=$nofield;$x++){
for($i=1;$i<=1;$i++){
echo "$field[$x]";
echo "$type[$x]";
echo "$set[$x]";
echo "$autonum[$x]";
echo "$pk[$x]";
}
}
ตัวด้านบนเมื่อกี้ผมก็อปมาผิดครับ แต่ก็ยังงง ไม่รู้จะทำยังไงคือจะทำเหมือนหน้าของ phpmyadmin ในขั้นตอนการสร้างฐานข้อมูลตอนนี้กำหนดได้แล้วว่าต้องการกี่ field ก็ได้ แต่ติดตรงตอนที่จะใช้คำสั่ง create table นะสิ ก็เลยลอง echo ออกมาดูเฉยๆ
|
 |
 |
 |
 |
Date :
2010-07-19 21:19:57 |
By :
benservice |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้การต่อสตริงคับ
ตัวอย่าง (ผมไม่ถนัด พวก sql เท่าไร)
Code (PHP)
$tables = array(
'table1' => array(
'cols' => array(
'col1' => array(
'type' => 'int',
'null' => FALSE,
'length' => 11,
'optional' => array(
'primary' => TRUE,
'unique' => FALSE,
'autoincrement' => TRUE,
'key' => FALSE,
),
),
'col2' => array(
'type' => 'varchar',
'null' => TRUE,
'length' => 200,
'optional' => array(
'primary' => TRUE,
'unique' => TRUE,
'autoincrement' => FALSE,
'key' => TRUE,
),
),
),
),
);
foreach ($tables as $name => $table)
{
$table_name = $name;
$sql = "CREATE TABLE '{$table_name}' (";
$primary_keys = array();
$unique_keys = array();
// concate cols
foreach ($table['cols'] as $name => $col)
{
$col_name = $name;
$col_type = $col['type'];
$col_null = $col['null'] ? 'default null' : 'not null';
$col_length = $col['length'];
$col_autoincrement = '';
if ($col['optional']['primary'] === TRUE)
{
$primary_keys[] = $col_name;
if ($col['optional']['autoincrement'] === TRUE)
{
$col_autoincrement = 'auto_increment';
}
}
if ($col['optional']['unique'] === TRUE)
{
$unique_keys[] = $col_name;
}
$sql .= "'{$col_name}' {$col_type}({$col_length}) {$col_null} {$col_autoincrement}, ";
}
foreach ($primary_keys as $pri)
{
$sql .= "PRIMARY KEY ('{$pri}'), ";
}
foreach ($unique_keys as $unique)
{
$sql .= "UNIQUE KEY '{$unique}' ('{$unique}'), ";
}
// ขอจบก่อนละกัน ยังมีอย่างอื่นอีก ทำเองนะคับ
$sql .= ')';
echo $sql, '<br><br>';
// query create table here
}
http://gunner.freetzi.com
|
 |
 |
 |
 |
Date :
2010-07-19 22:31:26 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|