Hi guys im having a difficulty with this scenario:
I want to get the product information using modals, i got this following code:
//Model.php
public function getProduct($product_id){
$this->db->select('product_id,product_name,product_price,product_qty');
$this->db->from('tbl_products');
$this->db->where('product_id',$product_id);
$query = $this->db->get();
return $query->result();
}
//Controller.php
public function view_product(){
$product_id = $this->input->post('product_id');
$this->load->view('header');
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
//update_product.php (modal) my View
lets just go straight into the form
<form action="" method="post">
<?php foreach($posts as $post){ ?>
<input type = "hidden" name = "product_id" value = "<?php echo $post->product_id;?>"/>
<input type = "text" name = "product_name" value = "<?php echo $post->product_name;?>"/>
<input type = "text" name = "product_price" value = "<?php echo $post->product_price;?>"/>
<input type = "text" name = "product_qty" value = "<?php echo $post->product_qty;?>"/>
<button type="submit">Update</button>
<?php } ?>
</form>
I got a table already: i can see all products, in my product_page.php
Here is the tables face looks like:
ID Name Price Quantity Option
1 Shoes 150.00 1 Update
2 Liquor 67.50 5 Update
3 Paint 1000.00 5 Update
Once I click the Update button, the update_product.php(a modal) will pop up and get the result of 1 of the product, if I press the first Update only the information for Shoes will be inside the modal, at first i tried it, I get all the information of all the products which makes my modal redundant and looping due to foreach, then I tried getting the information from the table ID itself, and no product pops out, how can I see only 1 product using modal? thank you very much maam and sirs. Please I really need youre help :(
Although I'm not really clear what you're asking, Here is an answer to you question. Your controller code doesn't seem to be making any sense.If you want to display all your products in on page and then you want to edit/update a product when clicked on corresponding update link, here is what you can do.
Use single controller method for list and update
//Controller.php
public function view_product(){
$product_id = (isset($this->input->post('product_id')) ? $this->input->post('product_id'): False ;
if($product_id == False){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
} else{
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
}
This is simple modification to your code to make it work correctly, but since this code cannot handle form submission of the update form (unless you're pointing update form to a different controller), you will have to add some other code to this controller and your code will get messy in no time. My personal suggestion to you is this,
Use different controllers for list view and update
//Controller.php
public function view_product(){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
}
public function update_product($product_id){
if($this->input>post('submit')){ // 'submit'should be replaced with the name attribute of your submit button
//call your model and handle the update form submission here
}
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('product_page',$this->data);
}
now, update option of your product_page.php should point to 'update_product' controller method with corresponding product id e.g. {base_url}/controller_class_name/update_product/1
I think your product_page.php has some code like this to loop through all the products and display products in a table, now when you click update link it will point to update_product controller and it will handle the update process.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php if(!$products){ ?>
<tr>
<td colspan="5">No result</td>
</tr>
<?php } ?>
<?php if($products){ ?>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->name; ?></td>
<td><?php echo $product->price; ?></td>
<td><?php echo $product->quantity; ?></td>
<td>
<a href="<?php echo base_url(); ?>controller_class_name/update_product/<?php echo $product->id; ?>" >Update</a>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
You won't be needing a foreach loop inside your update_product.php because you're updating only one product at once.
Hope this is the answer you're looking for, if not please comment and I will edit the answer accordingly.
Related
I am making a table from a database and want to add an Edit/delete button to each row update or delete a specific row from the database. I have successfully added working "delete" button but I have no idea how could I update data in table <td> in view and send it to controller.
Here is my code:
view file
<table class="table table-striped">
<tr>
<td>name</td>
<td>age</td>
<td>gender</td>
<td>class</td>
<td>roll no.</td>
</tr>
<?php foreach($record as $r): ?>
<tr>
<td><?php echo $r->name; ?></td>
<td><?php echo $r->age; ?></td>
<td><?php echo $r->gender; ?></td>
<td><?php echo $r->class; ?></td>
<td><?php echo $r->roll no; ?></td>
<td><a href="" >Edit</a>
<a href="<?php echo base_url()."student/deleteRow" ?id="$r->name">"
onclick="return confirm
('Are you sure to Delete?')"><i class="icon-trash"></a></td>
</tr>
<?php endforeach; ?>
</table>
Controller Function
public function deleteRow(){
if(isset($_GET['id'])){
$id=$this->input->get('id');
$this->student_model->rowDelete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
I don't know how can I now insert an input field to update table row without effecting previous view. Any suggestion would be helpful.
To Edit the Studen data you need to pass an id or uniue column name to the data base to get that student data.
First Set the student id in <a href=""> tag.
<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a>
Then When you click on the edit it will take you to the controller. You can get the third url parameter direct in as show in the controler code:
You can also use get as shon
Your Controller should be:
public function edit_student($id){
$student_data = $this->student_model->get_student_data($id);
$this->load->view('your_view',['student_data'=>$student_data)]);
}
Here is you model which get the id form controllr and find the student data and passit to back to the controller:
Your Model should be:
public function get_student_data($id){
$this->db->select('*');
$this->db->from('your_table_name');
$this->db->where('id',$id);
$query = $this->db->get();
$student_data = $query->$row_array();
if(isset($student_data) && !empty($student_data)){
return student_data;
} else {
return FALSE;
}
}
Form controller you pass the data to the view.
On View Side:
<?php
// Just to ensure the data. Comment it after testing
echo "<pre>";
print_r($student_data);
echo "</pre>";
?>
<form action="<?= base_url('student/update_student') ?>/<?= $student_data['id'] ?>">
<input name="your_column_name" value="<?= $student_data['your_column_name'] ?>">
// Try to do the same for all you column.
<input type="submit" value="updata">
</form>
Here is the controller for update the data
public function update_student($id){
$student_data = $this->input->post();
$status = $this->student_model->update_student($id,$student_data);
if($status == TRUE){
$this->load->view('your_view');
// Your Success view
} else {
// Your view if fail to update
$this->load->view('your_view');
}
}
Here is the model for update the data
public function get_student_data($id,$student_data){
$this->db->where('id',$id);
$this->db->update('your_table_name',$student_data);
if($this->db->affected_rows() == 1){
return TRUE;
} else {
return FALSE;
}
}
Very similar to what you have done for delete. Something like this:
<td>
<a href="" >Edit/Delete</a>
<!-- This should be another method in Student controller -->
<i class="icon-trash"><!-- I changed order of edit and delete -->
</td>
I need to warn you for CSRF. If you don't implement better security here, anyone pointing to that link would be able to edit or delete data.
Check Security class in documentation and how to set hidden value so that way you would ensure that only one who has already requested that page was able to edit/delete rows.
<td><a href="<?php echo base_url();?>controller_name/function_name/<?php echo $edit_id;?>" >Edit</a></td>
another way
<td><a href="<?php echo base_url();?>controller_name/function_name?id=<?php echo $edit_id;?>" >Edit</a></td>
You can use any one according to your requirement.
I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}
I have form where the select boxes are there and select all option, i want to post all the data without click on check boxes, i dont want to view this page, mean if user go to signup page the data will already selected by default how can i do that wothout any view?
this is my view code
<?php v_start($this);?>
<?php
#if(element exits of controller-action)
echo $this->element('validations/users/signup');
?>
<script>
function toggleChecked(status)
{
jQuery(".new-checkbox input").each( function() {
jQuery(this).attr("checked",status);
}
)
}
</script>
<h1><span style="color:CornflowerBlue"><?php echo __l('Step1');?></span></h1>
<?php
$form = $this->FormManager;
echo $form->create('User',array_merge($form_options,array('default'=>true)));
?>
<table width = "100%">
<tbody>
<tr>
<td>
<?php echo $form->input('',array('id'=>'mark-all','onclick'=>'toggleChecked(this.checked)','type'=>'checkbox','label'=>'Select All Products' ));?>
<?php echo $form->input('product_id',
array(
'multiple'=>'checkbox',
'options'=>$products,
'div'=>false,
'class'=>'new-checkbox'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo $this->FormManager->end(LBL_BTN_NEXT);
?>
</td>
</tr>
</tbody>
</table>
i dont want this page, the user will select the data by default.
this is my controller code
$products = $this
->User
->ProductsUser
->Product
->find('list',array(
'fields' => 'product_id,name',
'conditions'=>array(
'Product.is_visible'=>true
)
)
);
$this->set('products',$products);
if($this->request->is('post'))
{
$this->Session->write('signUp.signUpProduct',$this->request->data['User']);
$this->redirect(array('action'=>'signup_product'));
}
}
how can i do that, Thanks in advance.
The attr() doesn't take true or false as an argument.
jQuery(this).attr("checked","checked"); // Correct usage of attr()
Although I think attr is deprecated for the favorable prop() method.
jQuery(this).prop("checked",status);
Which indeed does take a true/fales argument. :)
Hope this helps.
EDIT: So a guy in this thread says the following:
"All order data in Magento is product-specific. It doesn't store anything about what category the products are in. So, as you have found out, there are no reports available in Magento for that sort of thing."
Is this true? Is what I am trying to do a lost cause?
I am trying to display product categories on Magento's backend Sales > Order > specific order view > Information > Items Ordered grid. This is not the main "Orders grid" that you see when navigating to Sales > Order. I want to alter the more detailed one that you see after clicking on a specific order in that first grid.
Following advice from this thread, I've made a new column and given it the proper title (Categories).
Then, to try to populate that column, I added code from Joost de Valk's solution on this thread to app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
I feel like I should be close (or not) but currently that code is just returning the word "Array". Any thoughts?
My current code is below. The column in question is the second td - the one that echoes $cats:
<?php $_item = $this->getItem() ?>
// Here is the snippet I pasted in to define variables.
<?php $product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
<?php $this->setPriceDataObject($_item) ?>
<tr<?php if (!$this->canDisplayGiftmessage()): ?> class="border"<?php endif; ?>>
<td>
<?php if ($this->canDisplayContainer()): ?>
<div id="<?php echo $this->getHtmlId() ?>" class="item-container">
<?php endif; ?>
<div class="item-text">
<?php echo $this->getColumnHtml($_item, 'name') ?>
</div>
<?php if ($this->canDisplayContainer()): ?>
</div>
<?php endif ?>
</td>
// Here is the column in question.
<td class="a-center"><?php echo $cats ?></td>
<td class="a-center"><?php echo $_item->getStatus() ?></td>
<td class="a-right"><?php echo $this->displayPriceAttribute('original_price') ?></td>
etc, etc, etc ...
$_cat is an object
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
<td class="a-center"><?php echo $cats ?></td>
You need to know which property you want to display
echo $_cat->getXyz();
eg.
echo $_cat->getName();
To see a list of some of it properties try
print_r($_cat->getData());
So it seems that categories are really complicated and certainly beyond my abilities. I figured out a work-around, however ugly. Since I am not using the manufacturer field and have only 4 relevant categories, I am creating "manufacturers" for each of those 4 categories, going through and assigning them to all products, and then calling for manufacturer in the relevant file. The code for manufacturer is as follows:
<?php
$manufacturer = Mage::getModel('catalog/product')->load($_item['product_id'])->getAttributeText('manufacturer');
echo $manufacturer;
?>
Thanks to this post for that snippet and thanks to everyone else for their help.
I am creating a diabetes management system for a university project. One of the features of this system is for the patient to be able to send latest glucose readings and for the nurse to be able to login and comment on those readings.
I have been able to code the patient feature, however I wish to add a comment button in the comments column, which when clicked, brings up a popup window or a textbox for the nurse to be able to comment on that specific record. If a comment has not already been entered, an empty box should come up, however if there has been a previously entered comment, it should be displayed in the box to be updated and sent back to the mysql database. I would like to ask if someone could give me a way to include this comment box and code for the existing value to be displayed in the box and if there is no existing comment, then a new comment can be entered and stored in the database.
Below is my php code.
<?php//run query
$result = mysql_query($GetReadings);
?>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Glucose Level</th>
<th>SBP</th>
<th>DBP</th>
<th>Comments</th>
</tr>
<?php
//display results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td><?php echo $row["Date"]; ?> </td>
<td><?php echo $row["Time"]; ?> </td>
<td><?php echo $row["GlucoseLevel"]; ?> </td>
<td><?php echo $row["SBP"]; ?> </td>
<td><?php echo $row["DBP"]; ?> </td>
<td><?php echo $row["Comments"];
<?php
//if statement to add comment link if user is a nurse
if ($_SESSION['User_level'] == 2)
{
//code for comments
}
?> </td>
</tr>
<?php
//end of while loop
}
?>
Hope I haven't missed out any crucial information.
Use the javascript function :
window.open(URL, windowName[, windowFeatures])
Where, URL - desired URL to display a page containing Textbox, use any window name you want.
Echo <a> or button with a onclick event eg:
Add Comment
Edit
The most basic way to achieve is, echo a <div></div> containing the past comments, the text box for new comments and Send/Cancel buttons. The trick is to set the display:none style property of that div. You the following code a guideline :
Echo the following code if the User has right user level.
Show Comments
<div id="comment-<?php echo $row['id']?>" style="display:none">
//display previous comments
<form method="post" action="addComment.php?id=<?php echo $row['id']?>">
<textarea name="comment"></textarea>
<input type="submit" value="Add Comment" /><input type="button" onclick="hideComment('<?php echo $row['id']?>')">
</form>
</div>
<script type="text/javascript">
function hideComment(id) {
document.getElementById('comment-' + id).style.display = 'none';
}
function showComment(id) {
document.getElementById('comment-' + id).style.display = 'block';
}
</script>