CodeIgniter Forums

Full Version: [HELP] Form Generation Library (radiogroup )
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ci_mohammed[/eluser]
hi all Smile

this is my first Topic in the forum , i hope to find some help ,

can some one tell me how to use radiogroup in Form Generation Library.

pls some one tell me how to use it in controller and model .

i trying to use it but i faced some problem .

here is what i do
in model

Code:
$news->isInMain = $data['isInMain'];
$news->isactive = $data['isActive'];
$news->can_comment = $data['can_comment'];

in controller
Code:
$isInMain[] = array(1, 'yes');
$isInMain[] = array(0, 'no');

$isActive[] = array(1, 'yes');
$isActive[] = array(0, 'no');

$can_comment[] = array(1, 'yes');
$can_comment[] = array(0, 'no');

$this->form->radiogroup('isInMain', $isInMain, 'isInMain:', '1', 'required')->br();
$this->form->radiogroup('isActive', $isActive, 'isActive:', '1', 'required')->br();
$this->form->radiogroup('can_comment', $can_comment, 'comment:', '1','required')->br();


this it error msgs that i faced
-------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Undefined index: isInMain

Filename: models/News.php

Line Number: 29
------------------------------------

------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Undefined index: isActive

Filename: models/News.php

Line Number: 30

------------------------------------

------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 552

------------------------------------


can some one tell me the problem ..

or to tell me how to use it step by step , and i will be very grateful .

El Forum

[eluser]S-Vizion Software and Development[/eluser]
Could you post some more of your code?

A undefined index means your array does not have a key named whatever your error is try something like below

Code:
// Type Cast Variable Data as a Array
$data = (array) $data;

// First check there is a Array Key named 'isInMain' and then check it's not empty
if (array_key_exists('isInMain', $data) && !empty($data['isInMain'])
{
  // Set Object Key to the Data
  $news->isInMain = $data['isInMain'];
}

if (array_key_exists('isActive', $data) && !empty($data['isActive'])
{
  $news->isInMain = $data['isActive'];
}

if (array_key_exists('can_comment', $data) && !empty($data['can_comment'])
{
  $news->can_comment = $data['can_comment'];
}

I personally use a helper function myself that I wrote along time ago to verify that the variable I'm checking is set, not empty, etc to reduce having to write so much extra code checking.

Hopefully that will help you get on the correct track.

El Forum

[eluser]InsiteFX[/eluser]
Try removing the colon!
Code:
'isInMain:'

El Forum

[eluser]ci_mohammed[/eluser]
mmm the problem is how can i pass the checked value of radiogroup to model ??

then model insert it to database .

in controller
Code:
function addform()
{
  $this->form->open();

                $isInMain[] = array(1, 'yes');
                $isInMain[] = array(0, 'no');

                $isActive[] = array(1, 'yes');
                $isActive[] = array(0, 'no');

                $can_comment[] = array(1, 'yes');
                $can_comment[] = array(0, 'no');
  

  $this->form->fieldset('add');

  $this->form->radiogroup('isInMain', $isInMain, 'isInMain:', '1', 'required')->br();
  $this->form->radiogroup('isActive', $isActive, 'isActive:', '1', 'required')->br();
  $this->form->radiogroup('can_comment', $can_comment, 'can_comment:', '1', 'required')->br();
  
  $this->form->submit('save');
  $this->form->model('news','add_update');
  $data['form'] = $this->form->get();

  if($this->form->valid)
  {
   onsuccess('redirect', 'success');
  }
  else
  {
   $data['errors'] = $this->form->errors;
  }
  

  $this->load->view("addform", $data);
}


in model news.php
Code:
function add_update( &$form, $data)
{
  echo $data['isInMain'];
  echo $data['isActive'];
  echo $data['can_comment'];
}

in view addform.php

Code:
<?php echo $errors ?>
<?php echo $form ?>


what i need is when i checked yes in isInMain , and press save button ,
result = 1 .
but it is not hapend.

El Forum

[eluser]ci_mohammed[/eluser]
--------------------
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 552

---------------------


database
Code:
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `news` (`isInMain`, `isActive`, `can_comment`) VALUES (Array, Array, Array)

Filename: C:\wamp\www\news\system\database\DB_driver.php

Line Number: 330

i should be
Code:
INSERT INTO `news` (`isInMain`, `isActive`, `can_comment`) VALUES (1, 1, 1)

but the value not pass