Welcome Guest, Not a member yet? Register   Sign In
Helper or View for a Dynamic Form Menu?
#1

[eluser]Solarpitch[/eluser]
Ello again,

On the left of my application I plan on having a form with a number of checkboxes for a search. This search wil be on a number of pages. Ideally Id's like the check boxes to be database driven so the client can add another checkbox to the form from the backend. Just wondering how I can achieve this.

Here's an example of the form...

Code:
<form name="" method="" action="">    
<ul>

<li><label>
&lt;input type="checkbox" name="item_1" value="1" /&gt;
Item 1</label></li>
    
<li><label>
&lt;input type="checkbox" name="item_2" value="2" /&gt;
Item 2</label></li>
    
<li><label>
&lt;input type="checkbox" name="item_3" value="3" /&gt;
Item 3</label></li><label>
    
<li><label>
&lt;input type="checkbox" name="item_4" value="4" /&gt;
Item 4</label></li>


</ul>
&lt;/form&gt;

Whats the best way to do this? I take it the <li> will be generated via a loop after a query on the DB. Should the query be in my model and then I can pass it to my controller?

Just a little lost.
#2

[eluser]Solarpitch[/eluser]
I've made an attempt at this myself. If someone could let me know if this is along the right lines Smile

MODEL...

Code:
function search_menu()
{
    
$query = $this->db->query('SELECT * FROM search_menu');
return $query->result_array();
    
}

CONTROLLER...

Code:
function products()
{
$this->load->view('template/header');
        
$data['query'] = $this->product_model->get_products();

$data['form'] = $this->load->view('menus/left_search', $data);
        
$this->load->view('pages/products', $data);
$this->load->view('template/footer');
}

VIEW (MENU/LEFT_SEARCH)...

Code:
&lt;form name="" method="" action=""&gt;    
<ul>

foreach($form as $row)
{
<li>&lt;input type="checkbox" name="$row-&gt;name" value="$row->value" /></li>
}

</ul>
&lt;/form&gt;
#3

[eluser]Pr0v4[/eluser]
Hey fellows!

My problem is similar but with a little difference, I don't understand how I could insert in the database this kind of form...

Few lines of code are always better than thousand of words Big Grin


Code:
//Controller

function run_test($id){
  $data = array();
  $test = $this->tests_model->get_record($id);  
    
  //Check if the variable contain something
  if(!count($test)){
    redirect('tysk/index', 'refresh');
  }                                  
    
  $data['main_content'] = 'inc/run_test';
  $data['test'] = $test;
  $data['categories'] = $this->categories_model->get_records();
  $data['questions'] = $this->quest_model->get_records();
  $this->load->vars($data);
  $this->load->view('index');
}

//View

&lt;?= form_open('/ans/insert'); ?&gt;
     &lt;?php foreach($questions as $quest) : ?&gt;      
        <div class="visible">
            <h5><span>Question_&lt;?= ++$i; ?&gt;</span>&lt;?= $quest['question']; ?&gt;</h5>
                &lt;textarea class="answer" name="&lt;?= 'answer-'.$quest['id']; ?&gt;"&gt;&lt;/textarea>
                    &lt;input type="hidden" name="quest_id" value="&lt;?= $quest['id']; ?&gt;" /&gt;
                <br />
                <a id="next" href="#&lt;?php echo $i; ?&gt;" title="Next Question" class="sexybutton sexyblue"><span><span>Next Question</span></span></a>
        </div>
    &lt;?php endforeach; ?&gt;        
    <p>Congratulation you finish the test!</p>
        &lt;input type="hidden" name="index" value="&lt;?= $i; ?&gt;" /&gt;
    <button class="sexybutton sexyblue" type="submit"><span><span>Send Your Test</span></span></button>
    &lt;?= validation_errors('<p class="error">'); ?&gt;
&lt;?= form_close(); ?&gt;
My intention are to create a kind of application where you can run some test, and save your answer in a database...

The thing that make so hard the realization is because each test has a different number of questions, and relatively answers... and when I find myself to insert the inputs, I don't understand how to move...

Can someone help me?

If you need more info I can leave more information! For example could be helpful a diagram of my db?!?

If someone wanna have a look click here.




Theme © iAndrew 2016 - Forum software by © MyBB