• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
submit multiple inputs within a forloop in codeigniter

#1
My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :

Model is :

public function get_questions(){

 $this->db->select('id,user_name, question, date');
 $this->db->order_by("id", "desc");
 $query=$this->db->get('t_questions');
 return $query->result();

}

My view is:

foreach ($questions as $id => $row) :
           ?>
       <?php

               echo "<h5>".$row->question;
               echo "<br>";

               echo "from : ".$row->user_name."</h5>";
               echo date('Y-m-d H:i');
               echo "<br>";
               $q_no='save'.$row->id;
               $ans_no='answer'.$row->id;
               echo  "<h4> Answer:</h4>";
               echo form_open('control_panel');
               ?>
               <textarea name='<?php echo 'answer'.$row->id; ?>'  value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
               <input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
               <input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
               <?php
               echo form_error($ans_no);
               echo "              
           <div class='form-group'>
                           <div >
                               <label class='checkbox-inline'>

                               <input type='checkbox' name='add_faq' value='yes' />
                                     Adding to FAQ page .
                               </label>
                           </div>
                       </div>




         <p>";

          ?>
         <input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
           <?php
           echo 'answer'.$row->id;
           ?>
           <hr>

           <?php endforeach; ?>

and my controller is :

$this->load->model('control_panel');
   $data['questions']=$this->control_panel->get_questions();
   $data['no_of_questions']=count($data['questions']);



   if($this->input->post($q_no))
   {

     $this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');  
       if($this->form_validation->run())
     {  

     /* code to insert answer in database */
     }

   }

of course it did not work with me : i get errors :

       Severity: Notice

   Message: Undefined variable: q_no

i do not know how to fix it

I am using codeigniter as i said in the headline.



to explain exactly what i want to do : it is supposed for users to ask the admin , so their questions are saved t_questions table in the database , then i got them in the view by a foreach loop and i wrote a code under each fetched question for a textbox to let the admin answer each question do to validate each answer through the validation rules i differ each answer by the id of the question which is a column in the table t_question and also i am doing the same for the submit button to differ which submit button is clicked that is it
Reply

#2
The error message is pretty straight-forward. It looks like you did not define $q_no in your controller before attempting to use it in the if statement:
if ($this->input->post($q_no))
Reply

#3
Please someone help me with this issue ,
i just want to create a comment system like facebook
it aims to get users' questions from database and a text area to add an answer for each question
how to do it ?!!! it is about 2 months now and i can't solve it Sad

Any Help ?
Any guidelines ?
Reply

#4
Honestly, I apparently didn't even look closely enough at the problems you were having last time I tried to answer your question, I just looked at the error message you were receiving and told you where to look to fix that problem.

Overall, by looking at the view I can determine that there are so many issues with the code you have posted here that fixing the error will, at best, just lead to another error, or, at worst, leave you with something which doesn't give you an error message, but doesn't work, either.

You need to start with some basic tutorials on PHP or CodeIgniter to get started. Your view is making separate forms for each question, but you have several controls in the form which will have the same name once those names are resolved in the loop, so it's unlikely you'll receive the data you want to save in the database.

You used set_value() incorrectly with the textarea, so you probably won't see the information in the form, either.

You also didn't close the forms, so you're leaving it up to the browser to decide what's actually in your forms.
Reply

#5
(03-23-2015, 03:29 PM)Abdelrahman Wrote: Please someone help me with this issue ,
i just want to create a comment system like facebook
it aims to get users' questions from database and a text area to add an answer for each question

Don't get discouraged but mwhitney makes a good point. You're trying to make something that is greater than what you know. Some of us could probably make it for you, but then you wouldn't learn. I'm not familiar with Facebook's comment system because I'm not on Facebook, but it sounds like you have a list of questions already in the database, and you want to make it so someone to pick a question and enter an answer. Once a question is answered, it won't appear in the list of questions that need answers. Is that about right?

Go over what mwhitney mentioned about the controls with the same name and the lack of closing form tags. Then post your code, and this time, please format it correctly and use the PHP Code button to insert it. It's hard enough to follow other people's code under the best of circumstances. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#6
Thanks mwhitney & RobertSF for your replies , i really appreciate it,
But , can you turn light on any guidelines to follow it which can help me for solving my problem which is exactly as what RobertSF described it !!
Reply

#7
You're welcome! Let me see if I can give you some guidelines. First, what would that part of your application look like? A very standard way of doing what you want looks like this.

Part 1. You see a list of questions with no answers, perhaps 10 or 20. Each question has a link that says "Answer." At the bottom of the list, there are links for "Previous" and "Next" to move to the previous or next page of questions. If you are at the start of the list, there is no "Previous" link, and if you're at the end of the list, there is no "Next" link. 

Part 2. When you click on the "Answer" link, you go to a page that displays the question with a text box to answer the question and buttons to "Ok" the answer or "Cancel." After you press "Ok" or "Cancel," you return to your list of questions with no answer, and the question you answered no longer appears on the list.

If you are new to programming in general or even just new to PHP/MySQL, you will spend a lot of time getting part 1 to work. Here are the basics for both parts.

You will need a controller. A rule of thumb is that your classes should be nouns and your methods should be verbs. The noun here is "unanswered questions," so let's call the controller "Unanswered_questions." This will correspond to http://yoursite/unanswered_questions. We will put the code that displays the 10 or 20 unanswered questions in the controller's index() function (method).

The code in your controller will calculate the first record to display. If the user presses "Previous," you will have to move to the previous page, etc., etc. Then the code in your controller will call your model to get the records (rows) it needs. If you are still learning MySQL, I recommend you do not use the Codeigniter functions like
PHP Code:
$this->db->select('id,user_name, question, date');
 
$this->db->order_by("id""desc");
 
$query=$this->db->get('t_questions');
 return 
$query->result(); 

Instead, use the complete SQL statement, like this
PHP Code:
$this->db->query('
SELECT
    id,
    user_name,
    question,
    date
FROM t_questions
ORDER BY
    id,
    desc
'
;) 

That's the formatting I personally prefer, but of course, it is the same thing as 
PHP Code:
$this->db->query('SELECT id, user_name, question, date FROM t_questions ORDER BY id, desc';) 


There are two reasons I suggest that you use pure SQL. One reason is that you must learn SQL. The other reason is that it is easier to ask people for help with an SQL query than with a Codeigniter DB query.

When your model return the rows to your controller, your controller will then call the view that will display them. That's the only place where you will possibly do a foreach. I say "possibly" because there is a table class that will display all the rows in an array or a database object. However, we have to add the "Answer" link to each row, so you will still have to do a foreach, but if you add links to your array or database object, you will do that in the controller, not the model.

The link is going to be the URL to the answer() method in your controller, and it's going to include the question id so that the answer() method can retrieve the question. Your link will look like this http://yoursite/unanswered_questions/answer/23 (suppose 23 is the id of the question you wish to answer).

Your answer() method will look something like this
PHP Code:
public function answer($question_id)
{
 
   $this->form_validation->set_rules('your rules');
 
   if ($this->form_validation_run())
 
   {
 
       $data = array(your clean POST data goes here);
 
       $this->your_model->save_answer($data);
 
       redirect('unanswered_questions');
 
       exit;
 
   }
 
   else
    
{
 
       $question_to_answer $this->your_model->get_question($question_id)
 
   }
 
   $data = array(
 
       'question' => $question_to_answer,
 
       'errors' => validation_errors()
 
   );
 
   $this->load->view('answer_question_view'$data);


Your view will display a form with information in $data, which will be the array or object you placed in it, and then you will process the form as you do any other form. If the input does not pass the validation tests, you view should display the errors your controller sends it in $data.

That's about it. You cannot cut and paste any of the code I posted above. It is only useful possibly as an example. Try to get part 1 working first. Once you can page forward and backward through your list of questions, then you are ready for part 2. Good luck!  Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#8
******************** RobertSF ****************

Firstly , i do not know how can i thank you for your time and effort you have spent to give me those very helpful guidelines , i thank you very much Smile.

Secondly , i can do what you advised me to do about you idea of another page which contains only a question with a text area for its answer , and thanks again by the way for your helpful illustrative codes , it was just a challenge for me to do the idea i show it previously about all questions in a page and a text area to answer each question under each question , but i think i will take your advice and do what you said...

Thanks again RobertSF Big Grin Smile
Reply

#9
You're very welcome! I'm very glad that you found what I wrote to be helpful, but now I realize I really didn't answer your original question. Smile

You want a text area under each question so that you can answer questions on the same page. After going through your code, I see what you were trying to do. One of the problems I see is that you were creating a form for each question. You have echo form_open('control_panel'); inside your loop.

You just need one form for all the questions. When your form is posted, you will know which question was answered by the $row->id you embedded in the submit button. However, your code if($this->input->post($q_no)) is incorrect because $q_no has no value in your controller, and besides, the value of $q_no in your view changes with each question.

I think you should code your submit buttons like this.

PHP Code:
<input type="submit" name="<?php echo 'S' . $row->id; ?>" value="Save" /> 

That way, all your buttons will say "Save" instead of "Save24" and "Save 72," etc., etc. In the post variable, the key => value pair will be, for example, 'S24' => 'Save.' To figure out which ID number it is, use array_search:

PHP Code:
// array_search returns the key associated with the value you search
$question_id array_search('Save'$_POST);

// take away the starting S
$question_id substr($question_id1); 

Now you have the ID of the question you answered.

Also, I don't think you need those hidden fields. Go over the problems mwhitney described with your code. Clean the code up and maybe post it again, this time using the PHP Code button so that it's readable. Good luck! Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#10
******************** RobertSF ****************

i did it Big Grin .... i did it Big Grin
Actually during my studying of php i read about array_search() and substr() but during working on my project i forgot to use them at all , thanks robert for your help , now i can answer each question at the same page and after answering it it disappears from the page which contains unanswered questions Big Grin
Thanks again Robert Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.