Yii Framework Forum: Konversi Char ke Number - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Konversi Char ke Number Rate Topic: -----

#1 User is offline   fajar aji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 17-July 12
  • Location:Jakarta

Posted 02 September 2012 - 12:42 AM

Hi, minta bantuan lagi ni gan,

cara konversi dari Char ke Number gimana ya?. Kasusnya begini ga :

ane punya field namanya id_number, tipe datanya char(3), ane mau buat id number ni jadi auto_increment, jadi setiap kali ane nambah data, id_number ni otomatis nambah, misalnya ni

---------
id_number
---------
001
002
003
...dst
---------

$sql = "SELECT id_number FROM tableX";
$id = Yii::app()->db->createCommand($sql)->queryAll();

if ($id === null) {
    $model->id_number = '001';
} else {
    $sql2 = "SELECT MAX(id_number) FROM tableX";
    $number = Yii::app()->db->createCommand($sql2)->queryAll();

    foreach($number as $id_number) {
            $model->id_number = $id_number+1;
    }
}
$model->save();


o iya gan, sebagai informasi saja, ane gunain database postgre. mohon bantuannya ya gan. thankz sebelumnya
0

#2 User is offline   mbahsomo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 29-March 10

Posted 02 September 2012 - 11:39 PM

1. Coba di query dulu.
Select max(id_number) as max from tatble_name;

2. Ambil nilai max.
3. Kalo sudah dapat tinggal di olah deh.
$hasil = "";
for ($a = strlen("max_query") ; $a < 3; $a++) {
  $hasil = "0" . $hasil;
}

sederhana kan ;D .

Merdeka.
Sugik Puja Kusuma [mbahsomo]
http://sugik.do-event.com
0

#3 User is offline   fajar aji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 17-July 12
  • Location:Jakarta

Posted 03 September 2012 - 02:52 AM

View Postmbahsomo, on 02 September 2012 - 11:39 PM, said:

1. Coba di query dulu.
Select max(id_number) as max from tatble_name;

2. Ambil nilai max.
3. Kalo sudah dapat tinggal di olah deh.
$hasil = "";
for ($a = strlen("max_query") ; $a < 3; $a++) {
  $hasil = "0" . $hasil;
}

sederhana kan ;D .

Merdeka.


mantab gan,

success....thanks y gan. ::)
0

#4 User is offline   mbahsomo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 29-March 10

Posted 04 September 2012 - 12:44 AM

Siap mas bro.
Sugik Puja Kusuma [mbahsomo]
http://sugik.do-event.com
0

#5 User is offline   siskalandre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 92
  • Joined: 16-March 11
  • Location:Indonesia

Posted 10 September 2012 - 03:36 AM

Hi gan

kenapa ga diubah format field jadi autoincrement gan, biar ga repot gitu gan ?
soalnya ane pernah coba model gitu, kalau usernya sudah banyak, dan akses ke aplikasi yg sama, bisa 'crash' rebutan nomor.


View Postfajar aji, on 02 September 2012 - 12:42 AM, said:

Hi, minta bantuan lagi ni gan,

cara konversi dari Char ke Number gimana ya?. Kasusnya begini ga :

ane punya field namanya id_number, tipe datanya char(3), ane mau buat id number ni jadi auto_increment, jadi setiap kali ane nambah data, id_number ni otomatis nambah, misalnya ni

---------
id_number
---------
001
002
003
...dst
---------

$sql = "SELECT id_number FROM tableX";
$id = Yii::app()->db->createCommand($sql)->queryAll();

if ($id === null) {
    $model->id_number = '001';
} else {
    $sql2 = "SELECT MAX(id_number) FROM tableX";
    $number = Yii::app()->db->createCommand($sql2)->queryAll();

    foreach($number as $id_number) {
            $model->id_number = $id_number+1;
    }
}
$model->save();


o iya gan, sebagai informasi saja, ane gunain database postgre. mohon bantuannya ya gan. thankz sebelumnya

0

#6 User is offline   fajar aji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 17-July 12
  • Location:Jakarta

Posted 10 September 2012 - 11:42 PM

View Postsiskalandre, on 10 September 2012 - 03:36 AM, said:

Hi gan

kenapa ga diubah format field jadi autoincrement gan, biar ga repot gitu gan ?
soalnya ane pernah coba model gitu, kalau usernya sudah banyak, dan akses ke aplikasi yg sama, bisa 'crash' rebutan nomor.


oh gitu ya gan, thankz infonya...
munkin buat project selanjutnya bisa ane praktekin...
0

#7 User is offline   mbahsomo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 29-March 10

Posted 12 September 2012 - 11:27 PM

Setuju dengan semuanya.
cuman yang tergantung kebutuhan dan kondisi
Sugik Puja Kusuma [mbahsomo]
http://sugik.do-event.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users