Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Dynamic URI and Validation problem.
#1

[eluser]FnTm[/eluser]
Hi everyone!

I am currently working on 2 very similar homepages, and ive run into some problems with the validation.

So i have my site with some news and stuff. When an admin clicks on an edit link either at the front end of the site, or in the admin backend, it sends you to a page like

http://www.mysite.com/index.php/site/news/3

So i use the uri segment method to get the right news item to edit. All of this works very well. But when i try to edit something, and it fails to validate, how should i take someone back to that page?

How do I add the uri segments to my
Code:
$this->load->view()
function?

Thanks
FnTm
#2

[eluser]Ebot Ndip-Agbor[/eluser]
Try using
Code:
$this->uri->uri_string()
as your form action. It is a function of the CI URI class. It returns the user/admin to the same page if validation fails.
#3

[eluser]FnTm[/eluser]
Thanks, but could you please give some more details on how this works?

P.S.

Ive found this wiki article, and used it a bit, and i have just one question, till ill be able to get it working... Where do i put the script, and how do i load it??
#4

[eluser]Ebot Ndip-Agbor[/eluser]
post some of your code
#5

[eluser]FnTm[/eluser]
controllers/news
Code:
*/
    function edit($id)
    {
    if ($id == NULL){echo 'yes'; $id = $this->uri->segment(4); }
        //$id = $this->uri->segment(4);
        $this->db->where('id', $id);

        $data['query'] = $this->db->query('SELECT * FROM zinas where id = '.$id.' ');
       $this->load->helper(array('form', 'url'));
    
            if ($this->uri->segment(4) == NULL){$data['rev_id'] = $id; }
            $data['heading'] = 'Labot jaunumu';
            $data['action'] = 'Labot jaunumu';
            $data['controller'] = 'users';
            $data ['page'] = $this->config->item('FAL_template_dir').'template_admin/news/edit';
            
          
            
            $this->load->vars($data);
            
$this->load->view($this->_container);
    
            
    }
    
        // --------------------------------------------------------------------
    
    function edit_check()
    {  

        
$id = $this->input->post('id');
$this->load->helper(array('form', 'url'));
    
    $this->load->library('validation');
        
    $rules['title']    = "required|min_length[5]";
    $rules['saturs']    = "required|min_length[5]";
    
    $this->validation->set_rules($rules);
    
    $fields['title']    = 'Nosaukums';
    $fields['saturs']    = 'Apraksts';

    $this->validation->set_fields($fields);

            //$this->output->enable_profiler(TRUE);
    
        
    if ($this->validation->run() == FALSE)
    {
      return $this->edit($id);    
    }
    else
    {
    /*
        $this->title   = $_POST['title'];
        $this->body = $_POST['apraksts'];
        $this->entered_by  =  $_POST['autors'];
        $this->parent  =  $_POST['vecaks'];
        */
        $insert = array(
                'title' => $this->input->post('title'),
                'body' => $this->input->post('saturs'),
                'entered_by' => $this->input->post('autors'),
                'kategorija' => $this->input->post('vecaks'),
                'member' => $this->input->post('member'),
                'shown' => $this->input->post('shown')
                );
        $this->db->where('id',$this->input->post('id'));
        $this->db->update('zinas', $insert);

        //$this->db->insert('kategorijas', $this);

    
        $this->load->view('formsuccess');
    }
            
    }


views/edit
Code:
<?
$row1 = $query->row();
echo anchor('admin/news', 'Atpakaļ uz jaunumiem');
echo form_open('admin/news/edit_check'); ?>

<h5>Nosaukums</h5>
&lt;input type="text" name="title" value="&lt;?=$row1-&gt;title;?&gt;" />
<br><h5>Vecāks</h5>
&lt;?
$sql = "SELECT * from kategorijas";
$row = mysql_query($sql);
$options ="<br><select name=\"vecaks\"><option value=\"1\" selected=\"selected\">opcija</option>";
while($rows = mysql_fetch_array($row)) {
$options .= "<option value=".$rows['id'].">". $rows['title'] ."</option>";
}
$options .="</select>";

echo $options;
echo $rev_id;

?&gt;

Tikai biedriem?
<select name="member">
<option value="1">Jā</option>
<option value="0">Nē</option>
</select>
Vai ir redzams?
<select name="shown">
<option value="1">Jā</option>
<option value="0">Nē</option>
</select>
<br>
<h5>Apraksts</h5>
<br>
&lt;?//echo  $this->validation->saturs;?&gt;
&lt;textarea type="text" name="saturs"  size="50" &gt;&lt;?echo $row1->body;?&gt;&lt;/textarea&gt;
&lt;input type="text" name="id" value=&lt;?if ($this-&gt;uri->segment(4) == NULL){echo $rev_id;} else {echo $this->uri->segment(4); } ?&gt;>
&lt;input type="hidden" name="autors" value=&lt;?=getUserProperty('id');?&gt; &gt;




<div>&lt;input type="submit" value="Submit" /&gt;</div>
&lt;?=loginAnchor('auth/login', 'auth/logout');



?&gt;


Really "clean" code, needs a lot of revising, but im just trying to get this sorted out before i start anything else.

Anyone could tell me where to put that script from the bottom of wiki page? Because im in sorta of rush.
#6

[eluser]Ebot Ndip-Agbor[/eluser]
I took a look at the wiki article you mentioned. Try addiing this to your view
Code:
&lt;?php
if(!isset($id)){
  $id = @field($this->uri->segment(4, NULL), $this->validation->id, 'X');
}

echo form_open(
  'admin/news/edith_check',
  '',
  array('id' => $id)
);
?&gt;

instead of
Code:
echo form_open('admin/news/edit_check'); ?&gt;
#7

[eluser]FnTm[/eluser]
Thanks, but that is not the issue.
What i REALLY would like to find out is, where do i put script, that makes use of the
Code:
@field
function?

EDIT: Problem solved

Just had to make a map inside my applications folder named scripts, make a file, and put all of the code inside the file. None other stuff needed. The just load the script with the load function.

Will clean my code, and have this thing up and running in no time.

Thanks batangai for your input on this subject, really appreciated your trying to help me.
#8

[eluser]Ebot Ndip-Agbor[/eluser]
Great. Good luck with your work. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB