Here is my database
I want to get the number of received letters but it does not work. This is my code:
$letters3=LettersModel::orderby('id','desc')->get();
$parts= array();
foreach ($letters3 as $letters) {
$parts = explode(',',$letters->girandeh);
foreach($parts as $key=>$parts2){
if($parts2==$email){
echo count($parts2);
}
}
}
It will be displayed in the following way [here is my db][2]
Related
I am creating a group and also storing users id's in it but its showing error in foreach loop i.e. Invalid argument supplied for foreach().
Here is my controller code :
public function createGroup(Request $request)
{
$user_id = request('user_id');
$member = request('member');
$data = array(
'name'=>$request->name,
);
$group = Group::create($data);
if($group->id)
{
$resultarr = array();
foreach($member as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}
}
I am adding values like this,
Desired Output,
I just want that each member to store with different id's in table and group id will be same.
Please help me out
Avoid that if check, it does absolute nothing.
if($group->id)
Secondly your input is clearly a string, explode it and you will have the expected results. Secondly don't save it to a temporary variable, create a new GroupUser immediately.
foreach(explode(',', $member) as $data){
$addmem = new GroupUser();
$addmem->user_id = $data;
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
}
That implode line makes no sense at all, i assumed there is a user_id on the GroupUser relation.
u need to send array from postman
like
Key | value
member[] | 6
member[] | 3
or
$memberArray = explode(",", $member = request('member'))
if($group->id)
{
$resultarr = array();
foreach($memberArray as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}
I'm using laravel and I need to convert my array into a string so that I can add it to the phpSpreadsheet validation. So that when I download an excel spreadsheet
I can select a list of products. The problem I'm having is that I can't get it to be the format that is needed.
Here is the documentation PhpSpreadsheet
The correct format is
$validation->setFormula1('"Item A,Item B,Item C"');
Here is my code
public function generate()
{
$this->spreadsheet = new Spreadsheet;
$validation = $this->spreadsheet->getActiveSheet()->getCell('B5')->getDataValidation();
$validation->setType( DataValidation::TYPE_LIST );
$validation->setErrorStyle( DataValidation::STYLE_INFORMATION );
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setShowDropDown(true);
$validation->setErrorTitle('Input error');
$validation->setError('Value is not in list.');
$validation->setPromptTitle('Pick from list');
$validation->setPrompt('Please pick a value from the drop-down list.');
$products = Product::all();
$product_list = [];
foreach($products as $product)
{
$product_name = $product->name;
array_push($product_list, $product_name);
}
$validation->setFormula1($product_list);
}
I tried a json_encode(), but it seems that excel doesn't like that.
Based on the documentation $product_list should be a comma delimited string not an array.
$products = Product::all();
$product_list = '';
foreach($products as $product) {
$product_name = $product->name;
$product_list .= '"' . $product_name . '",';
}
$validation->setFormula1(rtrim($product_list,','));
$validation4->setFormula1(implode(','), $product_list);
must be string right or formula range cell like =$A;
some value can find when you try make data validation list type on excel, you use that on your program.
Workflow type table
[Workflow caterory table][2]
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
$this->db->from('workflow_category');
$this->db->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() AS $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
here im not able to print the $row["type_name"] and also showing a notice as undefined index for $row["type_name"].
Try like this...
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat,workflow_type.type_name')
->from('workflow_category')
->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
Error due to this line..
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
So it should be just
$this->db->select('workflow_category.wf_cat,workflow_type.type_name');
In above query i have been used query grouping.
My Model code is given below.
public function getattendence($data)
{
$sql="CALL `sp_AttendanceDetails`(?,?,?);";
$query=$this->db->query($sql,$data);
return $query->result_array();
}
But it return one result set.The second result set i not get..
Pass data like this
public function getattendence($data)
{
# example of your $data array
$id = $data['id'];
$name = $data['name'];
$date = $data['date'];
$sql="CALL sp_AttendanceDetails($id,'$name',$date);";
$query=$this->db->query($sql);
return $query->result_array();
}
Use return $this->_execute_multi_query($sql);
refer http://php.net/manual/en/mysqli.multi-query.php
I need to concatenate variables come from model.I send the role_id from controller to model and get the role name according to its id.
controller:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
foreach($data['rec'] as $i)
{
$name['x']=$this->amodel->get_name($i->role_id);
}
$this->load->view('sections',array_merge($data,$name));
}
I write $name['x'].=$this->amodel->get_name($i->role_id); but it shows the error which undefined index:x.How can I concatenate the role name in cotroller to send it to view?
you probably didnt define $name
$name = array();
// your foreach
foreach ()
btw, the concatenating was ok
$var = "foo";
$var .= "foo"
// will result in "foofoo"
If you want to append something using the .= syntax you need to make sure the variable or array exists first.
Try this:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
$name = array();
foreach($data['rec'] as $i) {
if (isset($name['x'])) {
$name['x'] .= $this->amodel->get_name($i->role_id);
} else {
$name['x'] = $this->amodel->get_name($i->role_id);
}
}
$this->load->view('sections',array_merge($data,$name));
}