I have some values in my db (type string: ex id: 0001) but when I fetch data, CI convert it to int and I get 1 as result. How to avoid it? I want to display the db's value 0001 in the front
you can do
Model
function getValues(){
$query = $this->db->get('table_name');
$result = array();
foreach ($query->result() as $row) {
$id = str_pad($row->id, 4, '0', STR_PAD_LEFT);
$result[$id] = $row->type;
}
return $result;
}
Controller
$result = $this->model_var->getValues();
$data['result'] = $result;
$this->load->view('view_name',$data);
View
foreach($result as $key => $val) {
echo 'key: ' . $key . ', value: ' . $val;
}
Related
I am constructing an array in Laravel 5.7. I would like to paginate it, but it errors because of array feature. what shall I do?
In MyController.php:
$result = array();
foreach ($sameDateMatches as $key => $date) {
array_push($result, [
'date' => $key,
'day_of_week' => getDayOfWeek($key),
'matches' => $date,
]);
}
if (!empty($_GET['page'])) {
$result = $result->paginate(10);
$pagination = $result;
} else {
$result = $result;
$pagination = null;
}
return returnSuccessfulResponse(
trans('api.response.successful.show'),
[
'Scheduled Matches' => $result,
],
$pagination
);
Call to a member function paginate() on array
You can only use paginate on an instance of QueryBuilder or on an Eloquent query.
Instead, if you need to 'paginate' an array, you can use PHP array_chunk.
array_chunk($result, 10, true);
I solved my problems using make() and LengthAwarePaginator as below:
if (!empty($_GET['page'])) {
$per_page = !empty($_GET['per_page']) ? $_GET['per_page'] : 10;
$page = $_GET['page'];
$result = $result instanceof Collection ? $result : Collection::make($result);
$result = new LengthAwarePaginator(
$result->forPage($page, $per_page)->values(),
$result->count(),
$per_page,
$page,
['path' => request()->url()]
);
$pagination = $result;
} else {
$pagination = null;
}
need help what wrong with my code where this my sql command where successfull run in mysql :
SELECT func_inc_var_session() As row_number ,
vruasjalan2.*
FROM vruasjalan2 Where skid = #skid
ORDER BY kategoriruasjalanid, subkategoriruasjalanid,ruasjalanid;"
but when i want to display in my controller row_number start error say"Call to undefined method Jalan_model::num_rows()"
public function ajax_list()
{
$list = $this->Jalan->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $Jalan) {
$no++;
$row = array();
$row[] = $no;
$row[] = $Jalan->row_number;
$row[] = $Jalan->ruasjalanid;
$row[] = $Jalan->skid;
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Jalan->count_all(),
"recordsFiltered" => $this->Jalan->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
}
and this is my model
public function get_list_companys()
{
$this->db->select('vruasjalan2.*');
$this->db->from($this->table);
$this->db->order_by('kategoriruasjalanid','asc');
$query = $this->db->get();
$result = $query->result();
$companys = array();
foreach ($result as $row)
{
$companys[] = $row->kategoriruasjalanid;
}
return $companys;
}
if there any expert know this problems
I want to render an array and print the output as Block[content]. There is either my Foreach or the way I use $item array or $row that is wrong. I will appreciate your help.
function produkt_block_view($delta = '')
{
$block = array();
$list = produkt_produkliste(1);
$items = array();
foreach ($list as $row) {
$items[1] = $row->uri;
$items[2] = $row->title;
$items[3] = $row->body_value;
}
switch($delta) {
case 'produkt' :
$block['content'] = '<a href="/19">';
$block['content'] .= '<img class="pimg" src="'.$row->uri.'" alt="Se alle vores filter elementer her">';
$block['content'] .= '<p class="pname">"'.$row->title.'"</p>';
$block['content'] .= '<p>"'.$row->body_value.'"</p>';
$block['content'] .= theme ('item_list',array('items' => $items, 'type' => 'ol', 'attribute' => 'produkt'));
break;
}
return $block;
}
I am trying to get some data from a table but when i use foreach it returns only 1 character or the 1st letter/number and one row is returned.
Here is my code.
home.class.php
public function getData()
{
$aRow = $this->database()->select('*')
->from('tablename')
->execute('getSlaveRow');
return $aRow;
}
ajax.class.php
public function getArr()
{
$data = 'No data found';
$results = Phpfox::getService('files.home')->getData();
if($results) {
$data = '<div id="fileparse" style="height:295px;overflow:auto;display:none;"</div>';
$data .= '<div id="filelist" style="height:295px;overflow:auto;">';
$data .= '<table style="width:100%;"><tr><td><b>File Name</b></td><td> <b>Account Type</b></td><td><b>Account Number</b></td><td><b>Company</b></td><td><b>Results</b></td></tr>';
foreach($results as $result) {
$data .= '<tr>';
$data .= '<td>'.$result['file'].'</td>';
$data .= '<td>'.$result['result'].'</td>';
$data .= '</tr>';
}
$data .= '</table>';
$data .= '</div>';
} else
$data = 'No results found';
$this->html('#eqblock', $data);
}
use getSlaveRows insted of getSlaveRow
getSlaveRow return single array while getSlaveRows return all rows
I have these codes on my add view
$status = array('0' => 'Resolved', '1' => 'Assigned/Unresolved', '2' => 'Suspended', '3' => 'Closed', '4' => 'Bypassed');
echo $this->Form->input('status', array('options' => $status));
and instead of saving the value (e.g. Resolved) to the table, it saves the index of the array. Any ideas?
You should do something like this:
$status = array('resolved' => 'Resolved', 'assigned' => 'Assigned/Unresolved'...);
It saves the index of the array. But this is not a good practice, instead try using enums. Check this out:
/**
* Get Enum Values
* Snippet v0.1.3
* http://cakeforge.org/snippet/detail.php?type=snippet&id=112
*
* Gets the enum values for MySQL 4 and 5 to use in selectTag()
*/
function getEnumValues($columnName=null, $respectDefault=false)
{
if ($columnName==null) { return array(); } //no field specified
//Get the name of the table
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);
//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");
//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) ) { $types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default']; } //MySQL 5
elseif ( isset( $result[0][0]['Type'] ) ) { $types = $result[0][0]['Type']; $default = $result[0][0]['Default']; } //MySQL 4
else { return array(); } //types return not accounted for
//Get the values
$values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );
if($respectDefault){
$assoc_values = array("$default"=>Inflector::humanize($default));
foreach ( $values as $value ) {
if($value==$default){ continue; }
$assoc_values[$value] = Inflector::humanize($value);
}
}
else{
$assoc_values = array();
foreach ( $values as $value ) {
$assoc_values[$value] = Inflector::humanize($value);
}
}
return $assoc_values;
} //end getEnumValues
Paste that method in your AppModel.
Create the column in your table as an enum with the posible values, and use that method to get them.
Can you not define the index as the value you want?
$status = array('Resolved' => 'Resolved', 'Assigned/Unresolved' => 'Assigned/Unresolved', etc etc );
echo $this->Form->input('status', array('options' => $status));