CodeIgniter Forums
edit and add logic - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: edit and add logic (/thread-65531.html)



edit and add logic - davy_yg - 06-23-2016

Hello,

Can anyone help me do this?  This is my add_pages form which I would like to use for editing also.

Therefore if someone from pages.php click button add the below add_pages.php form appears with empty form and if the user click edit button.  The previous data will loads in add_pages.php

in value="" therefore should be an if statement.


If someone click edit button -->  therefore should be a certain value in value="".  How to write the correct if statement or code?

Thanks in advance.



views/add_pages.php


PHP Code:
<table border="0" style="width: 100%; height: 90px;">
                            <tr>
                                <td>Pages Name:</td>
                                <td><input type="input" name="pages_name"
                                value="
                                
                                <?php
                                
                                echo 
$this->pages_model->get_pages();
                                
                                ?>
                                
                                ></td>
                            </tr>
                            <!--
                            <tr>
                                <td>Create Date:</td>
                                <td><input type="
input" name="create_date" size="2"></td>
                            </tr> -->                            
                            <tr>
                                <td>Order:</td>
                                <td><input type="
input" name="pages_order" size="4"></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><input type="
submit" name="submit" value="Submit" /></td>
                            </tr>    
                        </table>        




controllers/cpages.php


PHP Code:
    public function addpages() 
    { 
 
        
        
        $this
->form_validation->set_rules('pages_name''pages_name''required');
        
$this->form_validation->set_rules('create_date''create_date''required');
        
$this->form_validation->set_rules('pages_order''pages_order''required');
        
$this->form_validation->set_rules('pages_content''pages_order''required');
        
                
                
            if (
$this->form_validation->run() === FALSE)
            {
            
$this->load->view('add_pages');
            }
                
            else
            {
            
$this->pages_model->insert_pages();        
            
$this->load->view('add_pages');                
            }
                        
         
 
    
    
}
    
    public function 
editpages()
    {
        
         
   $data['show'] = $this->pages_model->get_pages(); 
        
            
$this->load->view('add_pages'$data);    
            
    } 



RE: edit and add logic - JayAdra - 06-23-2016

Read. The. Docs.

http://www.codeigniter.com/userguide3/libraries/form_validation.html#re-populating-the-form


RE: edit and add logic - Martin7483 - 06-23-2016

(06-23-2016, 04:48 AM)JayAdra Wrote: Read. The. Docs.

http://www.codeigniter.com/userguide3/libraries/form_validation.html#re-populating-the-form

Agreed he should read the docs, but that link does not help in this case. He wants to populate fields with values from a DB record.

@davy_yg
First: Don't use tables for layout. Use divs

Now as for code, your question has little to do with CI and more with PHP in general.
Your page will need a flag telling it if it is in ADD mode OR EDIT mode. In ADD mode you could set an empty array with key names that match your field names. Use this array in your view setting each value.
PHP Code:
value="<?php echo $record['page_name']; ?>" 

In EDIT mode you will fill the empty array with the correct values, thus your form will have the values set for editing


RE: edit and add logic - davy_yg - 06-23-2016

Thanks I have read the docs.

Another thing that I am confuse, look at this one:

I try to click the edit button and nothing happen.  It does not bring me to the next page either.  How to make the edit button brings me to the next pages?


views/pages.php


PHP Code:
<?php foreach ($pages as $pages_item): ?>
                                    
                        <table border="0" style="width: 100%; height: 40px;"> 
                        
                        
                            <tr>
                                <td width="200px"><?php echo $pages_item->pages_name?></td>
                                <td width="300px"><?php echo $pages_item->create_date?></td>
                                <td width="100px"><?php echo $pages_item->pages_order?></td>
                                <td width="200px"><button type="button" class="edit" href="<?php echo base_url();?>/index.php/cpages/addpages">EDIT</button></td>
                                <td width="300px"><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </tr>    
                                
                        </table>        

                        <?php endforeach; ?>


views/add_pages.php

PHP Code:
<table border="0" style="width: 100%; height: 90px;">
                            <
tr>
                                <
td>Pages Name:</td>
                                <
td><input type="input" name="pages_name"
                                
value="<?php echo set_value('pages_name'); ?>"></td>
                            </
tr>
                            <!--
                            <
tr>
                                <
td>Create Date:</td>
                                <
td><input type="input" name="create_date" size="2"></td>
                         
   </tr> -->                            
                            <
tr>
                                <
td>Order:</td>
                                <
td><input type="input" name="pages_order" value="<?php echo set_value('pages_order'); ?>" size="4"></td>
                         
   </tr>
                            <
tr>
                                <
td></td>
                                <
td><input type="submit" name="submit" value="Submit" /></td>
                         
   </tr>    
                        </
table



RE: edit and add logic - InsiteFX - 06-23-2016

Maybe this will help:

PHP Code:
<div id="login">
    
    <?
php if(empty($username)) { ?>
    <h2>Register</h2>
    <?php } else { ?>
    <h2>Update</h2>
    <?php ?>
    
    <div class="box">
            <form method="post">
            <?php if(empty($username)) { ?>
            Username:<br />
            <input type="text" name="username" size="50" class="form" value="<?php echo set_value('username'); ?>" /><br /><?php echo form_error('username'); ?><br />
            Password:<br />
            <input type="password" name="password" size="50" class="form" value="<?php echo set_value('password'); ?>" /><?php echo form_error('password'); ?><br /><br />
            Password confirmation:<br />
            <input type="password" name="conf_password" size="50" class="form" value="<?php echo set_value('conf_password'); ?>" /><?php echo form_error('conf_password'); ?><br /><br />
            <?php ?>
            Email:<br />
            <?php if(empty($username)){ ?>
                <input type="text" name="email" size="50" class="form" value="<?php echo set_value('email'); ?>" /><?php echo form_error('email'); ?><br /><br />
            <?php }else{ ?>
            <input type="text" name="email" size="50" class="form" value="<?php echo set_value('email'$email); ?>" /><?php echo form_error('email'); ?><br /><br />
            
            <?php } if(empty($username)) { ?>
            <input type="submit" value="Register" name="register" />
            <?php } else { ?>
            <input type="submit" value="Update" name="register" />
            <?php ?>
            </form>
    </div>
</div> 



RE: edit and add logic - ivantcholakov - 06-23-2016

Here is something I've proposed before about add/edit logic implementation: http://forum.codeigniter.com/thread-65146-post-331960.html#pid331960