I want to display the specific data from database using <?= Html::encode() ?>
Let say, i get the specific column of the model as below:
<?php $model = ExampleModule::find()->select('anycolumn')->all(); ?>
And then, what should i write to the <?= Html::encode(anystatement) ?>to display the values ?
you can encode the single column result and ->all() return a collection fo models
so first you should access to single model, eg: assuming you obtain your collection of models as an array
<?php $model = ExampleModule::find()->select('anycolumn')->asArray->()all(); ?>
you can encode the single column result for the first model this way
<?= Html::encode($model[0]['your_column']); ?>
Get all data first .
First step
<?php $model = ExampleModule::find()->select('anycolumn')->asArray()->all(); ?>
Second step
<?= Html::encode($model[0]['anycolumn']); ?>
First of all ExampleModule::find()->select('anycolumn')->all() returns an array of records.
If you want to get first found record you need to use
<?php
$model = ExampleModule::find()->select('anycolumn')->one();
?>
Then
<?= Html::encode($model->anycolumn) ?>
Or if you want to display all records:
<?php
foreach (ExampleModule::find()->select('anycolumn')->all() as $model) {
echo Html::encode($model->anycolumn) . '<br>';
}
?>
Related
I have created a Mongo DB with a collection which has two fields first_name and last_name for 4 of the entries and three fields (two same as above) and "gender" for the other 3 entries.
Now my aim is to create a button and whenever we click it an excel file is downloaded. I have used phpSpreadsheet for this and MongoDB/MongoDB for the database connection.
The problem raises when I try to iterate through the columns and the rows of the database to save the data in an excel file. Obviously, it is because of the gender field which is not set in the rest of the entries.
I would like to know if there is any way I can save the NULL entries with a blank space in the excel file without changing the database itself.
Here is my Model/user_model.php
<?php
class User_model extends CI_Controller{
public function start_conn(){
/*Code for mongo connection...*/
/*Assuming we just want to echo the data rather than saving in excel*/
$cursor = $database->find();
return $cursor;
}
}
?>
Here is my Controller/user.php
<?php
class User extends CI_Controller{
public function index(){
$this->load->model('user_model');
$data['result'] = $this->user_model->start_con();
$this->load->view('user_view', $data);
}
}
?>
Here is my View/user_view.php
<html>
<head>
<title>
database
</title>
</head>
<body>
<?php
foreach($result as $object){
echo $object->first_name." ".$object->last_name." ".$object->gender."<br>";
}
?>
</body>
</html>
Where should I check for empty field and if it's empty how should I print a white space in its place ?
As far i understand your requirement,
Your view code should be like this ;
Note : make sure $result is an array of objects
<?php
foreach($result as $object)
{
/*check gender here if and set to blank('') if has not a value*/
$gender = ! empty($object->gender) ? $object->gender : '';
echo $object->first_name." ".$object->last_name." ".$gender."<br>";
}
?>
I want to store the list of data into session with single line and then how to extract that data on view.userData variable contains list of data.I can store the data like below that i know.but instead of writing multiple line can i store with single line.and how can i extract that data to use on view.Thanks in Advance.
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('userdata',$userData['id']);
$this->session->set_userdata('username',$userData['given_name'];
Can I store the data like below?
$this->session->set_userdata('userdata',$userData);
How can use the variable on view like this it is giving nothing
<?php if(!empty($userdata['given_name'])){?>
<li>HI <?php echo $userdata['given_name'];?></li>
<?php }
After reading the Codeigniter User Guide on sessions, where this is explained ( as we all have ), you could do the following...
This is demonstration code which is really good for testing stuff out like this.
The View auth_view.php for want of a better name
<h1> View </h1>
<?php if(!empty($this->session->given_name)){?>
<li>Hi <?= $this->session->given_name;?></li>
<?php }
The Controller
// $userData=$google_oauthV2->userinfo->get();
// Recreate the Array from google_oauthV2
$userData['id'] = 1;
$userData['given_name'] = 'Fred Flintstone';
$this->session->set_userdata($userData); // Save ALL the data from the array
var_dump($this->session->userdata()); // DEBUG- Lets look at what we get!
$data = $this->load->view('auth_view', NULL, true);
echo $data;
Does that help explain the possibilities?
I highly recommend reading the user guide, because I did to get this answer.
This is hard for me to answer as I don't know how the set_userdata() function works, ie will it accept an array.
But you could try:
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('data',$userData);
And on the view page:
<?php if(!empty($data['userData']['given_name'])){?>
<li>HI <?php echo $data['userData']['given_name'];?></li>
If that does not work this is what I would do.
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('userdata',$userData['id']);
$this->session->set_userdata('username',$userData['given_name']);
$_SESSION['data'] = $userData;
And on the view page:
<?php if(!empty($_SESSION['data']['userData']['given_name'])){?>
<li>HI <?php echo $_SESSION['data']['userData']['given_name'];?> </li>
<?php }
Also you are missing a right paren on:
$this->session->set_userdata('username',$userData['given_name'];
in your orginal code.
Yes, you can store data like this:
$this->session->set_userdata('userdata',$userData);
To access it from view, just store the session data into a local variable.
$userData = $_SESSION['userdata'];
then you can use it.
<?php if(!empty($userData['given_name'])){ ?>
<li>HI <?php echo $userData['given_name'];?> </li>
<?php } ?>
NOTE:
To use session in codeigniter you have to load the session library in your controllers.You can load it in constroctor or top of the method where you are going set session.(you don't have to load it to view.)
$this->load->library('session');
I am new to codeigniter. I have successfully stored the data to database from view. Now thing is i want to retrieve the data from database and show it in the view. Anyone can help from another link or atleast tell me the flow of passing the data. I m working on a form with text fields and buttons
To retrieve the rows in questions, you want to do something like this:
$data['rows'] = $this->db->get_where('table_name')->result();
And then pass this variable to your view:
$this->load->view('index', isset($data)?$data:'');
And then access it:
var_dump($rows);
Here is an example of one of mine
Controller
$data['title'] = "title";
$this->db->select("title, content, date")->from("posts")->where('parent', $parent)->where('status', 'publish')->order_by("id", "asc");
$query = $this->db->get();
if($query->result()){
$data["mainContent"] = $query->result();
}
$this->load->view("pages/header/head", $data); //this sends the mysql to the page
$this->load->view("pages/van");
$this->load->view("pages/footer/footer");
View (this is for an unordered list
<?php foreach($mainContent as $row):?> Note maincontent above as well.
<ul>
<li> <?php echo $row->title;?></li>
<li> <?php echo $row->content;?></ul>
</ul>
<?php endforeach;?>
You would do the same for tables, or posts or whatever. The foreach will echo data until it runs out
Let's say I have 3 columns/divs/sections in my html layout, let's call them cat1, cat2, cat3. Then I have a database with a table called "Content". In that table I have the fields id, title, content, category.
Now I want to populate those 3 columns with their corresponding content from my mysql table. For example - get all the content from table where category = cat1(corresponding to the first column).
I could just make 3 functions that each have their own parameter, but I don't want to repeat code. How can I do this with a single function, but just using a different parameter?
I was thinking something like this:
public function displayAll_with_category ($category) {
$sql = $con->prepare("SELECT * FROM Table WHERE Category=?");
$sql->bindParam(1, $category);
$sql->execute();
while ($row = $sql->fetch()) {
echo $row['Title'];
}
}
Then in my HTML:
<div id="cat1">
// Using a central class
<?php include('class.php'); $obj = new handler; $obj->displayAll_with_category(); ?>
</div>
So far so good, I need to somehow be able to alter the $category parameter so I can use the same function to call the contents for cat1/2/3. I have no idea how to do that, usually I need to send something to the PHP function via a button or form, but this time that's not the case. Is this even doable in pure PHP? I can think of doing this with AJAX, but for now I want to know if it can be done with PHP.
I'm not sure if this is what you want.
This would get you the data for category 1, 2 and 3.
I just created a loop in the view to call the function with different parameters:
<?php
for($cont = 0; $cont<4; $cont++){
?>
<div id="cat1">
// Using a central class
<?php include('class.php'); $obj = new handler; $obj->displayAll_with_category($cont); ?>
</div>
<?php
}
?>
Oh and if I were you i would try to separate the logic from the view and I will print the result of the function in the view instead of doing it inside it.
I have tried to pass a value from controller to layout using this but it does not work:
foreach ($user_info_details as $details):
$first_name = $details['first_name'];
endforeach;
Zend_Layout::getMvcInstance()->assign('first_name',$first_name);
and retrieve it using
<?php echo $this->layout()->first_name; ?>
but it shows blank in every case
To output values to your view easily, in your controller use:
$this->view->first_name = $first_name;
And in your view, access it like this:
echo $this->first_name;