Asked  7 Months ago    Answers:  5   Viewed   27 times

I'm having difficulty with display data from the db to dropdown.

This is what I have tried:

Model.php

        public function __construct()
        {
            parent::__construct();
        }

        function getAllGroups()
        {
            /*
            $query = $this->db->get('location');

            foreach ($query->result() as $row)
            {
                echo $row->description;
            }*/

            $query = $this->db->query('SELECT description FROM location');

            foreach ($query->result() as $row)
            {
                echo $row->description;
            }

            //echo 'Total Results: ' . $query->num_rows();
        }

Controller.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Delivery_controller extends CI_Controller{
        public function __construct()
        {
            parent::__construct();
            $this->load->model('delivery_model');

        }
        public function index()
        {

            $data['title']= 'Warehouse - Delivery';
            $this->load->view('include/header',$data);
            $this->load->view('include/navbar',$data);
            $this->load->view('delivery_view', $data);
            $this->load->view('include/sidebar',$data);
            $this->load->view('include/footer',$data);
        $data['groups'] = $this->delivery_model->getAllGroups();
        }


    }

View.php

           <select class="form-control">
                <?php 
                        $data = $this->delivery_model->getAllGroups();
                foreach($description as $each)
                { ?><option value="<?php echo $each['description']; ?>"><?php echo $each['description']; ?></option>';
                <?php }
                ?>
                </select>

But the results appear on top of my page. It's not appearing on the dropdown list. What am I doing wrong in here? Help is pretty much appreciated. Thanks.

 Answers

75

You should not be calling your model from your view. Instead try calling you model and setting $data['groups'] before you load your views.

Also do not echo the row results in your model unless you want it displayed on your page.

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('delivery_model');

    }
    public function index()
    {

        $data['title']= 'Warehouse - Delivery';
        $data['groups'] = $this->delivery_model->getAllGroups();
        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('delivery_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);

    }


}

Model:

    public function __construct()
    {
        parent::__construct();
    }

    function getAllGroups()
    {
        /*
        $query = $this->db->get('location');

        foreach ($query->result() as $row)
        {
            echo $row->description;
        }*/

        $query = $this->db->query('SELECT description FROM location');


        return $query->result();

        //echo 'Total Results: ' . $query->num_rows();
    }

View:

       <select class="form-control">
            <?php 

            foreach($groups as $row)
            { 
              echo '<option value="'.$row->description.'">'.$row->description.'</option>';
            }
            ?>
            </select>
Wednesday, March 31, 2021
 
Kevin_Kinsey
answered 7 Months ago
27

For me the issue was in the php.ini file. The property mysql.default_socket was pointing to file in a non-existent directory. The property was pointing to /var/mysql/mysql.sock but in OSX, the file was located in /tmp/mysql.sock.

Once I updated the entry in php.ini and restarted the webserver, the issue was resolved.

Wednesday, March 31, 2021
 
jcubic
answered 7 Months ago
81

Don't be afraid to change core files, just alter FCPATH/system/database/drivers/mysqli/mysqli_driver.php

function escape_str($str, $like = FALSE)
{
    if (is_array($str))
    {
        foreach ($str as $key => $val)
        {
            $str[$key] = $this->escape_str($val, $like);
        }

        return $str;
    }

    if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
    {
        $str = mysqli_real_escape_string($this->conn_id, $str);
    }
    else
    {
        $str = addslashes($str);
    }

    // escape LIKE condition wildcards
    if ($like === TRUE)
    {
        $str = str_replace(array('%', '_'), array('\%', '\_'), $str);
    }

    return $str;
}

I had the same issue


Better solution -> https://ellislab.com/forums/viewthread/228288/ "stated in github that it will be fixed in CodeIgniter 3.0 the fix already exists in that repository"

Wednesday, March 31, 2021
 
themihai
answered 7 Months ago
16

Okay, I think what you need to do is set an alias for the id field in the comments table:

function com_control() {
    $this->db->select('entry_id, comments.id AS comment_id, comment, title');
    $this->db->from('comments');
    $this->db->join('posts', 'comments.entry_id = posts.id');
    $query = $this->db->get();

    return $query->result;
}

Then you can reference the comments.id field as simply $row->comment_id:

$this->table->set_heading('Entry ID', 'Comment ID', 'Comment', 'Title');

foreach ($comm_control as $row ) {
    $this->table->add_row(
        $row->entry_id,
        $row->comment_id,
        $row->comment,
        $row->title             
    );      
}

echo $this->table->generate();

Actually if the column 'id' is unique to the comments table, then you could just use $row->id; the problem arises when you are joining tables that both have a column named the same; it becomes ambiguous and the computer won't know what you're referencing.

Wednesday, March 31, 2021
 
mistero
answered 7 Months ago
49

1) Id attribute should be unique for each element.

2) You have much code redundant .reduce the code redundant using for loop .

3) Remove your unwanted display:none style property and jquery . because you can able to handle the empty option in server side code .

PHP : Simplified version of your code

$result = mysqli_query($con,$query);

if($result)
{

    while($row = mysqli_fetch_array($result))
    {

        $questions = $row['questiontitle'];
        $qid = $row['question_id'];
        $anstype = $row['answer_type'];
        $count++;
        $answer++;

        echo "<b>Question ". $count.".)</b> ". $questions. "</br>";


        $option1 = $row['Option_1'];
        $option2 = $row['Option_2'];
        $option3 = $row['Option_3'];
        $option4 = $row['Option_4'];
        $option5 = $row['Option_5'];
        $option6 = $row['Option_6'];
        $option7 = $row['Option_7'];
        $option8 = $row['Option_8'];
        $option9 = $row['Option_9'];
        $option10 = $row['Option_10'];

         ?>


         <input style="display:none" type="text" class="form-control" value="<?php echo $qid ?>" name="question_<?php echo $answer; ?>">

        <?php   
        for($j=1;j<=10;$j++)
        {

             if(isset(${'Option_'.$j}) && !empty(${'Option_'.$j}) )
             { 
             ?>

                   <div>
                        <input type="<?php if($anstype == 'radiobutton'){  echo 'radio'; }else{ echo 'checkbox';  }?>" name="answer_<?php echo $answer ?>" value="<?php echo ${'Option_'.$j};?>" id="<?php echo ${'Option_'.$j}; ?>"><?php echo ${'Option_'.$j};  ?>
                    </div>
             <?php  
             }
             ?>  


            <br/>

        <?php 
        } 
        ?>

                <?php  

    }

}  

?>
Saturday, May 29, 2021
 
Optimus
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :