i want to import from excel using php excel from my codeigniter with current cell containt current text
cell1
cell2
A
1
B
2
C
3
i want just cell except B in cell1 to insert database
my controller
public function import_excel(){
if (isset($_FILES["fileExcel"]["name"])) {
$path = $_FILES["fileExcel"]["tmp_name"];
$object = IOFactory::load($path);
foreach($object->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=2; $row<=$highestRow; $row++)
{
$cell1= $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$cell2= $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$temp_data[] = array(
'cell1' => $cell1,
'cell2' => $cell2,
);
}
}
if($cell1 == 'B'){
}else{
$this->load->model('M_Admin');
$insert = $this->M_Admin->insert($temp_data);
}
but this failed and data still insert. please help me and many thanks you for help.
I never use phpexcel before, but if it's array and consider that $cell1 is a string/text, I guess you can try to use this:
for($row=2; $row<=$highestRow; $row++)
{
$cell1= $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$cell2= $worksheet->getCellByColumnAndRow(1, $row)->getValue();
//cell value you want to hide
if ($cell1 == 'B') {
$cell1 = 'xxx'; //overwrite that value into something
}
$temp_data[] = array(
'cell1' => $cell1,
'cell2' => $cell2,
);
}
note: I'm using codeigniter 3
Newbie at codeigniter here. how can I save my total amount in my table per user? The only thing I did is to sum it and show it on my view. My target is to save the total commission on my "agent_commission" table". I have provided a screenshot of my target for better visualization. Thank you in advance. Any help will be appreciated.
Views:
<?php $formattedNum = number_format($commCurrentBalance, 2);
echo $formattedNum;
?
Controller:
public function commi()
{
$data['activeNav'] = "";
$data['subnav'] = "commissions";
$this->header($data);
$this->nav();
$data['currentPoints']=$this->session->userdata('currentPoints');
$data['result'] = $this->commissions->get1();
$data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
$this->load->view('comm_view', $data);
$this->footer();
}
Model:
function gettotalcommi(){
$reqcommi= $this->session->userdata('uid');
$this->db->select_sum('amount');
$this->db->where('commTo',$reqcommi);
$result = $this->db->get('agent_commission_history')->row();
return $result->amount;
}
I'm not sure if it will work but please try
$db->select_sum('amount');
$db->where('commTo',$param);
$aq = $db->get_compiled_select('agent_commission_history');
$db->set('commCurrentBalance',"($aq)",false);
$db->where('commTo',$param2);
$db->update('agent_commission');
This is the Codeigniter equivalent of the query I posted earlier:
UPDATE agent_commission
SET commCurrentBalance = ( SELECT SUM(amount) FROM agent_commission_history WHERE commTo = ?)
WHERE commTo = ?
public function commi(){
$data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
$dataArray = array(
'commCurrentBalance'=>$data['commCurrentBalance']
);
$this->db->insert('agent_commission',$dataArray);
$this->load->view('comm_view', $data);
$this->footer();
}
Laravel Many to Many relation
Hi, I am stuck trying to get the data from database by weeks.
I have the next table (Many to Many relation)
routine: id, plan_id, user_id.
exercise: id, name, description, image.
exercise_routine: id, routine_id, exercise_id, week, day.
Relations
Routine
public function exercises() {
return $this->belongsToMany(Exercise::class)->withPivot('week', 'day', 'completed')->orderBy('week','asc');;
}
Exercise
public function routines() {
return $this->belongsToMany(Routine::class)->withPivot('week', 'day', 'completed');
}
I would like to get all the rows by week like this.
Json example:
{
"week": [
1: {
"exercises": [
{
"name": "Abs"
}
]
},
2: {
...
}
]
}
I already tried this
if(!empty($routine)) {
foreach ($routine->exercises as $exercise) {
$week = $exercise->pivot->week;
if($week == $previous_week) {
array_push($this->weeks, $exercise->pivot->week);
} else {
array_push($this->weeks, [
$exercise->pivot->week => $exercise->pivot->week
]);
}
$previous_week = $exercise->pivot->week;
}
// dd($this->weeks);
// return DB::table('exercise_routine')->where('routine_id',$routine->id)->max('week');
}
The explanation
exercise_routine table has a week number to separate exercises by week.
I need to create an array and if the week is 1, then push in the array 1, if the next is 1, push in the same array. If the next is 2 or different just push but in the number 2.
I am not sure if you can understand what I mean, it is just trying to get the way to do it.
Many thanks in advance.
Using the collection method mapToGroups can help you simplify grouping up the exercises.
Here's an example on how I would use it for your case:
if(!empty($routine)){
// creating groups of exercise by week [1 => ['abs'], 2 => []]
$exerciseWeeks = $routine->exercises->mapToGroups(function($exercise, $k){
return [$exercise->pivot->weeks => $exercise->name];
})
->map(function($exerciseInWeek, $k){ // creating your json format from here point on
$weekObj = new \stdClass;
$weekObj->exercises = [];
foreach($exerciseInWeek as $exerciseName){
$exerciseObj = new \stdClass;
$exerciseObj->name = $exerciseName;
$weekObj->exercises[] = $exerciseObj;
}
return $weekObj;
});
// creates the final json object
$routineObj = new \stdClass;
$routineObj->week = $exerciseWeeks->all();
$finalRoutineJSON = json_encode($routineObj);
}
I am working in a import method, that save the data od an *.csv into a table, but i verify if this data already exist in table, if this is true, i would redirect to a view or to another method, but dont redirect!
In the line
//comparing if the data of file exists in the table
if exists data i try to make a return:
return view ('catinvistas.ingresos.mensaje');
and must to finish all the following proccess, right?
public function import()
{
Excel::load('storage\app\reloj.csv', function($reader) {
foreach ($reader->get() as $marcacion) {
//file data
$archivo=(int)$marcacion->id_voluntario;
$archivo_fecha=$marcacion->marcacion;
//table data
$tabla_c1 = DB::table('marcaciones')->where('id_voluntario', $marcacion->id_voluntario)->first();
if (!empty($tabla_c1)){
$tabla=(int)$tabla_c1->id_voluntario;
$tabla_fecha=$tabla_c1->marcacion;
//comparing if the data of file exists in the table
if (($tabla==$archivo) && ($tabla_fecha==$archivo_fecha))
{
//dd ($tabla,$tabla_fecha,$archivo,$archivo_fecha);
return view ('catinvistas.ingresos.mensaje');
}
}
//insert all data of the file into the table
Marcaciones::create([
'id_voluntario' =>$marcacion->id_voluntario,
'marcacion' => $marcacion->marcacion
]);
}
});
$data=Marcaciones::all();
return view('catinvistas.ingresos.importacion')->with('data',$data);
}
I want to create a slug. it should be a concatenation of title and subtitle.The following is my code and it's not working, i don't know where i went wrong?
public function setTitleAttribute($value)
{
$this->attributes['main_title'] = ucfirst($value);
$this->attributes['sub_title'] = $value;
if (! $this->exists) {
$this->attributes['slug'] = str_slug($this->attributes['main_title'].$this->attributes['sub_title']);
}
}
I need slug as a combination of main_title+sub_title
public function to_slug ($string) {
$table = array(
'Š'=>'S', 'ı'=>'i', 'ğ'=>'g', 'ü'=>'u', 'ş'=>'s', 'ö'=>'o', 'ç'=>'c', 'Ğ'=>'G', 'Ü'=>'U', 'Ş'=>'S',
'İ'=>'I', 'Ö'=>'O', 'Ç'=>'C',
'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
);
return preg_replace('/[^A-Za-z0-9-]+/', '-', strtr($string, $table) );
}
I'm using this code and its working for me.
It is also good for utf-8.
You can use a laravel package:
https://github.com/cviebrock/eloquent-sluggable
Or I use JavaScript when I sent the form:
https://github.com/madflow/jquery-slugify
I have replicated the exact condition like yours passing static data and its working perfectly in my machine.. try below solution.. do some dd and see whats the combination of the main_title and sub_title is resulting. Also check by removing that if condition once.
$this->attributes['main_title'] = ucfirst($value) . " ";
$this->attributes['sub_title'] = $value;
$slugToUse = $this->attributes['main_title'] . $this->attributes['sub_title'];
if (! $this->exists) {
$this->attributes['slug'] = str_slug($slugToUse);
}
}