Severity: Notice Message: Undefined variable: questions Filename: views/quiz.php - codeigniter

**view**
<?php foreach($questions as $row){ ?>
<?php $ans_array = array($row->choice1,$row->choice2,$row->choice3,$row->answer);
shuffle($ans_array);?>
<p><?=$row->quizID?>.<?=$row->question?></p>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[0]?>"> <?=$ans_array[0]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[1]?>"> <?=$ans_array[1]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[2]?>"> <?=$ans_array[2]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[3]?>"> <?=$ans_array[3]?><br>
<?php } ?>
</div>
controller
public function quest ()
{
$this->Model_students;
$this->data['questions']=$this->Model_students->quest();
$this->load->view('quest',$this->data);
}
public function quiz ()
{
$this->load->view('quiz');
}
model
public function quest()
{
$this->db->select("quizID,question,choice1,choice2,choice3,answer");
$this->db->from("quiz");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1)
{
echo "there is no data in the db";
exit();
}
}
after submitting this i get 2 errors;
1. Severity: Notice
Message: Undefined variable: questions
Filename: views/quiz.php
Line Number: 14
2.everity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/quiz.php
Line Number: 14

Remove $this
$data['questions']=$this->Model_students->quest();
$this->load->view('quest',$data);

Related

Error: page requested not found codeigniter

Controller
public function index()
{
//load session library
$this->load->library('session');
if($this->session->userdata('user')){
// redirect('home');
$this->load->view('heropage');
}
else{
$this->load->view('login_page');
}
}
public function login(){
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$grade=$data[0]->grade;
$points=$data[0]->points;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->session->set_userdata('grade',$grade);
$this->session->set_userdata('pts',$points);
$this->getImg();
redirect('home');
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found'); }
}
View
<?php if(isset($_SESSION['success'])) :?>
<div class="alert alert-success"><?=$_SESSION['success'];?></div>
<?php endif; if(isset($_SESSION['error'])) :?>
<div class="alert alert-warning"><?=$_SESSION['error'];?></div>
<?php endif;?>
<!-- End alerts -->
<form action="<?php echo base_url();?>index.php/User/login" method="post" accept-charset="utf-8">
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="email" placeholder="Email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"name="password" placeholder="Password">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<button class="btn btn-sm btn-success" type="submit" align="center" name="login" class="submit">Log in</button>
</div>
</div>
</form>
model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
Upon trying to log in, I got the error page cant be found. I want it to go to the home page if the session is correct. here is the error message:
404 Page Not Found
The page you requested was not found.
How can I solve the error because I have also set as needed in the routes
I think your form action should be <?php echo base_url(); ?>user/login
Also in your model you're not checking for password anywhere.
You're also not returning anything if the email is not found or more than 1 results are found -
($query->num_rows() == 1)
Model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password))->result(); // you should use row() here to return only 1 row.
return $query; // you should check the uniqueness of email on registration, not here -- not allow duplicate email on registration
}
Controller
public function login(){
$email = $_POST['email']; // $this->input->post('email');
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if( !empty($data) ) // if no result found it'll be empty
{
// your code
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
See, if this helps you.

Codeigniter Error Trying to get property of non-object

I am getting error of " Trying to get property of non-object" my controller code is
public function productDetails($pro_name,$product_id) {
$data['prodRating'] = $this->ProductsModel -> get_one($product_id);
$this->load->view('home',$data);
}
Model Code
function get_one($pro_id){
$query= $this->db->select('ratingid, pro_item_id, pro_total_points, pro_total_rates, proid ')
->from('tblproducts')
->where('proid',$pro_id)
->join('tblprorating','tblprorating.pro_item_id = tblproducts.proid','left')
->get();
return $query->result();
}
view code
<span dir="ltr" class="inline">
<input id="input-<?=$prodRating->proid ?>" name="rating"
<?php if ($prodRating->$pro_total_rates > 0 or $prodRating->pro_total_points > 0) { ?>
value="<?php echo $prodRating->pro_total_points / $prodRating->pro_total_rates ?>"
<?php } else { ?>
value="0"
<?php } ?>
<?php if ($this->session->userdata('userid') == false) { ?>
data-disabled="false"
<?php } else { ?>
data-disabled="<?= $rated ?>"
<?php } ?>
class="rating "
min="0" max="5" step="0.5" data-size="xs"
accept="" data-symbol="" data-glyphicon="false"
data-rating-class="rating-fa">
</span>
What is problem in my code if i use foreach loop then i can solve my problem. But in this code i not want to use the foreach loop. I want to access the fields of database. but i am getting error.
Return return $query->row() for single item. result() is for many. Further you should check num_rows() before attempting to access the result object/array and handle 0 rows properly as it is good practice.
In this case:
if ($query->num_rows() > 0) {
return $query->row();
}
return false;

submit multiple inputs within a forloop in codeigniter

My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}

Search is not working in codeigniter

i did search like this but it is not taking passed value in text box,can any one help me to solve this issue..thanks in advances........my model query is correct or any error is there......
if i enter in search term it is redirecting to search results page but it is showing blank results.......and it is saying invalid argument supplied for foreach()
if i print last_query() in that it is showing Like below.....
SELECT * FROM (`clients`) WHERE `clientName` LIKE '%%'
This is My Controller:
class Client extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("client_model");
$this->load->model("interview_model");
$this->load->model("candidate_model");
$this->load->model("requirement_model");
}
function search()
{
$search_term = $this->input->post('search');
$data['query'] = $this->client_model->search_client($search_term);
echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Search Results";
$this->layout->view("client/search",$data);
}
}
Model:
function search_client($search_term)
{
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
This is form in index.php(View):
<form action="<?php base_url();?>client/search">
<input type="text" name="search" id="search" style="width:180px" placeholder="Search here..." />
<input type="submit" value="Search" name="submit" class="btn btn-primary btn-sm" /></form>
This is search.php(view):
<?php foreach($query as $client){ ?>
<tr>
<td><?php echo $client->clientName;?></td>
<td><?php echo $client->clientSName; ?></td></td>
<td>
<?php } ?>
change this line:
<form method="POST" action="<?php base_url();?>client/search">
to this:
<form action="<?php echo base_url();?>client/search">
and try the following approch:
function search_client($search_term){
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
else
return false;
}

CodeIgniter, Message: Undefined variable, Passing data

>> My controllerv - verifyregistration
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class verifyregistration extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('users_model','',TRUE);
$this->load->library('email');
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('Login', 'Login', 'trim|required|xss_clean|callback_check_database|min_length[6]|strtolower|callback_username_not_exists');
$this->form_validation->set_rules('Haslo', 'Haslo', 'trim|required|xss_clean|min_length[6]');
$this->form_validation->set_rules('Powtorz_Haslo', 'Powtorz haslo', 'trim|required|xss_clean|matches[Haslo]|min_length[6]');
$this->form_validation->set_rules('Imie', 'Imie', 'trim|required|xss_clean|min_length[2]');
$this->form_validation->set_rules('Nazwisko', 'Nazwisko', 'trim|required|xss_clean');
$this->form_validation->set_rules('Email', 'Email', 'trim|required|xss_clean|valid_email|min_length[6]');
$this->form_validation->set_rules('Data_urodzenia', 'Data urodzenia', 'trim|required|xss_clean');
$this->form_validation->set_rules('Telefon', 'Telefon', 'trim|required|xss_clean|min_length[8]');
$this->form_validation->set_rules('Miasto', 'Miasto', 'trim|required|xss_clean');
$this->form_validation->set_rules('Ulica', 'Ulica', 'trim|required|xss_clean|min_length[6]');
$this->form_validation->set_rules('Kod_pocztowy', 'Kod pocztowy', 'trim|required|xss_clean');
$this->form_validation->set_error_delimiters('<span style="color: red; font-size: 12px; ; line-height: 14px; ">', '</span>');
$this->form_validation->set_error_delimiters('<li>', '</li>');
$data['heading'] = "My Real Heading";
if($this->form_validation->run() == FALSE)
{
$this->load->view('register/register_open',$data);
}
else
{
return false; //this is not important
}
}
>> My view open - register_open
<?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view',$data);
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?>
>>My view - register_view
<?php echo form_open('verifyregistration/index'); ?>
<form>
<label for="username">Login:</label>
<input type="text" size="25" id="Login" name="Login" set_value='Login' />
.
.
.
<legend>
<input type="checkbox" id="regulamin" name="Regulamin" onclick="this.form.elements['Wyslij'].disabled = !this.checked" />
Akceptuję regulamin serwisu. </legend>
<label> </label>
<input type="submit" name="Wyslij" value="Wyslij" disabled="disabled"/>
<label> </label>
<legend>
<<?php echo $heading;?>
</legend>
</form>
And I have errors:
Severity: Notice
Message: Undefined variable: data
Filename: register/register_open.php
Line Number: 9
this line-> $this->load->view('register/register_view',$data);
Severity: Notice
Message: Undefined variable: heading
Filename: register/register_view.php
Line Number: 48
this line-> <<?php echo $heading;?>
How I can pass the data to another view?
In your controller, change
$this->load->view('register/register_open',$data)
to
$this->load->view('register/register_open',array('data' => $data));
When you do $this->load->view('view', $variable) it takes $variable and uses the php function extract() to turn the array keys into variables. So, if you want to use the variable $data in a nested view, you have to send another array like I did above.

Resources