Welcome Guest, Not a member yet? Register   Sign In
Crazzy query idea
#1

[eluser]Atrhick[/eluser]
Hello guys, I'm not a very good programmer so this maybe very simple but I'm going to ask anyhow.

here is what I need to do

I'm collecting this data from the view using post.

Code:
function add_test() {
$test_title = $this->input->post('test_title');
$test_creator = $this->input->post('test_creator');
    $test_created = $this->input->post('test_created');

$this->db->escape($test_title);  
$this->db->escape($test_creator);
$this->db->escape($test_created);

//Question #1

$question_1 = $this->input->post('question_1');
    $answer_1_question_1 = $this->input->post('answer_1_question_1');
$answer_2_question_1 = $this->input->post('answer_2_question_1');
$answer_3_question_1 = $this->input->post('answer_3_question_1');
$answer_4_question_1 = $this->input->post('answer_4_question_1');
$answer_5_question_1 = $this->input->post('answer_5_question_1');
    
$this->db->escape($question_1);  
$this->db->escape($answer_1_question_1);  
$this->db->escape($answer_2_question_1);  
$this->db->escape($answer_3_question_1);
$this->db->escape($answer_4_question_1);  
$this->db->escape($answer_5_question_1);

$data = array(
       'test_title' => $this->input->post('test_title'),
       'test_creator' => $this->input->post('test_creator'),
    'question_1' => $this->input->post('question_1'),
    'answer_1_question_1' => $this->input->post('answer_1_question_1'),
    'answer_2_question_1' => $this->input->post('answer_2_question_1'),
    'answer_3_question_1' => $this->input->post('answer_3_question_1'),
    'answer_4_question_1' => $this->input->post('answer_4_question_1'),
    'answer_5_question_1' => $this->input->post('answer_5_question_1'),
    'test_created' => $this->input->post('test_created')
    );
    $this->db->insert('tests',$data);
}

My problem is the when the user is creating a test they can create 350 questions in one test "yes the database can hold all that data in one table row" is there a loop i can create or something that will do what i have above based on how much data in coming in?

I don't this its good coding practice for me to do the above code 350 time... or do I have to?
#2

[eluser]Rolly1971[/eluser]
you could try something like this:

Code:
$postdata = $this->input->post();
$data = array();
foreach ($postdata as $key => $value)
{
    $data[$key] = $this->db->escape($value);
}

$this->db->insert('tests',$data);
#3

[eluser]Atrhick[/eluser]
[quote author="Rolly1971" date="1405537411"]you could try something like this:

Code:
$postdata = $this->input->post();
$data = array();
foreach ($postdata as $key => $value)
{
    $data[$key] = $this->db->escape($value);
}

$this->db->insert('tests',$data);
[/quote]


wow thank you that worked perfectly!!!! I only had to unset some post but it works like a charm!! i can't believe how easy and small this code was compared to what i was thinking of doing.. its a good thing i asked.
#4

[eluser]CroNiX[/eluser]
He doesn't need to manually use escape anyway since he's using active record. Now things are going through escape twice.
#5

[eluser]CroNiX[/eluser]
All of this:
Code:
$postdata = $this->input->post();
$data = array();
foreach ($postdata as $key => $value)
{
    $data[$key] = $this->db->escape($value);
}

$this->db->insert('tests',$data);

does the exact same thing as:
Code:
$this->db->insert('tests', $this->input->post());
Active Record AUTOMATICALLY escapes the variables, so there is no need to do it twice. Read the pink note at the bottom of the db::insert() section here: http://ellislab.com/codeigniter/user-gui...tml#insert




Theme © iAndrew 2016 - Forum software by © MyBB