Welcome Guest, Not a member yet? Register   Sign In
does it possible to simplify this source code ?
#1

[eluser]souri84[/eluser]
Hi everybody,

I have a problem, I think my code is bad.

To explain you, $contract is an array brought to my view by a controller. It's the same thing for $product.

Is there another method to replace foreach(... as $row) when I have an array as variable ?

Code:
//on créé la liste des contrats
        foreach($contract as $row)
        {
            $tableResult .='<tr>';
                // on créé le select des produits et on selectionne le bon en fonction du contrat du client
                $strProd = '<select name="produits" class="cqte">';
                foreach($product as $row1)
                {
                    
                    if($row[0]== $row1[1])
                    {
                        $strProd .='<option selected value="'.$row1[0].'">'.$row1[1].'</option>'
                        ;
                    }
                    else
                    {
                        $strProd .='<option value="'.$row1[0].'">'.$row1[1].'</option>';
                    }
                }
                $strProd .= '</select>';
                
            $tableResult .='<th>'.$strProd.'</th>';

best reguards,

Thibaut
#2

[eluser]Nick_MyShuitings[/eluser]
The only thing I think you could "clean up" there is if you want to use the form_helper to create that select instead of using the foreach directly (same end result, but cleaner code)
#3

[eluser]souri84[/eluser]
ok.. I have created a fonction to reduce my code and i insert it before doctype of my view...

thanks !
#4

[eluser]Nick_MyShuitings[/eluser]
Interesting... if you find yourself re-utilizing that function in other places.. then consider making a helper.
#5

[eluser]boltsabre[/eluser]
Souri84,

Your code looks fine (at a casual glance), what you've done is pretty much the standard way of doing this kind of thing, and don't worry about the length of it - try doing all this (ie, your model calls, controller 'controlling' and view displaying) in the old fashioned procedural way and your code would be much much much bigger that what you have now!

Ummm, but why are you inserting anything before your doctype in your view file? this is a big no no... if you need a function, create a helper, store your function in there, load your helper in your controller, and call it in your view where you need it (but NOT BEFORE your doctype) - this way you can use your function over and over in any view)

ie: your function is called 'makeMySelectMenu($var)', so in your view call as such (just make sure you've loaded your helper file in your controller!)

&lt;/head&gt;
&lt;body&gt;
<h1>Page Title</h1>
&lt;?php makeMySelectMenu($var); ?&gt;
#6

[eluser]souri84[/eluser]
Because, i'm starting with MVC and i didn't think to create helper but it's my need ! so I will create helper to execute my differents functions and loops.

Great thanks to Nick_MyShuitings and you !
#7

[eluser]boltsabre[/eluser]
No worries, hope you get it sorted out, I started with CI a few weeks ago, and it's all a bit strange at first compared to the old school way of doing things, but you'll pick it up really quick if you keep at it!

And don't forget to google 'codeigniter....(your question)', chances are someone has already asked it and you won't have to wait for a reply on here!




Theme © iAndrew 2016 - Forum software by © MyBB