問題描述
嚴重性:警告消息:非法字符串偏移 'id' MY OWN PROJECT (Severity: Warning Message: Illegal string offset 'id' MY OWN PROJECT)
我在 codeigniter 中的 model/Master.php 代碼有問題。
這是我的觀點截圖
model/Master.php:
function get_tasks($limit=20,$task_id=0,$page_num=1,$search='',$sortfield='default',$sort='sort')
{
$data = array();
$sortable_fields = array('id'=>'t.id','ime'=>'t.ime','sn_br'=>'t.sn_br','gar_stanje'=>'t.gar_stanje','opis_kvara'=>'t.opis_kvara','comment'=>'t.comment','task_assigned'=>'t.task_assigned','ime'=>'t.ime','prezime'=>'t.prezime','br_tel'=>'t.br_tel','email'=>'t.email','datum'=>'t.datum','account_id'=>'u3.account_id');
$account_id = $this‑>session‑>userdata('account_id');
$offset = ($page_num==1) ? 0 : ($page_num*$limit)‑$limit;
if ($task_id!=0){
$this‑>db‑>select('t.id, t.ime, t.sn_br, t.gar_stanje, t.opis_kvara, t.comment, u2.user_firstname as task_assigned, t.ime, t.prezime, t.br_tel, t.email, t.datum, u3.account_id');
}else{
$this‑>db‑>select('SQL_CALC_FOUND_ROWS t.id, t.ime, t.sn_br, t.gar_stanje, t.opis_kvara, t.comment, u2.user_firstname as task_assigned, t.ime, t.prezime, t.br_tel, t.email, t.datum, u3.account_id', FALSE);
}
if ($task_id!=0){
$this‑>db‑>where('t.id',(string)$task_id);
}
$this‑>db‑>where('u3.account_id',(string)$account_id);
$this‑>db‑>where('t.task_status','1');
$this‑>db‑>from('serv t');
$this‑>db‑>join('users u2', 't.task_assigned = u2.user_id', 'inner');
$this‑>db‑>join('accounts u3', 'u3.account_id = u3.account_id', 'inner');
if ($task_id!=0){
$this‑>db‑>limit(1);
}else{
$this‑>db‑>limit($limit,$offset);
}
if (array_key_exists($sortfield,$sortable_fields) && ($sort=='asc' || $sort=='desc')){
$this‑>db‑>order_by($sortable_fields[$sortfield],strtoupper($sort));
}else{
$this‑>db‑>order_by('t.task_status ASC, t.datum DESC');
}
$query = $this‑>db‑>get();
// echo $this‑>db‑>last_query();
if ($query‑>num_rows()>0){
$tasks = $query‑>result_array();
if ($task_id!=0){
$data['task'] = $tasks[0];
}else{
$data['serviss'] = $tasks;
}
$query = $this‑>db‑>query('SELECT FOUND_ROWS() AS `Count`');
$data["total_rows"] = $query‑>row()‑>Count;
}else{
$data['serviss']['id'] = 'Unable to find!';
$data['serviss']['ime'] = 'Unable to find!';
$data['serviss']['prezime'] = 'Unable to find!';
$data['serviss']['br_tel'] = 'Unable to find!';
$data['serviss']['naziv_artikla'] = 'Unable to find!';
$data['serviss']['sn_br'] = 'Unable to find!';
$data['serviss']['gar_stanje'] = 'Unable to find!';
$data['serviss']['opis_kvara'] = 'Unable to find!';
$data['serviss']['opis_posla'] = 'Unable to find!';
$data['serviss']['utro_mater'] = 'Unable to find!';
$data['serviss']['comment'] = 'Unable to find!';
$data['serviss']['datum'] = 'Unable to find!';
$data['serviss']['datum_otp'] = 'Unable to find!';
$data['serviss']['task_status'] = '0';
$data["total_rows"] = '0';
}
return $data;
}
views/default/servis.php
<div class="clearfix"> </div>
<?php if ($this‑>session‑>flashdata('msg')=='1'){ ?>
<div id="msg_container2" class="alert alert‑success alert‑dismissible" role="alert">
<button id="msg_close_button2" type="button" class="close" onclick="$('#msg_container2').hide();">×</button>
<span id="msg_content2"><?=$this‑>session‑>flashdata('msg_data')?></span>
</div>
<?php } ?>
<div class="table‑responsive">
<table class="table table‑hover table‑bordered table_sortable">
<thead>
<tr>
<th class="s_header<?=($sortfield=='id' && $sort=='asc' ? ' s_asc' : ($sortfield=='id' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/id/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">ID</a></th>
<th class="s_header<?=($sortfield=='ime' && $sort=='asc' ? ' s_asc' : ($sortfield=='ime' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/ime/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Ime</a></th>
<th class="s_header<?=($sortfield=='prezime' && $sort=='asc' ? ' s_asc' : ($sortfield=='prezime' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/prezime/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Prezime</a></th>
<th class="s_header<?=($sortfield=='br_tel' && $sort=='asc' ? ' s_asc' : ($sortfield=='br_tel' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/br_tel/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Broj telefona</a></th>
<th class="s_header<?=($sortfield=='naziv_artikla' && $sort=='asc' ? ' s_asc' : ($sortfield=='naziv_artikla' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/naziv_artikla/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Naziv Artikla</a></th>
<th class="s_header<?=($sortfield=='sn_br' && $sort=='asc' ? ' s_asc' : ($sortfield=='sn_br' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/sn_br/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Serijski broj</a></th>
<th class="s_header<?=($sortfield=='gar_stanje' && $sort=='asc' ? ' s_asc' : ($sortfield=='gar_stanje' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/gar_stanje/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Pod garancijom</a></th>
<th class="s_header<?=($sortfield=='opis_kvara' && $sort=='asc' ? ' s_asc' : ($sortfield=='opis_kvara' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/opis_kvara/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Opis Kvara</a></th>
<th class="s_header<?=($sortfield=='opis_posla' && $sort=='asc' ? ' s_asc' : ($sortfield=='opis_posla' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/opis_posla/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Opis Posla</a></th>
<th class="s_header<?=($sortfield=='utro_mater' && $sort=='asc' ? ' s_asc' : ($sortfield=='utro_mater' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/utro_mater/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Utroseni materijal</a></th>
<th class="s_header<?=($sortfield=='comment' && $sort=='asc' ? ' s_asc' : ($sortfield=='comment' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/comment/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Komentar</a></th>
<th class="s_header<?=($sortfield=='datum' && $sort=='asc' ? ' s_asc' : ($sortfield=='datum' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/datum/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Datum zaprimanja</a></th>
<th class="s_header<?=($sortfield=='datum_otp' && $sort=='asc' ? ' s_asc' : ($sortfield=='datum_otp' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/datum_otp/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Datum otprimanja</a></th>
<th class="s_header<?=($sortfield=='task_status' && $sort=='asc' ? ' s_asc' : ($sortfield=='task_status' && $sort=='desc' ? ' s_desc' : ''))?>"><a href="<?=url('servis/s/'.$search.'/task_status/'.($sort!='desc' ? 'desc' : 'asc').'/'.$limit.'/'.$page_num)?>">Posao zavrsen</a></th>
</tr>
</thead>
<tbody>
<?php
if (!empty($task_data['serviss'])){
foreach ($task_data['serviss'] as $t_key => $t_value) { ?>
<tr>
<td><a href="<?=url('servis/info/'.$t_value['id'])?>"><?=$t_value['naziv_artikla']?></a></td>
<td><?=$t_value['ime']?></td>
<td><?=$t_value['prezime']?></td>
<td><?=$t_value['br_tel']?></td>
<td><?=$t_value['naziv_artikla']?></td>
<td><?=$t_value['sn_br']?></td>
<td><?=$t_value['gar_stanje']?></td>
<td><?=$t_value['opis_kvara']?></td>
<td><?=$t_value['opis_posla']?></td>
<td><?=$t_value['utro_mater']?></td>
<td><?=$t_value['comment']?></td>
<td><?=$t_value['datum']?></td>
<td><?=$t_value['datum_otp']?></td>
<td><?=$this‑>selects‑>task_status('','',$t_value['task_status'],1)?></td>
</tr>
<?php }}else{ ?>
<tr>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
<td>No Information</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?=$pagination_links?>
controllers/Servis.php
public function s(){
if ($this‑>session‑>userdata('user_id')==''){
header('Location: '.url('home'));
}else{
$data = array();
$data['search'] = urldecode(($this‑>uri‑>segment(4)!='' ? $this‑>uri‑>segment(4) : 'search'));
$data['sortfield'] = ($this‑>uri‑>segment(5)!='' ? $this‑>uri‑>segment(5) : 'default');
$data['sort'] = ($this‑>uri‑>segment(6)!='' ? $this‑>uri‑>segment(6) : 'sort');
$data['limit'] = ($this‑>uri‑>segment(7)!='' ? $this‑>uri‑>segment(7) : 20);
$data['page_num'] = ($this‑>uri‑>segment(8)!='' ? $this‑>uri‑>segment(8) : 1);
$data['task_data'] = $this‑>master‑>get_tasks($data['limit'],'',$data['page_num'],$data['search'],$data['sortfield'],$data['sort']);
$this‑>load‑>library('pagination');
$config['uri_segment'] = 8;
$config['num_links'] = 2;
$config['base_url'] = url('servis/s/'.$data['search'].'/'.$data['sortfield'].'/'.$data['sort'].'/'.$data['limit']);
$config['total_rows'] = $data['task_data']['total_rows'];
$config['per_page'] = $data['limit'];
$config['use_page_numbers'] = TRUE;
$this‑>pagination‑>initialize($config);
$data['pagination_links'] = $this‑>pagination‑>create_links();
$data['meta']['title'] = 'Servis';
$theme = $this‑>master‑>get_theme();
$this‑>load‑>view($theme.'/servis',$data);
}
}
如果有人知道如何解決這個問題以及女巫導致這個問題的原因,請聯繫我或寫答案。
非常感謝致所有試圖克服錯誤的人。
在我更改代碼之後
@Sugumar Venkatesan
在我更改之後:foreach ($task_data['serviss ' ] as $t_key => $t_value) {} to foreach($task_data as $t_key => $t_value) {}
現在我收到消息“無法找到”,顯然現在是檢索數據的問題從數據庫。我更改代碼後的看法
而且,@Alex Andrei 這裡是來自 var_dump 的迴聲....
array(14) { ["id"]=> string(15) "Unable to find!" ["ime"]=> string(15) "Unable to find!" ["prezime"]=> string(15) "Unable to find!" ["br_tel"]=> string(15) "Unable to find!" ["naziv_artikla"]=> string(15) "Unable to find!" ["sn_br"]=> string(15) "Unable to find!" ["gar_stanje"]=> string(15) "Unable to find!" ["opis_kvara"]=> string(15) "Unable to find!" ["opis_posla"]=> string(15) "Unable to find!" ["utro_mater"]=> string(15) "Unable to find!" ["comment"]=> string(15) "Unable to find!" ["datum"]=> string(15) "Unable to find!" ["datum_otp"]=> string(15) "Unable to find!" ["task_status"]=> string(1) "0" }
參考解法
方法 1:
I think
foreach ($task_data['serviss'] as $t_key => $t_value) {}
should be
foreach($task_data as $t_key => $t_value) {}
(by Filip Radicevic、Sugumar Venkatesan)