I want to populate array record in controller, what should I do?
$data['list'] = $this->tshirt_model->getAllModelType(0,0,1); //this working
foreach($data['list']->result_array() as $row) :
$info =array('name'=>$row['name']);
endforeach;
print_r($info); die(); //when i tracing, only returned last record. it should be 20 records.
Yes it will be the last record because $info is just array as I can see.
Try using:
$info[] =array('name'=>$row['name']);
in some versions of php you shoud use:
foreach($data['list']->result_array() as $key => $row) :
$info[$key] =array('name'=>$row['name']);
endforeach;
Related
I add my session variables like this:
foreach ( $data as $key => $value ) {
$this->Session->write("MyVariable.$key", $value );
}
Is it possible to add element to session variable array without passing the key ?
I mean like this:
$MyArray[] = "apple";
$MyArray[] = "banana";
So is it possible to add like this? Pseudo code:
$this->Session->write('MyVariable'.[], "apple");
$this->Session->write('MyVariable'.[], "banana");
Edit: $data array was for giving an example. The data that will be saved is not array. It is a string. Everytime I add to session variable I don't want to give key by code. I wonder whether is it possible out of the box. In my current codes I make it like this:
$newKey = count( $this->Session->read("MyVariable") );
$this->Session->write("MyVariable.$newKey", "apple");
Hi i guess it should be like this:
foreach ( $data as $key => $value ) {
$this->Session->write('MyVariable.'.$key, $value );
}
You have to place a dot inside the quotation mark.
If you don't want to give the key value each time, then store it as an array like #mark said
$this->Session->write("MyVariable", $data);
If you want to add a new value to the $data array in some other part of your code, you'll have to do something like
$data = $this->Session->read("MyVariable");
$data[] = array('other'=>'value');
$this->Session->write("MyVariable", $data);
Or add the exact key like #mmahgoub said
$this->Session->write("MyVariable".$key, $value);
I wish to display the results of a database query in my view whilst giving each row a unique ID.
My model is:
function get_load_lines($ordernumber)
{
$this->db->select('product,pricepercube,qty,linecost,linediscount,cubicvolume,bundles,treated,autospread');
$this->db->from('Customer_Order_Lines');
$this->db->where('CustomerOrderID',$ordernumber);
$q = $this->db->get();
if($q->num_rows() > 0)
{
return $q->row_array();
}
}
My controller is:
function edit_order_lines()
{
$data = array(
'ordernumber' =>$this->input->post('ordernumber'),
'customer' =>$this->input->post('customer'),
'loadlines' => $this->sales_model->get_load_lines($this->input->post('ordernumber'))
);
$this->load->view('sales/edit_order_lines',$data);
}
As mentioned I want to display each of these rows in my view.
So I use this code in my view:
<table>
<?php
$i=1;
foreach ($loadlines as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['pricepercube']."</td>";
echo "<td>".$row['qty']."</td>";
echo "</tr>";
$i++;
}
?>
</table>
This does not have the intended result. $i increments for each array entry, not each array line.
Any advice on the best way to display this data?
So if 3 rows the below is required:
In your model, try returning a result_array, rather than a row_array:
return $q->result_array();
If more than one result exists, result array will return all of the results, whereas row array will only return one. CodeIgniter's user guide explains the differences.
You could also make your for loop at little bit tidier, by incrementing $i like this:
$i=1;
foreach ($loadlines as $row)
{
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['pricepercube']."</td>";
echo "<td>".$row['qty']."</td>";
echo "</tr>";
}
I'm not sure I understand you question clearly, but in order to edit Customer Order Line (order details) you should also pull the ID of each Customer_Order_Lines when you query the database. (assuming your table Customer_Order_Lines has primary key filed called ID).
$this->db->select('id,product,pricepercube,q...');
Then when you loop through the results, do:
foreach ($loadlines as $row)
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['pricepercube']."</td>";
echo "<td>".$row['qty']."</td>";
echo "</tr>";
$i++;
}
This will give you the specific unique ID's (primary keys) of each Order Line. Then, you can updated each Order Line by referring to this ID.
Let me know if I misunderstood your question.
Is there a simple way to count all the queries executed on a CakePHP page?
I will just need a number, representing how many queries were executed from my AppController start, to the end of the page render.
This is basically an edit of the sql_dump element. Which resides in lib/cake/view/elements.
It is a starting point for you anyway.
$sources = ConnectionManager::sourceList();
$logs = array();
foreach ($sources as $source):
$db = ConnectionManager::getDataSource($source);
if (!method_exists($db, 'getLog')):
continue;
endif;
$logs[$source] = $db->getLog();
endforeach;
$queries = 0;
foreach ($logs as $source => $logInfo):
$queries += $logInfo['count'];
endforeach;
The code below is only printing the first letter of each array item. It's got me quite baffled.
require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));
$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
echo $tag['$index'];
$index = $index + 1;
}
print_r return:
Array ( [0] => Wallpapers [1] => Free )
the loop:
WF
Try echo $tag, not $tag['$index']
Since you are using foreach, the value is already taken from the array, and when you post $tag['$index'] it will print the character from the '$index' position :)
It seems you've attempted to do what foreach is already doing...
The problem is that you're actually echoing the $index letter of a non-array because foreach is already doing what you seem to be expecting your $index = $index+1 to do:
require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));
$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
echo $tag; // REMOVE [$index] from $tag, because $tag isn't an array
$index = $index + 1; // You can remove this line, because it serves no purpose
}
require_once 'includes/global.inc.php';
// Store the value temporarily instead of
// making a function call each time
$tags = $site->bookmarkTags(1);
foreach ($tags as $tag) {
echo $tag;
}
This should work. The issue might be because you're making a function call every iteration, versus storing the value temporarily and looping over it.
I am able to access and output a full array list of Zip items like so (this is working as expected):
... (this is a foreach within a foreach)
foreach ($plan_edit['Zip'] as $zip) :
echo $zip['title'] . "<br />";
endforeach; ...
Returns:
Array
(
[0] => Array
(
[id] => 110
[state_id] => 1
[title] => 97701
[PlansZip] => Array
(
[id] => 83698
[plan_id] => 443
[zip_id] => 110
)
)
[1]
I am trying to ONLY get the first and last value (of ['title']) of each array set for each main record.
I've been messing around with phps array current() and end() functions, but I can only get "Array
" to print out with those.
I know I am doing something wrong, but kind of lost direction at this point.
Any constructive criticism of my work/methods is welcome.
This is where I am at currently:
<?php
foreach ($plan_edit['Zip'] as $zip) :
echo current($zip['title']) . "<br />";
endforeach;
foreach ($plan_edit['Zip'] as $zip) :
echo end($zip['title']) . "<br />";
endforeach;
?>
$first = reset($plan_edit['Zip']);
$last = end($plan_edit['Zip']);
echo $first['title'];
echo $last['title'];
If the array is numerically indexed, you can also just do:
echo $plan_edit['Zip'][0]['title'];
echo $plan_edit['Zip'][count($plan_edit['Zip']) - 1]['title'];