0

I have this kind of table

aa

I want fetch data like this

a

i have fetched dishes name(text) but i also need its id with it in a single array how can i do this? My code is as below: Controller:-

function fetch()
    {
        $this->load->model('fetch');
        $d=$this->fetch->call();
        $e=$d->result();
        $max=$d->num_rows();
        for($i=0;$i<$max;$i++){
            $a[$i]=$e[$i]->product;
            $a2[$i]=$e[$i]->id;
            $b[]=explode(',',$a[$i]);
        }
        echo '<pre>';
        print_r($a2);
        $array['arr'] = call_user_func_array('array_merge', array_map('array_values', $b));
        $this->load->view('display',$array);
    }

Model:-

function call()
    {
        $this->db->select('*');
        $this->db->from('ruff');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return false;
        }
    }

View:-

<body>
<input type="text" name="item" id="txt_search" list="item_list">
<datalist id="item_list">
    <?php
        foreach($arr as $a)
        {
            echo "<option>".$a."</option>";
        }
    ?>
</datalist>
</body>

Thanks in advance.

0

4 Answers 4

0

In your controller try this...code

 $this->load->model('fetch');
    $d=$this->fetch->call();
    $e=$d->result_array();
    // $max=$d->num_rows();
    $products='';
    foreach ($e as $p) {
      $products.=$p['product'];
    }

    $data=explode(',', $products);

    echo '<pre>';
    print_r($data);
    echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

0

Usually accessing database functions in the controller is not a good practice. So, in order to attend the needs of counting items and such you could change your model to return an array of items.

Model:

function call()
{
    $data = array();

    $this->db->select('*');
    $this->db->from('ruff');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach($query->result() as $row){
            $data[] = (array)$row;
        }
        return $data;
    } else {
        return false;
    }
}

Then, in the fetch function in your controller, you can explode the product field and create another array to submit to your view.

Controller:

function fetch()
{
    $this->load->model('fetch');
    $d=$this->fetch->call();

    $list = array();

    foreach($d as $row)
    {
        $products = explode(',', $row['product']);
        foreach($products as $p)
        {
            $list[] = array('id' => $row['id'], 'product' => $p);
        }
    }

    $this->load->view('display',array('arr' => $list));
}

It will result you to have an array like the one which follows:

Array
(
[0] => Array
    (
        [id] => 1
        [product] => veg. burger
    )

[1] => Array
    (
        [id] => 1
        [product] =>  non-veg burger
    )

[2] => Array
    (
        [id] => 1
        [product] =>  chees burger
    )

[3] => Array
    (
        [id] => 2
        [product] => vadapau
    )

[4] => Array
    (
        [id] => 2
        [product] =>  butter vadapau
    )

[5] => Array
    (
        [id] => 2
        [product] =>  chees vadapau
    )
)

Now your ids are available in your view.

View:

<body>
    <input type="text" name="item" id="txt_search" list="item_list">
    <datalist id="item_list">
    <?php
        foreach($arr as $a)
        {
            echo "<option>".$a['product']."</option>";
            //$a['id']
        }
    ?>
    </datalist>
</body>

Comments

0

Here is Your model Code.

public function call()
{
   $query = $this->db->select("id,product")
                        ->from("stuff")
                        ->get();
    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return false;
    }
}

Now you will get the result array of data in controller. Here is controller function.

public function fetch()
{
    $this->load->model('fetch');

    $result = $this->fetch->call();
    foreach ($result as $value) {
        $newArr = explode(',',$value['product']);   
        foreach ($newArr as  $val) {
            $productArray[]  = array('productID'=>$value['id'],"product"=>$val);
        }
    }
    $data['finalArray'] = $productArray;
    $this->load->view('display',$data);

}

Now access the final product array in view. Here is view Code.

<input type="text" name="item" id="txt_search" list="item_list">
<table id="item_list" border="1">
    <?php

        foreach($finalArray as $stuffs)
        {
            echo "<tr>";
            echo "<td>".$stuffs['productID']."</td>";
            echo "<td>".$stuffs['product']."</td>";
            echo "</tr>";
        }
    ?>
</table>

Cheers!

Comments

0

Its very simple code please following code your model code below:

Model:-

function call() { return $this->db->get('ruff')->result_array(); }

This code give you all data now controller code like below

Controller:-

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

This code give you all data now controller code like below

Controller:-

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); } This code give you all data now controller code like below

Controller:-

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

Now please write code in view View:-

` This code give you all data now controller code like below

Controller:- function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

Now please write code in view View:-

<body> <input type="text" name="item" id="txt_search" list="item_list"> <datalist id="item_list"> <?php foreach($arr as $value){ $newarray=explode(',',$value); foreach($newarray as $test) { echo "<option value=".$arr['id'].">".$test['product']."</option>"; } } ?> </datalist> </body>

I think this code will help You Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.