I have following method:
public function actionIndex() {
$companyModel = Company::model()->findAll();
$supplierProductModel = SupplierProduct::model()->findAll();
$this->render('index', array(
'companyData' => $companyModel,
'supplierProductData' => $supplierProductModel,
));
}
Here I have passed model objects to render function and want to access these objects in view (Active Relational Type) but when I am accessing its in view its showing error:
Trying to get property of non-object
view file (index.php)
echo $companyData->name . "\n";
echo $this->companyModel->phone . "\n";
echo $this->companyModel->fax . "\n";
echo $this->companyModel->cell . "\n";
Any help would be appreciated.
you need to declare $this->companyModel in your controller/action
$this->companyModel = Company::model()->findByPk($companyId);
with Company::model()->findAll() you get an array of Company-Models you can iterate over in your view-file.
foreach ($companyData as $companyModel) {
var_dump($companyModel->attributes);
}
It is happening becoz of findAll()
findAll() reruns all rows of company table in multidimensional array, so here
$companyData is multidimensional Array, now change your code in index like bellow,
<?php
foreach ($companyData as $compSub)
{
echo $compSub->name . "\n";
echo $compSub->phone . "\n";
echo $compSub->fax . "\n";
echo $compSub->cell . "\n";
}
?>
If you want a company data(single row), change your query like this
$companyModel = Company::model()->findByPk($id_Of_company);
//$companyModel is single dimensional array, it has all the info of a company.
Send this to view
$this->render('index', array(
'companyData' => $companyModel,
....................
));
Now you can show the data using bellow code
echo $companyData->name . "\n";
echo $companyData->phone . "\n";
echo $companyData->fax . "\n";
echo $companyData->cell . "\n";
You are trying to get all the entries from the database as findAll() returns all data in multidimensional array of objects.If you need all the entries you could iterate over it in the view file and get the results as shown
In the View File do as shown
<?php foreach($companyData as $value){
echo $vlaue->name . "\n";
echo $value->phone . "\n";
echo $value->fax . "\n";
echo $value->cell . "\n";
?>
With this you get all the entries from the table
If you want to get a particular record use condition using Cdbcriteria and pass the object and get the single result
Related
everyone.
In the codeigniter there is update_batch function by using it we are able to bulk update multiple rows.
$this->db->update_batch('table_name', $update_data_array, 'where_condition_field_name');
I want similar functionality in core PHP in one function or in one file. Is there any workaround?
Is there any way to extract update_batch function from Codeigniter in one file/function?
I tried to extract this function but it is very lengthy process and there will be many files / functions should be extracted.
Please help me in this regard
Thanks in advance
You can also insert multiple rows into a database table with a single insert query in core php.
(1)One Way
<?php
$mysqli = new mysqli("localhost", "root", "", "newdb");
if ($mysqli == = false) {
die("ERROR: Could not connect. ".$mysqli->connect_error);
}
$sql = "INSERT INTO mytable (first_name, last_name, age)
VALUES('raj', 'sharma', '15'),
('kapil', 'verma', '42'),
('monty', 'singh', '29'),
('arjun', 'patel', '32') ";
if ($mysqli->query($sql) == = true)
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. "
.$mysqli->error;
}
$mysqli->close();
? >
(2)Second Way
Let's assume $column1 and $column2 are arrays with same size posted by html form.
You can create your sql query like this:-
<?php
$query = 'INSERT INTO TABLE (`column1`, `column2`) VALUES ';
$query_parts = array();
for($x=0; $x<count($column1); $x++){
$query_parts[] = "('" . $column1[$x] . "', '" . $column2[$x] . "')";
}
echo $query .= implode(',', $query_parts);
?>
You can easily construct similar type of query using PHP.
Lets use array containing key value pair and implode statement to generate query.
Here’s the snippet.
<?php
$coupons = array(
1 => 'val1',
2 => 'va2',
3 => 'val3',
);
$data = array();
foreach ($coupons AS $key => $value) {
$data[] = "($key, '$value')";
}
$query = "INSERT INTO `tbl_update` (id, val) VALUES " . implode(', ', $data) . " ON DUPLICATE KEY UPDATE val = VALUES(val)";
$this->db->query($query);
?>
Alternatively, you can use CASE construct in UPDATE statement. Then the query would be something
like:
UPDATE tbl_coupons
SET code = (CASE id WHEN 1 THEN 'ABCDE'
WHEN 2 THEN 'GHIJK'
WHEN 3 THEN 'EFGHI'
END)
WHERE id IN(1, 2 ,3);
this code in controller gives me five rows but when i use it in view file it just returns first row like:
a
b
c
d
e
controller:
$data = [
'lesson' => $x->name,
];
echo $data['lesson'].'</br>';
$this->view('manage/add-cat', $data);
view:
echo $data['lesson'].'</br>';
it returns just:
a
!!!
I've tried:
foreach($data['lesson'] as $y):
echo $y;
endforeach;
I'd be thankful if you help :)
How can I show an associative array in codeigniter view from controller page?
Here is my code below. I want to show the values of array in view.
Please help me!
function colorr(){
$color['color'] = array("RED"=>"#FFCC00","GREEN"=>"#99FF00","YELLOW"=>"#FF0000");
$this->load->view('color_all',$color);
}
In your view file, you can access it by:
print $color["RED"]; // will output "#FFCC00"
print $color["GREEN"]; // will output "#99FF00"
Or you can loop:
foreach($color as $k => $v)
{
print $k . " => " . $v . " <br />";
}
In your case, this will output something like this:
RED => #FFCC00
GREEN => #99FF00
YELLOW => #FF0000
I have this problem on Code Igniter, with a library containing my own validation rules (MY_Form_validation).
I want to check if a field is populate, only if another one is populate. Field2 is required only if field1 is filled.
Here the lines on my controller :
for($j = 1 ; $j < $i ; $j++) {
$this->form_validation->set_rules('day' . $j, 'Jour ' . $j, 'dateIsPosteriorToField[start_date]|trim|xss_clean');
$this->form_validation->set_rules('alert' . $j, 'Alerte jour ' . $j, 'conditionalRequired[day' . $j . ']|trim|xss_clean');
}
(i have multiple fields like that, so I get them with a loop)
And my form validation :
function conditionalRequired($valueTocheck, $conditionalField)
{
$this->set_message('conditionalRequired', 'Le champ %s est requis.');
$conditionalValue = $this->CI->input->post($conditionalField);
if( ! empty($conditionalValue) && empty($valueTocheck)) {
return false;
}
return true;
}
Problem : if field2 is filled, CI goes into the rule and match it. If field2 is empty, CI does NOT go into the rule. I can see that by dumping any value in the rule, it simply ignores the rule if field2 is empty.
There must be something obvious I don't see, please help me !
This might not be the solution you are looking for but you could get around your problem by doing the following.
for($j = 1 ; $j < $i ; $j++) {
$this->form_validation->set_rules('day' . $j, 'Jour ' . $j, 'dateIsPosteriorToField[start_date]|trim|xss_clean');
if ($this->input->post('day' . $j)) {
$this->form_validation->set_rules('alert' . $j, 'Alerte jour ' . $j, 'required|trim|xss_clean');
}
}
this way if the 'day' . $j POST is set the 'alert' . $j is set to be required.
Doing it like this means that you don't need to extend the form_validation class.
I've been looking everywhere but haven't found the answer to the question: How can I use my own thumbnail per feed in a multifeed page? If I've overlooked the answer it would be due to the wrong search keys for which i apologize in advance.
I've got the following code so far (which is not much):
<?php
include_once('autoloader.php');
include_once('idn/idna_convert.class.php');
date_default_timezone_set('Europe/Amsterdam');
setlocale(LC_TIME, 'nld_nld');
$feed = new SimplePie();
$urls = (array(
'http://myfeedurla.com' => 'descriptiona',
'http://myfeedurlb.com' => 'descriptionb',
'http://myfeedurlc.com' => 'descriptionc'
));
$feed->set_feed_url(array_keys($urls)); // this should get parse only the URLS
$feed->set_cache_location('cache/');
$feed->init();
$feed->handle_content_type();
?>
// As from here I'm using the standard simplepie code to show multiple feeds and order them by day
<?php
// Set up some variables we'll use.
$stored_date = '';
$list_open = false;
// Go through all of the items in the feed
foreach ($feed->get_items() as $item)
{
// What is the date of the current feed item?
$item_date = $item->get_date('M jS');
// Is the item's date the same as what is already stored?
// - Yes? Don't display it again because we've already displayed it for this date.
// - No? So we have something different. We should display that.
if ($stored_date != $item_date)
{
// If there was already a list open from a previous iteration of the loop, close it
if ($list_open)
{
echo '</ol>' . "\r\n";
}
// Since they're different, let's replace the old stored date with the new one
$stored_date = $item_date;
// Display it on the page, and start a new list
echo '<h1>' . $item->get_local_date('%A %d %B %Y') . '</h1><hr>' . "\r\n";
echo '<ol>' . "\r\n";
// Let the next loop know that a list is already open, so that it will know to close it.
$list_open = true;
}
// Display the feed item however you want...
echo '<li>' . $item->get_local_date('%H:%M') . ' | <h4>' . $item->get_title() . '</h4></li>' . "\r\n";
}
?>
Somewhere in the HTML I want to add the 'img scr=" . . "' etc. And there it should only refer to an image by the name 'descriptiona'.png respectively 'descriptionb'.png etc, per feed. Is this possible? And if yes, how?
If I should be more clear, pls don't hesitate to ask. Thanks in advance for the help!
Best regards,
Ok, so far I have one solution :) I use the title of the respective feeds in the img filename. I've added the following code:
<img src="images/' . $item->get_feed()->get_title() . '.png" width="12" height="12" />
Now I only hope that feeds won't use / or something in their titles... If someone has a better way of doing this, eg. by being able to add a personal title per feed, that would be very welcome.