Welcome Guest, Not a member yet? Register   Sign In
How to manage fields in my view
#1

[eluser]il_dandi[/eluser]
Hi! My script works but I would like your suggestion if something isn't right of there is a best/new way

(in the same view I manage add and update)

My controller


Code:
function add(){
        $data['countries']=$this->db->get('countries');
        $data['tipo']="insert new";
        $this->template->write_view('content', 'admin/avatar', $data, TRUE);
        $this->template->render();
    }
    
    function add2(){
        $idUtente=$this->Login_model->idUtente();
        $_POST['idUser']=$idUser;
        $idAvatar=$this->Avatars_model->add();
        redirect(base_url()."admin/avatars/");
    }
    
    function mod($id){
        $data['countries']=$this->db->get('countries');
        $avatar=$this->Avatars_model->avatar($id);
        $data['avatar']=$avatar;
        $data['tipo']="update";
        $this->template->write_view('content', 'admin/avatar', $data, TRUE);
        $this->template->render();
    }
    
    function mod2(){
        $idUtente=$this->Login_model->idUtente();
        $this->Avatars_model->update($_POST["id"],$idUtente);
        redirect(base_url()."admin/avatars");
    }


my View


Code:
<?
$id=0;
$firstname='';
$lastname='';
$age=18;
$idCountry=0;
$formCommand="admin/avatars/add2";
if ($tipo!="aggiungi"){
    $id=$avatar->id;
    $firstname=$avatar->firstname;
    $lastname=$avatar->lastname;
    $age=$avatar->age;
    $idCountry=$avatar->idCountry;    
    $formCommand="admin/avatars/mod2";
}
?>

<?=form_open($formCommand);?><?=form_hidden('id', $id)?>


<table width="100%">
<tr>
<td width="100px" align="right">fistname: </td>
<td>&lt;input name="firstname" type="text" id="firstname" size="30" maxlength="255" value="&lt;?=$firstname?&gt;"&gt;&lt;/td>
</tr>

<tr>
<td align="right">lastname: </td>
<td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="&lt;?=$lastname?&gt;" &gt;&lt;/td>
</tr>

<tr>
  <td align="right">age: </td>
  <td>&lt;?=$age?&gt;</td>
  </tr>
<tr>
  <td align="right">nazione: </td><td>
<select name="idCountry">
&lt;? foreach ($countries->result() as $row):?&gt;
    <option value="&lt;?=$row->id?&gt;" &lt;? if ($idCountry==$row->id) echo " selected";?&gt;>
    &lt;?=$row->description?&gt;
    </option>
&lt;? endforeach?&gt;
</select>
  </td>
  </tr>

  <tr>
  <td colspan="3" align="right">&lt;input type="submit" value="&lt;?=$tipo?&gt;"&gt;&lt;/td>
  </tr>

</table>
            
&lt;/form&gt;


I hope that you can help me!!
#2

[eluser]eveiga[/eluser]
Hey there!

Try to start coding your html the right way, if your making a form with no tabular data, use divs/fieldsets/labels/inputs etc. and style it with css's.
It's always better when you try to follow the web standarts Smile

Edgar
#3

[eluser]il_dandi[/eluser]
Yes!!! You'are right!!

what do you think about the code?? Controller and PHP code in the View...

(the Model it's easy)

Thanks
#4

[eluser]il_dandi[/eluser]
Hi!!!

Can you tell me if my code is OK??

I would like to know if my solution is the best way to manage update/insert in the same page.

Before starting my project I would like to have the right structure in my PHP files!

Hope that you can help me!

Thanks
#5

[eluser]jedd[/eluser]
Quote:Can you tell me if my code is OK??

Howdi,

Things I'd change, off the top of my head.

I'd not use a number (eg. 2) to provide meaning to a function name - if it confuses me now, it'll confuse you later. Describe what the function does, and if there's two similar functions, obviously adjust your function name to highlight the difference.

I'd definitely re-read the section on CI's [url="http://ellislab.com/codeigniter/user-guide/libraries/table.html"]Table Class[/url] as that should make your life easier.

I'd remove any direct reference to $_POST
#6

[eluser]il_dandi[/eluser]
Thanks for help me and for the suggestion!

About the number in functiona name, you are right ... I will change the names!

About the view... Do you think that is it right manage update/insert with my solution?? Or there is a best solution??


Quote:I’d definitely re-read the section on CI’s Table Class as that should make your life easier.

Do you have seen errors in my code??


Thanks
#7

[eluser]jedd[/eluser]
No, no errors. It just seems that you might get what you want via the CI Table Class (of course, you might not, too Wink)

I don't know why you' initialise some variables to empty values and then assign them other values - I think you only need to do that if you're going to add (concat, or insert new vars into an array, etc) to them.

I'd completely avoid using short PHP tags, but that's in part a matter of style rather than technicality.

I don't know why you use form_open() functions and then use raw input tags (rather than using the form helper for everything form-ish).
#8

[eluser]il_dandi[/eluser]
[quote author="jedd" date="1237837584"]No, no errors. It just seems that you might get what you want via the CI Table Class (of course, you might not, too Wink)
[/quote]


Can you tell me how could I obtain the same result using CI Table Class??


[quote author="jedd" date="1237837584"]
I'd completely avoid using short PHP tags, but that's in part a matter of style rather than technicality.
[/quote]


oky!!! Thanks!!



[quote author="jedd" date="1237837584"]

I don't know why you use form_open() functions and then use raw input tags (rather than using the form helper for everything form-ish).[/quote]

Can you tell me how to modify my form for do what you have writte??

Thansk for help me!!
#9

[eluser]jedd[/eluser]
Quote:Can you tell me how could I obtain the same result using CI Table Class??

Have a read of the CI [url="http://ellislab.com/codeigniter/user-guide/libraries/table.html"]HTML Table Class[/url]

Quote:Can you tell me how to modify my form for do what you have writte??

Have a read of the CI [url="http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html"]Form Helper[/url]

Of particular interest - the form_input() function.




Theme © iAndrew 2016 - Forum software by © MyBB