Codeigniter Model: How to use values from a query for further calculations? - arrays

i have a function in a model with the following query, and i want to make some calculations with value1, value2 and value3 outside the query. how to get the values out of this result()?
function ($id,$username$) {
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where("username=$username and id = $id");
$query=$this->get();
$result = $query->result();
$result['$testvalue'] = (value1 * value2/113) + value3;
return $result; }
can someone help? i already found out how to use values from a table, but not from a query!
ok now i know how to use row_array.
i call the row_array in the controller
$data = $this->model_testmodel->testfunction($id, $username);
...
$this->load->view('pages/view_test', $data);
now i wanna know how the view would look like.
i tried many different ways.
in the moment i am stuck with ...
<?php foreach $data as $value: ?>
<label>Value1:</label>
<input type="text" name="value1" value="<?php echo $value['value1']; ?>">
<label>Value2:</label>
<input type="text" name="value2" value="<?php echo $value['value2']; ?>">
...
<?php endforeach; ?>
but i get two errors:
Message: Undefined variable: data
Message: Invalid argument supplied for foreach()

Try this...
function ($id,$username)
{
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where(array('id'=>$id,'username'=>$username));
$query=$this->db->get();
$result = $query->row();
$testvalue = (($result->value1 * $result->value2/113) + $result->value3);
//test here
echo $testvalue;
$res = array();
$res['testvalue'] = $testvalue;
$res['value1'] = $result->value1;
$res['value2'] = $result->value2;
$res['value3'] = $result->value3;
return $res;
}
It returns $res as array.Easy to execute of you got problem comment.. if it works accept.Lets enjoy..
Then make a function call and accept array in a variable..like this..
first load model then make function call.from your controller
$this->load->model('model_name');
$data= $this->model_name->function_name($id,$username);
//Fetch returned array like this..
echo $data['testvalue'];
echo $data['value1'];
//so on

Related

Return all array values with ACF fields and foreach loop

I created a checkbox field in ACF and want to loop this field again and again and show all these values. First I tried it the way the ACF documentation shows, but the following code results in just the value of the first checked checkbox.
function ms_get_department(){
$departments = get_field('vakgebied');
if($departments):
foreach($departments as $department):
echo '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
endif;
}
I also tried to store all the values inside an array but in the code below it just shows 'Array' and don't know how to show all these data in this case.
function ms_get_department(){
$departments = get_field('vakgebied');
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Does anyone know how I can solve this problem in a proper way?
It's not clear where you're adding this function. If it's on the single page then the code should work; however, if it's on any other page then you'd need to pass the post ID to ACF Field.
function ms_get_department(){
$departments = get_field('vakgebied', 123); // 123 being the post ID
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Another thing to check is, make sure ACF field is returning both 'label' and 'value'.

WPDB exporting custom query to csv - file not being created

I'm using the following code to export a custom MYSQL query to CSV. The data and column headers are working fine (print $csv_output; is exactly as I expect) but the actual CSV file is not being created.
What am I getting wrong?
function generate_csv(){
global $wpdb;
$table_name = $wpdb->prefix."ck_shipment";// table name
$file = 'database_csv'; // csv file name
$results = $wpdb->get_results("SELECT * FROM $table_name",ARRAY_A );
// get column names
$query = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='".$wpdb->dbname."' AND `TABLE_NAME`='".$table_name."'";
$columnNamesList = $wpdb->get_results($query);
foreach ( $columnNamesList as $column_name ) {
$csv_output.=$column_name->COLUMN_NAME.",";
}
// remove last additional comma
$csv_output = substr($csv_output,0,strlen($csv_output)-1);
// start dumping csv rows in new line
$csv_output.="\n";
if(count($results) > 0){
foreach($results as $result){
$result = array_values($result);
$result = implode(", ", $result);
$csv_output .= $result."\n";
}
}
$filename = $file."_".date("Y-m-d_H-i",time());
print $csv_output;
exit;
}
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<br /><input type="submit" name="export" value="export" class="button-primary">
</form>
<?php
if (isset($_POST['export'])) {
generate_csv();
}
Thanks!

Displaying Data from a Join in Codeigniter 2.2.1

model
function select_nip($data){
$query = $this->db->select('mx_pegawai.nama, mx_pegawai.nip, mx_jabatan.unit_kerja AS unit')
->from('mx_pegawai')
->join('mx_jabatan', 'mx_pegawai.nip = mx_jabatan.nip')
->where('mx_pegawai.nip', $data)
->get();
return $query;
}
view
$i=1;
foreach($hasil->result() as $row){
echo '
<tr>
<td>'.$i.'</td>
<td>'.$row->nama.'</td>
<td>'.$row->nip.'</td>
<td>'.$row->unit.'</td>
</tr>
';
$i++;
}
Error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$unit
Filename: home/cari.php
Line Number: 50
$this->db->select() accepts an optional second parameter.
If you set it to FALSE, CodeIgniter will escaping your field. This is useful if you need a compound select statement where automatic escaping of fields may break them.
$query = $this->db->select('mx_pegawai.nama, mx_pegawai.nip, mx_jabatan.unit_kerja AS unit')
->from('mx_pegawai')
->join('mx_jabatan', 'mx_pegawai.nip = mx_jabatan.nip')
->where('mx_pegawai.nip', $data)
->get();
return $query;

CakePHP trouble with posting from select to controller

The value of my select tag doesn't seem tto post to my controller, no matter what I try
The select tag
<select name="whatever">
<?php
foreach($packs as $packName => $pack) {
echo " '<option value=" . $packName . '">' . $packName . '</option>';
}
?>
</select>
Where I try to use it in controller
function procedures() {
$errors = array();
$otsing= "";
if (!isset($this->data)) {
App::import('Helper', 'Formatter');
$formatter = new FormatterHelper();
$this->data['start'] =
$formatter->FormatDate($this->Dating->Now());
$this->data['end'] = $formatter->FormatDate($this->Dating->Now());
if(!empty($_POST['whatever']))
{
$otsing = $this->$_POST['whatever'];
}
}
}
The select name should be wriiten like
data[Formname][selectname]
if you want to give it in HTML format or you should use cakephp way to define dropdwon:
<?php
echo $form->select(‘whatever’,$packs)
?>

Fatal error: Call to undefined function text_view

I have a basic code here:
<?
include("inc_dblib.php");
include("inc_ecs.php");
$db = dbconnect();
$id = 10;
?>
<?php echo text_view($db,$id,"<h3><br />^lead^</h1> <br />^text^");?>
<br />
inside inc_ecs.php i have:
function text_view($dblink,$id,$code) {
if( !$rset = dbquery($dblink,"article_view",$id) )
return FALSE;
$item = mysql_fetch_assoc($rset);
$text=$item["text"];
$title=$item["title"];
$lead=$item["lead"];
$capelo=$item["capelo"];
$author=$item["author"];
$vowels = array("^text^","^title^","^capelo^","^lead^", "^author^");
$yummy = array($text, $title, $capelo, $lead,$author);
$code = str_replace($vowels,$yummy,$code);
return $code;
}
however every time I run my script it tells me
Fatal error: Call to undefined function text_view
. Any ideas? Thanks.
Ok
so I found an other problem.
I have tried to insert the echo "Hello World!"; in the code of the inc_ecs.php.
When I browsed the page, I realised that a major part the code it is shown as text.
I turned back to the remote version and it shows a blank page when called in a browser.
the inc_ecs.php page start showning the code from "return $outputVar;" and the remaining hole code of the page is shown:
function graphical_counter ($db, $id){
$str = counter($db, $id);
$visitors_split = chunk_split ($str,1,'');
$visitors = strlen($str);
for ($i ; $i< $visitors ; $i++){
$outputVar .= "<img src='./images/counter/".$visitors_split[$i].".gif' width='15' height='20' border='0' align='absmiddle'>";
}
return $outputVar;
}
/*
End Counter Functions
Is there an error in this code ?
your include(); should be inside of the tag:
<?php
include("inc_ecs.php");
echo text_view($db,$id,"<h3><br />^lead^</h1> <br />^text^");
?>

Resources