Call variable from view in Codeigniter - php

I am trying to call $data['form_value'] from view in Codeigniter, but that array returns empty.
Here is my Controller:
$data['field'] = $this->cc->get_form_names($data['selid']);
foreach($data['field'] as $key=>$valuee) {
$data['form_value'] = $this->cc->get_form_value($valuee->select);
}
Now in the View:
foreach($field as $key=>$valuee) {
echo "<span\"> ".$valuee->disp_name." :</span>";
<select name=\"\">";
foreach($form_value as $v=>$vv) {
$value = $vv->select;
echo "<option value=\"".$vv->select."\" >".$vv->select."</option>";
}
echo"</select> ";
}
When I do var_dump($form_value) it return array(0) { }
How would I get that variable?

You are overwriting form_value in loop. Try below code in your controller.
$form_value = array();
foreach($data['field'] as $key=>$valuee) {
$form_value[] = $this->cc->get_form_value($valuee->select);
}
$data["form_value "] = $form_value ;

Modify your Controller code like this,
$data['field'] = $this->cc->get_form_names($data['selid']);
foreach($data['field'] as $key=>$valuee) {
$data['form_value'][] = $this->cc->get_form_value($valuee->select);
}
$data['form_value'] should be an array to loop through it. You are using it as string and trying to loop through it.
Hope this helps :)

Related

Get values ​from select2 with php without jquery

I have a registration system where there are several fields. One of the registration fields is Segments and they come from the database:
public function comboSegmentos($key)
{
$sqlMostrar = mysqli_query($this->conexao, "SELECT * FROM loja_segmentos;");
if (mysqli_num_rows($sqlMostrar) == 0)
{
$mostrar = "<div style='color: red'>Nenhuma segmento cadastrado até o momento.</div>";
}
else
{
$mostrar = "<select name='Segments[]' id='segmentos' class='form-control select2' multiple='multiple' style='width: 35%'>";
while ($jmMostrar = mysqli_fetch_object($sqlMostrar))
{
$mostrar .= "<option value='".$jmMostrar->Segmentos."' ".$selected.">".$jmMostrar->Segmentos."</option>";
}
$mostrar .= "</select>";
}
return $mostrar;
}
So that the method doesn't have too many parameters, I did it like this:
<?php
...
$dados = array_filter($_POST);
echo $metodos->cadastrarProdutos($dados);
Method cadastrarProdutos($dados)
But I'm not able to get the values ​​of the Segments even using foreach().
<?php
public function cadastrarProdutos(array $dados)
{
...
$segments = $dados["Segments"];
foreach($segments as $seg)
{
$segm = $seg;
}
...
}
How can I get the values ​​of the select2 field and get them using a PHP method without Jquery?
I managed to solve it as follows:
<?php
public function cadastrarProdutos(array $dados)
{
...
$segments = $dados["Segments"];
$segm = array();
foreach($segments as $seg)
{
$segm[] = $seg;
}
$segments = implode(',',$segm);
...
}

variables within controller foreach returns NULL inside view

I have a web application built using codeigniter. What I'm trying to do is show a summary of results on my view. In my controller I have the below code
function index() {
$data['summery'] = $this->Main_model->get_summery();
foreach ($data['summery'] as $d) {
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
}
}
The problem that im having is each summery record has total weekly count and a monthly count based on the i_id
in my view i loop through the summery and it display the summery but when i echo $total_week or echo $total_month its always returns NULL
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
When i do a var_dump on the above variables it shows the number, but when i try to echo it on the view it returns NULL
I hope I under stood correct Try something like this
function index() {
$this->load->model('main_model');
$data['summery'] = array();
$summery = $this->main_model->get_summery();
foreach ($summery as $d) {
$data['summery'][] = array(
'total_week' => $this->main_model->get_total_week($d['i_id']),
'total_month' => $this->main_model->get_total_month($d['i_id'])
);
}
$this->load->view('the_view', $data);
}
View
<?php foreach ($summery as $something) {?>
<?php echo $something['total_week'];?>
<?php echo $something['total_month'];?>
<?php }?>

PHP bringing back all rows instead of requested

I have a query that is bringing back all rows from the table instead of the fields I have specified. Can you see any mistakes in this? I'm using codeigniter.
Thanks in advance!
unset($conditions);
$conditions['conditions'] = array("accountid"=>$this->sessionInfo['database_account_id'],
"DATE_FORMAT(salestart,'%Y-%m-%d')"=>$today,
"shop"=>"london"
);
$conditions['group_by'] = "item";
$conditions['fields'] = "accountid, item, count(uniqueid) as totalitems, sum(options) as totaloptions, colour";
$today_sales = $this->Database_Model->selectData("sales",$conditions);
My model is:
public function selectData($table,$condition=array()) {
if(isset($condition['fields'])){
$fields = $condition['fields'];
}
else{
$fields = "*";
}
$this->Database->select('*');
$this->Database->from($table);
if(isset($condition['conditions'])){
$this->Database->where($condition['conditions']);
}
if(isset($condition['group_by'])){
$this->Database->group_by($condition['group_by']);
}
if(isset($condition['order_by'])){
$this->Database->order_by($condition['order_by']);
}
if(isset($condition['where_in'])){
$where_in = $condition['where_in'];
foreach($where_in as $key =>$value){
$this->Database->where_in($key,$value);
}
}
if(isset($condition['joins'])){
$joins = $condition['joins'];
foreach($joins as $join){
$this->Database->join($join['table'], $join['joinWith'],$join['type']);
}
}
$query = $this->Database->get();
return $query->result_array();
}
Change this
$this->Database->select('*');
to this
$this->Database->select($fields);

Putting data base values into array shows error in codeigniter

I am taking data from many tables. I want to display many objects in different places. I got the data from data base, but I want to put the data into an array for useful purpose, but it's not working.
This my controller code:
public function compare_by_business_sectors() {
//print_r($this->input->post());exit;
if ($this->input->post())
{
$solution_array = array();
//print_r (json_encode($business_sectors)); exit;
$business_sectors=$this->home_model->compare_business_sectors_data($this->input->post());
$tab_child_id = "";
$id="";
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id = array();
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
//$solution_array[$id]= $id;
}
print_r($id);
//$this->load->view('compare_by_business_sectors.php');
}
This is my model code:
public function compare_business_sectors_data($sectorid) {
$query = $this->db->select('solutions.*,solution_tabs_child_fields.field_id,solution_tabs_child_fields.tab_child_id,solution_tabs_child_fields.title')
->from('solutions')
//->join('solutions', 'business_sector.sector_id = solutions.business_sector_id',"left")
->join('solution_features','solutions.entry_id = solution_features.entry_id',"left")
->join('solution_tabs_child_fields','solution_features.field_id = solution_tabs_child_fields.field_id')
->where('solutions.business_sector_id', $sectorid['id'])
->get();
return $query->result();
//print_r($query->result());exit;
}
change it as follow and try.
$id_string = "";
$id_array = array();
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id_string = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
print_r($id_string);
print_r($id_array);
First you are assigning string value to $id then, $id will convert to array only if first if() statement execute other wise it will not be a string. So to overcome from this keep $id_array before for loop and you can capture string in another variable.

How to correlate below given foreach loop?

I have two for loops given below
foreach($result_access as $acc){
$usr_access_id[] = $acc->id;
$usraccess[] = $acc->rules;
}
> UPDATED
foreach($somearray as $someid){//Updated
foreach($usraccess as $accessusr){
if(in_array($someid,$usraccess)){
$myid = ??;///Here i want the $usraccess associated $acc->id, how can I get that?
}
}
}
As you can see that I want the $myid get assigned with $acc->id which should be associated with the current $usraccess array
Here's a sane solution:
foreach ($result_access as $acc) {
if (in_array($someid, $acc->rules)) {
$myid = $acc->id;
}
}
The insane solution would be:
...
foreach($usraccess as $i => $accessusr){
if(in_array($someid,$usraccess)){
$myid = $usr_access_id[$i];
}
}
Is this what you want?
foreach($result_access as $acc){
$usr_access_id[] = $acc->id;
$usraccess[ $acc->id ] = $acc->rules;
}
foreach($usraccess as $accessusr){
if(in_array($someid,$usraccess)){
$myid = $usraccess[ $someid ] ;///Here i want the $usraccess associated $acc->id, how can I get that?
}
}

Categories