PHP Function to select and display all the records stored under the column "clubName", from the table 'clubs'. The foreach statement ($club) returns an undefined variable error. What have I missed?
Controller:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Club_Controller extends Template_Controller {
public $template = 'kohana/template';
public function index()
{
$this->template->title = 'All clubs';
$this->template->content = new View('allclubs');
$clubs = ORM::factory('club')->find_all();
$this->template->content->club = $clubs;
}
}
?>
View:
<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
<div class="box">
<b><?php echo html::anchor('entry/form', 'add entry') ?></b><br>
<table cellpadding="10">
<?php foreach($clubs as $club): ?>
<tr>
<td align="left">
<?php echo html::anchor('entry/form/'.$club->id, 'edit') ?>
<?php echo html::anchor('entry/delete/'.$club->id, 'delete',
array('onclick'=>'return confirm("Are you realy realy want to do it?")')) ?>
</td>
<td align="left">
<?php echo $club->clubName ?>
</td>
</tr>
<?php endforeach ?>
</table>
</div>
Change this
$this->template->content->club = $clubs;
to this
$this->template->content->clubs = $clubs;
Related
I know how to show all data from db using foreach but do not know how to show only 1 record that I select.
My code:
public function go($id)//controller
{
$data["log"] = $this->product_model->getAll();
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>
<?php endforeach; ?>
Please help me to show one record using foreach.
All you have to do is remove the foreach.
Or, change row() to result().
You should remove foreach and do this
return $this->db->get_where($this->_table, ["ID" => $id])->row_Array();
than you can use this
<tr>
<td>
<?php echo $product['id'] ?>
</td>
<td>
<?php echo $product->['tg_mulai'] ?>
</td>
<td>
<?php echo $product->['keterangan'] ?>
</td>
</tr>
you can use foreach() for only multiple rows
foreach operate on arrays and to show single record you should send it to view as array of objects. To do this just replace $data["log"] with $data["log"][] when you get single record from db. Check below code
public function go($id)//controller
{
$data["log"][] = $this->product_model->getById($id);
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>
First view, where you click to see selected data
<?php foreach ($log as $product): ?>
<a href="<?= base_url('controller/function/').$product->id; ?>"
class="btn btn-info">Edit</a>
<?php endforeach; ?>
Then Controller will receive the id like
public function edit_product($id){
$data['datas'] = $this->your_model->model_function($id);
$this->load->view('your_view', $data);
Model
public function model_function($id){
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('table_name');
$query = $this->db->get();
return $query->result();
}
Single Data View
<?php foreach($datas as $data): ?>
<?php echo $data->id; ?>
<?php echo $data->name; ?>
<?php endforeach; ?>
Let me know if it's ok
I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach ($userdetail_list as $row) {
?>
<tr>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach ($userdetail_list as $row) { ?>
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load this view file I do see the echoeod email but not the username? And there is also no link to the users profile page..
Here is my controller file (User.php):
<?php
class User extends CI_Controller {
public function index() {
$this->load->view('profile', $data);
}
public function __construct() {
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id) {
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user', $data);
}
public function profile() {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
?>
And here is my model (User_model.php) :
<?php
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function getUserInfo($user_id) {
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if ($this->db->affected_rows() > 0) {
$row = $q->row();
return $row;
} else {
error_log('no user found getUserInfo(' . $user_id . ')');
return false;
}
}
?>
Database information:
I have 2 tables:
users:
user_id
voornaam
achternaam
email
password
(products:
product_id
product_name
product_description)
I hope someone can help me,
Thanks!
You are concatenating user name in anchor tag herf separate it and write between <a> and </a>
change your code as below:
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
You can try this solution for your problem.
Please change your view file.
details.php
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
and please changes your controller function:
User.php
<?php
public function profiel_user($user_id) {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$data['user_id'] = $user_id; // you can used user id for getting data
$this->load->view('profile');
}
?>
Thanks
I can't load mysql data to the view by selecting please help me to find out the error. thank you.
View
<div class="grid">
<div class="row">
<?php $this->load->view('admin/admin_topmenu.php'); ?>
</div>
<div class="row">
<div class="span10 box" style="padding: 10px;">
<p>Recent Messages</p>
<table class="table hovered">
<thead>
<tr class="selected">
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
<th class="text-left">Message</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin_forms/inbox') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->contactus_name; ?></td>
<td class="right"><?php echo $row->contactus_email; ?></td>
<td class="right"><?php echo $row->contactus_phone; ?></td>
<td class="right tertiary-text-secondary text-justify"><?php echo $row->contactus_comment; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
</div>
<div class="span3">
<nav class="sidebar light">
<ul>
<li>
<a href="#">
<i class="icon-home"></i>
Inbox <strong>(5)</strong>
</a>
</li>
<li>
<a href="#">
<i class="icon-home"></i>
Send
</a>
</li>
<li>
<a href="#">
<i class="icon-home"></i>
Newsletter/Ad
</a>
</li>
</ul>
</nav>
</div>
</div>
controller
<?php class Admin extends CI_Controller {
function index()
{
}
function inbox()
{
$data = array();
if($query = $this->mod_contactus->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_messages',$data);
}
}
model
<?php
class Mod_contactus extends CI_Model
{
function get_records()
{
$query = $this->db->get('tbl_contactus');
return $query->result();
}
function add_record($data)
{
$this->db->insert('tbl_contactus', $data);
return;
}
function update_record()
{
}
}
When I go to the Source Code of the page click the form post method it gives me 404 Page Not Found error.
I'm assuming you are able to get to the page and you are getting the error upon form submission. Unless you have special routes setup in your application/config/routes.php file I believe by changing this line in your view
<?php echo form_open('admin_forms/inbox') ?><!-- start of the form -->
to
<?php echo form_open('admin/inbox') ?><!-- start of the form -->
would do the trick.
I say this because your controller's name is Admin and in order to call it (by default) you would have to use admin in the URL and not admin_forms.
You need to fetch the results,
So the line:
Actually, your variable $records is a resource, not a result set.
<?php if(isset($records)) : foreach($records as $row) : ?>
So, please use this resource and then fetch the records.
It should become:
<?php if(isset($records)) : foreach($records->result as $row) : ?>
<?php if(isset($records)) : foreach($records->result() as $row) : ?>
I have a weird problem with Codeigniter and Datamapper,
I am making an CMS system and I am trying to edit an article from the Database using Datamapper. When I click to edit the article I get the message 'You have succesfully saved the article ' without clicking on the SAVE button, also all the data becomes empty and leaves 0. When I enter the data and click on SAVE it saves it but again when I try edit it again it goes back to 0 ... Can someone help out please
Here is my edit function in the controller
public function edit($id = NULL)
{
// Get articles by ID
$articles = new Article_model();
$article = $articles->where('id', $id)->get();
if ($id)
{
$id == NULL || $article;
count($article) || $error = 'Page not found ';
}
else
{
$article = $this->article_model->get_new();
}
$article->title = $this->input->post('title');
$article->text = $this->input->post('text');
if ($article->save())
{
echo 'You have succesfully saved the article';
}
else
{
echo 'Sorry something went terribly worng';
}
$data = array(
'admin_content' => 'admin/article/edit',
'article' => $article,
);
$this->parser->parse('admin/template_admin', $data);
}
and here is my view
<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
<div class="col-md-9">
<h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Publication Date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
that's not weird at all...every time that controller is loaded it's going to execute the save() method of the article class.
you should probably include some form validation and only save the article if the form was submitted and validation passes.
In a table I've 3 columns nota1, nota2, nota3 of table Notas.
I want to calc the average of nota1, nota2, nota3
Then I want to save using beforeSave() method the average result on nota_final field
This is my code in the model Notas.php:
public function getAvg()
{
$avg = Yii::app()->db->createCommand()
->select('(nota1 + nota2 + nota3)/3 as NotaFinal')
->from('notas')
->queryRow();
return $avg;
}
protected function afterSave()
{
if($this->isNewRecord){
$this->nota_final->Avg();
$this->isNewRecord= false;
$this->nota_final->saveAttributes($_POST['nota_final']);
if($model->save() == false) var_dump($model->errors);
}
return parent::afterSave();
}
It's not saving the avg value and it's not display any error.
What's wrong?
my View/notas/_form.php
tr>
<td><div class="row2">
<?php echo $form->labelEx($model,'nota1'); ?>
<?php echo $form->textField($model, 'nota1', array('maxlength'=>'3')); ?>
<?php echo $form->error($model,'nota1'); ?>
</div></td>
<td>
<div class="row2">
<?php echo $form->labelEx($model,'nota2'); ?>
<?php echo $form->textField($model,'nota2', array('maxlength'=>'3', 'input'=>'20px')); ?>
<?php echo $form->error($model,'nota2'); ?>
</div>
</td>
<td>
<div class="row2">
<?php echo $form->labelEx($model,'nota3'); ?>
<?php echo $form->textField($model,'nota3', array('maxlength'=>'3')); ?>
<?php echo $form->error($model,'nota3'); ?>
</div>
<td>
<div class="row2">
<?php echo $form->labelEx($model,'nota_final'); ?>
<?php echo $form->textField($model, 'nota_final', array('readOnly'=>true)); ?>
<?php echo $form->error($model,'nota_final'); ?>
</div>
public function getAvg() // remove this method
{
}
protected function beforeSave() // change to beforeSave()
{
if($this->isNewRecord) {
// Copy attributes from $_POST
$this->nota_final->saveAttributes($_POST['nota_final']);
// Calculate average
$this->nota_final->avg =
($this->nota_final->nota1 + $this->nota_final->nota2 +
$this->nota_final->nota3) / 3;
// DO NOT NEED TO CALL ->save() here, it will result in loop.
// Do it in your controller, and this beforeSave method will be triggered
}
return parent::beforeSave(); // change to beforeSave
}