0

i struggling with forloop in MVC. i want to pass data from input text where in data is equivalent to 1,2,3,4,5,6,7...... -that is from database.

i pass it to controller and get the data using $_POST method with name text1. here is my controller code

    {
        $template = $this->loadView('view_display');
        if(isset($_POST['check'])):
            $text1= $_POST['text1'];
            $text1= explode(',', $text1);
            foreach($text1 as $text1):
            $rem = $this->Faculty_model->testFunction($this->security_plugin->cleanInput($text1));
            $template->set(['stud' => $rem, 'username' => $this->session_helper->get('username'), 'security'=> $this->security_plugin, 'url' => $this->url_helper]);
            endforeach;
        endif;
        $this->security_plugin->CSRFToken();
        $template->render();

here is my model

    {
        $sql="select * from table where id=:text1";
        $bind = array(
            ':text1' => $text1
        );
        $data = $this->db->fetchAll($sql, $bind);
        return $data;
    }

and this is my view

<?php if(!empty($stud)): ?>
<?php foreach($stud as $stud): ?>
<?php echo $security->SanitizeString($stud->ID); ?>
<?php echo $security->SanitizeString($stud->NAME); ?>
<?php echo $security->SanitizeString($stud->AGE); ?>

The problem is it will only display the last number from text1 textbox. i cant figure it out.

anyhelp is appreciated :)

1
  • This has NOTHING to do with MVC, Commented Apr 16, 2020 at 11:40

1 Answer 1

1

1.You need to define $rem as an array before foreach() and then do assignment of values.

2.Put $template->set() code outside of foreach()

$template = $this->loadView('view_display');
if(isset($_POST['check'])):
    $text1= $_POST['text1'];
    $text1= explode(',', $text1);
    $rem = [];
    foreach($text1 as $text1):
    $rem[] = $this->Faculty_model->testFunction($this->security_plugin->cleanInput($text1));
    endforeach;
    $template->set(['stud' => $rem, 'username' => $this->session_helper->get('username'), 'security'=> $this->security_plugin, 'url' => $this->url_helper]);
endif;

$this->security_plugin->CSRFToken();
$template->render();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.