I'm trying to send from a controller to a view an array of all contacts who are in my database table "Contacts" and then display them on a drop down menu.I followed the CodeIgniter documentation about this topic http://codeigniter.com/user_guide/general/views.html but it isn't really what I would like to do.
Here is what I tried to do :
function getAll_contact(){
$exist= $this->contacts_model->get_all('contacts');
if($exist)
{
$all_contact = $this->contacts_model->read('contacts');
//echo json_encode($all_contact); prints all the contacts in the table
$this->load->view('myView', $contact);
}
}
In my view :
<select class="span4">
<?php if(isset($all_contact) && ! empty($all_contact)){
foreach($all_contact as $contact){
echo "<option value='".$contact->id_contact."'>".$contact->company."</option>";
}
}
</select>
This does not show anything on the drop down menu. Could anyone help me please ?
put ur result into data array..
$data['all_contact']=$this->contacts_model->read('contacts');
and send the array to view
$this->load->view('myView', $data);
and you can take that variable in your view with $all_contact ..like u currently have..
First of all, you've named the variable $contact instead of $all_contact.
The solution:
function getAll_contact(){
$exist= $this->contacts_model->get_all('contacts');
if($exist)
{
$data['all_contact'] = $this->contacts_model->read('contacts');
//echo json_encode($data); prints all the contacts in the table
$this->load->view('myView', $data);
}
}
Then you access it like you are currently in your view.
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 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
Following this Question, im also creating an quiz approach in Codeigniter. here is the snippet of my code.
Controller :
function quiz(){
$this->load->model('belajar_model');
$data['data']=$this->belajar_model->quiz();
$this->load->view('quiz',$data);
}
function getQuiz($id){
$this->load->model('belajar_model');
$data['data']=$this->belajar_model->getQuiz($id);
$this->load->view('quiz',$data);
$this->load->model('belajar_model');
$data['data']=$this->belajar_model->quiz();
if($this->input->post()){
$jawab = $this->belajar_model->getQuiz($id);
$soal = $this->belajar_model->getQuiz($id);
if($jawab['benar']==$this->input->post('jawab')){
}
$data['jawab'] = $soal;
$data['next'] = $id+1;
$this->load->view('quiz', $data);
}
else{
$jawab = $this->belajar_model->getQuiz(1);
$data['soal'] = $soal;
$data['next'] = 2;
$this->load->view('quiz', $data);
}
}
and here is the model :
function quiz(){
//$this->db->where('id = 1');
$query = $this->db-> get('kuis');
return $query->result_array();
}
function getQuiz($data){
$this->db->where('id',$data);
$query = $this->db-> get('kuis');
return $query->result_array();
}
and here is the snippet of the view (my friend did the view) :
<h3>Pilihlah salah satu jawaban yang benar!</h3>
<?php foreach ($data as $list){
echo"<p>".$list['soal']."</p>";
echo"<table>
<tr><td><label><input type='radio' name='jawab' value='a'>".$list['ansA']."</label></td></tr>
<tr><td><label><input type='radio' name='jawab' value='b'>".$list['ansB']."</label></td></tr>
<tr><td><label><input type='radio' name='jawab' value='c'>".$list['ansC']."</label></td></tr>
<tr><td><label><input type='radio' name='jawab' value='d'>".$list['ansD']."</label></td></tr>
";
}?>
<tr><td><button type='submit' >Submit</button><td></tr><table>
after trying to implement it, with some modification make it work on the page, the page is jumbled like this, it's jumbled because the question and the answer is not displayed correctly. it also didn't receive the inputs from the quiz and there are some errors displayed.
my question is : where did it go wrong? is it from the view or either the models/controller not right? FYI, some of the attributes are written on my native language(Indonesian) to fulfill my studies, to make the code understandable to both my friend and my lecturer.
thanks for answering! it'll means a lot for me to get the webpage work because it's my final project and i didn't adapt enough (yet) in php and codeigniter.
Before going further, you should first clean your code to make sure you don't have any unforseable side effects:
In your getQuiz() method in your controller, you load your model two times and you also render the view twice. Once at the beginning and once after you checked the presence of $this->input->post().
PS for the mods: I know this is more comment material, but I can't comment yet.
I've built a contest system where users submit tickets then one is randomly chosen to win, and now I'm trying to figure out a way to display to users the tickets they have already submitted. Each ticket has an id, a date, and an invoicenumber. I want to display all the invoice numbers that a user has submitted so far.
Here is the method I have in my methods page. (I've organized my methods into one php file and then i just call them when needed.)
function GetSubmittedBallots()
{
if(!$this->CheckLogin())
{
$this->HandleError("Not logged in!");
return false;
}
$user_rec = array();
if(!$this->GetUserFromEmail($this->UserEmail(),$user_rec))
{
return false;
}
$qry = "SELECT invoicenumber FROM entries WHERE user_id = '".$user_rec['id_user']."'";
$result = mysql_query($qry,$this->connection);
while($row = mysql_fetch_array($result))
{
echo $row['invoicenumber'];
}
}
and then on my html page that I want it to echo on, i just call it
<?php GetSubmittedBallots(); ?>
Sadly, this doesn't work. So my question is, how would i go about displaying the $row array on my html page?
<?php
require("methods.php"); // Include the file which has the "GetSubmittedBallots" function or method, if it's in a separate file
GetSubmittedBallots(); // Run the function / method
?>
If this doesn't work, please let us know any errors you receive.
Does it echo "Array"?
That's because you are trying to echo an array.
You should use something like print_r or var_dump, given that you are just trying to access the queried results. In my opinion the method should build a multidimensional array with the records, and then the template logic should loop through them and echo the values in a nice way. Be it a table or nicely arranged HTML.
If I'm not wrong, $this keyword is indicating you're in a class? If so, you need first to init that class and try to call GetSubmittedBallots function after init;
// assuming that class's name is Users
$users = new Users();
$users->GetSubmittedBallots();
Form on Page:
<div>
<?php echo form_open('add/player');
echo form_label('First Name','playerFirstName');
echo form_input('playerFirstName','test');
echo "<br />";
echo form_label('Last Name','playerLastName');
echo form_input('playerLastName','test');
echo "<br />";
echo form_submit('addPlayer','Add Player');
echo form_close(); ?>
</div>
Called Controller:
class Add extends CI_Controller
{
public function index()
{
$this->load->view('header');
$this->load->view('add');
$this->load->view('footer');
}
public function Player()
{
if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
if(basename($_SERVER['HTTP_REFERER']) == basename($_SERVER['PHP_SELF']))
{
$this->load->model('players');
$returnMessage = $this->players->add_player($this->input->post('playerFirstName'), $this->input-post('playerLastName'));
}
}
$this->load->view('header');
$this->load->view('addplayer');
$this->load->view('footer');
}
}
I'm a very green novice when it comes to PHP, CodeIgniter and MySQL (or web coding in general), so if you see any suggestions, please don't hesitate to make them (though the credit will be given to the one who answers my question, not makes my code work the best). However, as to my question, is there a way to see if a string is within another string? My $returnMessage, if successful will have the word successfully in it. What I want to do is if the player is not added, I want to reload the values from the form and put them back. Here's where it gets tricky.
The names are saved in table people_names. In the players table, I save the nameid that is associated with that name in the people_names table. In my add_player function, I check to see if the names are in the table, if they aren't I add them, then I add the player to the players table with the appropriate ids. Because of this, I'm not sure form_validation would be able to be used or not. If it were, please let me know.
So what I want to do is check the $returnMessage and if it doesn't contain successfully, then I want to add the values back to the fields. However, I'm not finding a function where I can see if a string is inside of another string. I see string compares and substring functions, but no string contains. Any ideas? Thanks.
Use native PHP's strpos: http://php.net/manual/en/function.strpos.php
Cheers
http://php.net/manual/en/function.strpos.php
if (strpos($returnMessage,'successfully') !== false) {
echo 'true';
}