I have a database table in mysql that has 11 entities. When the user first searches for an item, my code displays 5 entities and then there is an link for the user to click to view more information on the item. When the user click the link it should lead them to a new page with information about the item that is clicked based on itemID. I don't know how to pass the itemID of the entity to the controller. Please help!
Here's my code:
Contoller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ItemView extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('itemModal');
}
public function index(){
$this->load->view('base');
$this->load->view('searchResult');
}
public function viewItems(){
//trying to get the id of the clicked item
$id = $this->input->post($rows['inventoryID']);
$data['results'] = $this->itemModal->get_items($id);
$this->load->view('base.php',$data);
$this->load->view('itemview.php',$data);
}
}
?>
Model:
<?php
class ItemModal extends CI_Model {
function __construct(){
parent::__construct();
}
function get_items($id){
$this->db->select('*');
$this->db->like('inventoryID',$id);
// Execute the query.
$query = $this->db->get('inventory');
// Return the results.
return $query->result_array();
}}?>
View:
<body>
<h1><center>Item List</center></h1>
<hr>
<div class="container">
<form method="post" action="<?php echo site_url('itemView/index'); ?>">
<table>
<tr>
<th><input type="radio" name="chk"></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th>Item Name</th>
<th>Color Name</th>
<th>Location</th>
<th>Link to more information</th>
</tr>
<?php foreach($results as $rows):?>
<tr>
<td><input type="radio" name="chk"></td>
<td><?php echo $rows['inventoryID'] ?></td>
<td><?php echo $rows['masterCode'] ?></td>
<td><?php echo $rows['itemName'] ?></td>
<td><?php echo $rows['colorName'] ?></td>
<td><?php echo $rows['location'] ?></td>
<td>click to view more information</td>
</tr>
<?php endforeach; ?>
</table>
</form>
</div></body>
View for itemView
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/search.css">
<title>Item Information Page</title>
</head>
<body>
<h1><center>Item Information</center></h1>
<hr>
<div class="container">
</div>
<!-- End of Container -->
</body><br><br>
</html>
yo need to change like this
controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ItemView extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper(array('url','form'));
$this->load->model('itemModal', 'model_items');
}
public function index(){
//get all items from db to display basic info in form
$data['items']=$this->model_items->get_all();
$this->load->view('base');
$this->load->view('searchResult', $data);
}
//get the specific item to be updated
public function updateItem( $id ){
$data['results'] = $this->model_items->get_item_by_id($id);
$this->load->view('base',$data);
//viw with inpus form to change the info and send to controller to be updated
$this->load->view('itemview',$data);
}
//get all items to be updated
public function updateItems(){
foreach($this->input->post() as $item => $value){
${$item} = $value;
}
//here you need to add logic to make wherever you want with all items selected in form
}
}
model itemModal
<?php
class ItemModal extends CI_Model {
function get_all(){
return $this->db->get('inventory')->result();
}
function get_item_by_id($id){
return $this->db->get_where('inventory', array('id' => $id))->row();
}
}
view searchResult
<form method="post" action="<?= site_url('itemView/updateItems'); ?>">
<table>
<tr>
<!-- select one or more items to be updated bach -->
<th><input type="checkbox" name="chk" value="<?= $rows->inventoryID ?>"></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th>Item Name</th>
<th>Color Name</th>
<th>Location</th>
<th>Link to more information</th>
</tr>
<?php foreach($itemsas $rows):?>
<tr>
<td><input type="radio" name="chk"></td>
<td><?= $rows->inventoryID ?></td>
<td><?= $rows['masterCode'] ?></td>
<td><?= $rows['itemName'] ?></td>
<td><?= $rows['colorName'] ?></td>
<td><?= $rows['location'] ?></td>
<!-- update specific item -->
<td>click to view more information</td>
</tr>
<?php endforeach; ?>
<tr>
<td>Update all items selected</td>
<td><input type="submit" value="Update"></td>
</tr>
</table>
</form>
Related
I wanted to be able to update and delete items for a list of products from an sql database. I already have the table from a database listed and I am able to read and insert a new product but I'm not sure how to update the product or delete it? Any advice or tutorials would be good? I have tried a few but none seem to work.
Here is my code for showing the products and inserting a new product
Product.php - controller
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('product_model');
}
public function index()
{
$data['product_list'] = $this->product_model->getproduct();
$this->load->view('header');
$this->load->view('nav');
$this->load->view('product', $data);
$this->load->view('footer');
}
public function add_form()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('insert');
$this->load->view('footer');
}
public function insert_product()
{
$pdata['Name'] = $this->input->post('Name');
$pdata['Type'] = $this->input->post('Type');
$pdata['Price'] = $this->input->post('Price');
$res = $this->product_model->insert_product($pdata);
if($res){
header('location:'.base_url()."index.php/Product/index");
}
}
}
?>
Product_model.php
<?php
class Product_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function getproduct()
{
$query = $this->db->get('testProduct');
return $query->result();
}
public function insert_product($data)
{
return $this->db->insert('testProduct', $data);
}
}
?>
product.php - View
<h2> Product</h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Price</th>
</tr>
<?php foreach ($product_list as $p_key){ ?>
<tr>
<td><?php echo $p_key->id; ?></td>
<td><?php echo $p_key->Name; ?></td>
<td><?php echo $p_key->Type; ?></td>
<td><?php echo $p_key->Price; ?></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?> >Edit</a></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?>>Delete </a></td>
</tr>
<?php }?>
<tr>
<td colspan="7" align="right"> Insert New Product</td>
</tr>
</table>
What you are making is called CRUD - create, read, update, delete. There are a lot of examples of doing CRUD with CodeIgniter in Google.
Below are (over)simplified conceptual samples.
Based on your HTML, adding links to edit and delete a product:
<td width="40" align="left">
Edit
<!-- Don't really delete like this, seciruty issue! Just shown as a concept for manipulating records with IDs -->
Delete
</td>
And in Product.php - controller we'll add new methods to edit and delete
public function edit($id)
{
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// get fields from form and update
// database record with them
$pdata['Name'] = $this->input->post('Name');
$pdata['Type'] = $this->input->post('Type');
$pdata['Price'] = $this->input->post('Price');
$this->product_model->update_product($id, $pdata);
}
else
{
// show form to edit product
// need to get product from database and pass to a view
$product = $this->product_model->getproduct_by_id($id);
$this->load->view('header');
$this->load->view('nav');
$this->load->view('edit', array("product" => $product));
$this->load->view('footer');
}
}
public function delete($id)
{
$this->product_model->delete($id);
}
edit.php - View
<form action="/index.php/product/edit/<?php echo $product->id ?>" method="post">
<table>
<tr>
<td><input name="name" value="<?php echo $product->Name; ?>"></td>
</tr>
<tr>
<td><input name="type" value="<?php echo $product->Type; ?>"></td>
</tr>
<tr>
<td><input name="price" value="<?php echo $product->Price; ?>"></td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
product_model.php
public function update_product($id, $pdata)
{
$this->db->where("product_id", $id);
$this->db->update("product", $pdata);
}
This is user class.
<?php
class User {
/*
*public static function get_all_users()
*/
public function get_all_users() {
global $link;
$result_set = $link->query("SELECT * FROM `user`");
return $result_set;
}
}
Above class is user class i made object but cannot access. Display all use page of html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>Admin Panel ::: </title>
</head>
<body>
<h3>Users :</h3>
<table border="1">
<tr>
<th>S.N</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th>E-mail</th>
<th>Contact</th>
<th colspan="2">Option</th>
</tr>
<?php
$user = new User();
$result_set = $user->get_all_users();
/*
*$result_set = User :: get_all_users(); - accessing user class using static keyword.
*/
$i = 1;
while ($row = mysqli_fetch_assoc($result_set)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['Contact']; ?></td>
<td>Update</td>
<td>Delete</td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
I can not access user class when I create object of user class and user class is inside includes folder and below HTML file is inside admin folder or admin ->viewuser.php and admin->includes->user.php it gives error.
You need to include that User class inside the viewuser.php script !
include_once("includes/user.php");
I am not sure organizing your folders like that is the best way to do but it should work.
I am trying to create a index method for the content model controller and I am getting the following error, I searched here but did not find the relevant answer. Please take a look at the following code and screenshot:
Content.php:
class Content extends AppModel{
public $name = 'Content';
}
ContentsController.php:
class ContentsController extends AppController {
public $name = 'Contents';
public function index() {
$this->set('Contents', $this->Content->find('all'));
}
}
index.ctp:
<h1>View All Content</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $this->$content['Content']['id']; ?></td>
<td><?php echo $this->$content['Content']['title']; ?></td>
<td><?php echo $this->$content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Screenshot:
http://postimg.org/image/sslcgoutx/
Please help as I am new to cakephp and learning it.
Try this in index.ctp
<h1>View All Content</h1> <table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $content['Content']['id']; ?></td>
<td><?php echo $content['Content']['title']; ?></td>
<td><?php echo $content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Here's the issue : CodeIgniter loads data from the controller but not from the view
When I go the the source of the page and click the form action link its load data in the source code but not in the html view.
--view
<table class="table hovered">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Type</th>
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin/manage_customer') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->cus_id; ?></td>
<td class="right">Admin</td>
<td class="right"><?php echo $row->cus_name; ?></td>
<td class="right"><?php echo $row->cus_email; ?></td>
<td class="right"><?php echo $row->cus_phone; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
---controller
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 //Users (model name) share all the class and functions CodeIgniter models have
{
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()
{
}
}
i would go with hussain in this. first check if the necessary data is returned by the model or not. Only then you should check the html structure. But even if there is a problem with structure, something should be displayed. So, check the flow of data to the view first.
I have this code block in html for inputting the search.
<?php echo form_open('bookstore/_searchbook'); ?>
<table>
<tr>
<td>
<label for="searchid">Search:</label>
</td>
<td>
<input type="text" size="15" name="searchid" />
</td>
</tr>
<tr>
<td>
<label for="searchtype">Type:</label>
</td>
<td>
<select>
<option value="book_id">Id</option>
<option value="book_author">Author</option>
<option value="book_name">Title</option>
</select>
</td>
</tr>
<tr>
<input type="submit" value="Search" />
</tr>
</table>
<?php echo form_close(); ?>
And i have this on my controller,
public function booksearch()
{
if($_POST['submit'])
{
$col= $this->input->post('searchtype', TRUE);
$val= $this->input->post('searchval', TRUE);
$data['book'] = $this->books_model->showbook($col,$val);
$this->load->view('showbooks',$data);
}
}
and this would be my model
public function showbook($col, $searchid)
{
$this->db->select()->from('books')->where(array($col=>$searchid));
$query=$this->db->get();
return $query->first_row('array');
}
Further info on my view,
I have this to print the results of my search.
<table cellpadding='5'>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>
<?php
if(isset($books)) : foreach($books as $book) :
?>
<tr>
<td><?php echo $book['book_id'] ?></td>
<td><?php echo $book['book_name'] ?></td>
<td><?php echo $book['book_author'] ?></td>
<td><?php echo $book['book_year'] ?></td>
<td><?php echo $book['book_isbn'] ?></td>
</tr>
<?php
endforeach;
?>
<?php
else :
?>
<h5>No records</h5>
<?php
endif;
?>
</table>
It returns nothing so all i see is no records. Someone direct me to the thing i did wrong.
Your form's action (view) is _searchbook while your controller function is booksearch
Your submit input needs a name attribute
in the controller function you have $data['book'] while in your view (in the foreach loop) you reference that variable as $books
Model: you need to select at least something; e.g. $this->db->select('*')->from('books')->where(array($col=>$searchid)); instead of $this->db->select()
Model: I think you need return $query->result_array(); instead of $query->first_row('array')
Because your submit button has not got any name attribute. Instead of if($_POST['submit']) write $this->input->post(null)
Try adding global $_POST; to the top of your function. In php all variables used(declaired) in a function will be private unless you tell it otherwise by adding it to the global statement in the function.
public function booksearch()
{
global $_POST;
if($_POST['submit'])
{
$col= $this->input->post('searchtype', TRUE);
$val= $this->input->post('searchval', TRUE);
return $data['book'] = $this->books_model->showbook($col,$val);
}
}