Welcome Guest, Not a member yet? Register   Sign In
How using foreach in controller?
#1

[eluser]tashigiri[/eluser]
Dear all..
am a newbie with development, i got a my first task and here is my example code :
COntroller
Code:
// getting list design package from database
$sql     = "SELECT * FROM package WHERE type LIKE '% design %' ORDER BY price DESC";
$result = $this->db->query($sql)->result();
$data['designs'] = $result;

//company config
$sql  = "SELECT * FROM company_configuration where user_id = '".$this->session->userdata('company_id')."'";
$result = $this->db->query($sql)->result();
$data['companys'] = $result;

View
Code:
<?php
//show normal price
        foreach ($designs as $design)
    {                            
        if ($design->reduce_type == '0')
        {
            $price = $design->price;
            echo $price;
        }
    }
    ?>

//show discount price
<?php
    foreach ($companys as $company)
    {                    
        $discount = $company->discount;
        $sp_price = $price - (($price * $discount) / 100);        
        echo $sp_price;        
    }
    ?>
My question is, i want to make "strikethrough" in normal price if price > sp_price. But when i give som if clause like that, my code will error, because when i give if clause in normal price, it will show error "undefined variable $sp_price". Thats why i want to make foreach in controller so i can easily called in view.
Anybody can help this?? Thanks for your attention
#2

[eluser]Thorpe Obazee[/eluser]
Perhaps you haven't set the variable yet when you echoed $sp_price? Show a bit more code on the part wherein you get the error.
#3

[eluser]tashigiri[/eluser]
i think i got error when i use code like this :
Code:
foreach ($designs as $design)
    {                            
        if ($design->reduce_type == '0')
        {
                $price = $design->price;
                $discount = $company->discount;
                $sp_price = $price - (($price * $discount) / 100);
                if ($price > $sp_price)
                {
                   echo "<s>".$price;echo "</s";
                }
                else echo $price;
        }
    }

Anyway i have solve this, yups my code above shown "undefined variable $sp_price" because i not yet define that so i define that again :
here is my example code now :
Code:
<tr>
      <td>Design-Anzeige</td>
    <td>
    &lt;?php
    foreach ($designs as $design)
    {                            
        if ($design->reduce_type == '0')
        {
            $price = $design->price;
            foreach ($companys as $company)
            {
                $discount = $company->discount;
                if ($discount > 0)
                {
                    echo "<s>".$price;
                    echo "</s>";
                }
                else echo $price;
            }
        }
    }
    ?&gt;  
     Credits
    </td>
    <td>
    &lt;?php
    //company config
    foreach ($companys as $company)
    {                    
        $discount = $company->discount;
        $sp_price = $price - (($price * $discount) / 100);        
        echo $sp_price;        
    }
    ?&gt;
    Credits</td>
     </tr>
#4

[eluser]oddman[/eluser]
hi tashigiri.

you're fairly new at this, so I'll cut you some slack Tongue

First and foremost - just about everything you're doing should go in the model (Except for display). Write a method in your model for that data, and format the data there so that all you have to do in the view, is check to see if a certain value is set, and alter the display accordingly.

Then in your controller, all you have to do is something like:

Code:
$data['data_i_want_in_view_variable'] = $this->model_name->get_data_method()

Then loop through in your view.
#5

[eluser]tashigiri[/eluser]
[quote author="oddman" date="1240840258"]hi tashigiri.

you're fairly new at this, so I'll cut you some slack Tongue

First and foremost - just about everything you're doing should go in the model (Except for display). Write a method in your model for that data, and format the data there so that all you have to do in the view, is check to see if a certain value is set, and alter the display accordingly.

Then in your controller, all you have to do is something like:

Code:
$data['data_i_want_in_view_variable'] = $this->model_name->get_data_method()

Then loop through in your view.[/quote]

Thanks oddman.. am very new with this and not very deep in know in model Sad..
though this was solved, ill try to short cut code to more effisient..
overall thanks for this..^^
#6

[eluser]oddman[/eluser]
hi tashigiri.

it's not about efficiency, it's about following the MVC pattern.

if you're not sure what this is (and most new people don't when they jump into a framework), I'd suggest reading up on MVC (Model View Controller) design pattern in OOP. Should give you a good idea as to "best practises" using the pattern.

Hope that helps.
#7

[eluser]tashigiri[/eluser]
[quote author="oddman" date="1240844213"]hi tashigiri.

it's not about efficiency, it's about following the MVC pattern.

if you're not sure what this is (and most new people don't when they jump into a framework), I'd suggest reading up on MVC (Model View Controller) design pattern in OOP. Should give you a good idea as to "best practises" using the pattern.

Hope that helps.[/quote]
Thanks a lot..
do you have any reference sites??
#8

[eluser]oddman[/eluser]
lol

Did you even TRY searching for any of the terms I suggested!?
#9

[eluser]tashigiri[/eluser]
[quote author="oddman" date="1240846858"]lol

Did you even TRY searching for any of the terms I suggested!?[/quote]
i just want to know, beside that i have some e-book too that still i read..^^
#10

[eluser]oddman[/eluser]
Yup, if you want to know - you'll search Wink

Not about to spoonfeed you, the terms are there. heh




Theme © iAndrew 2016 - Forum software by © MyBB