CodeIgniter Forums
Something wierd here - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Something wierd here (/showthread.php?tid=41538)



Something wierd here - El Forum - 05-10-2011

[eluser]Todlerone[/eluser]
Hello everyone and TY in advance. For the life of me I can't figureout why this will not work (I'm sure is sooooo simple...but OMG I think I'm brain dead).

This is in a view with some $data. Here is the output of the $plan_fract array. I have used and end() function in the controller to always get the last row in the database so my array will always only contain data like below (ie, one row).
Code:
Array
(
    [plan_fraction_num] => 1
    [plan_fraction_partial_fraction] => No
    [plan_fraction_pct_deli] =>
    [plan_fraction_pct_total] =>
)

I'm trying to do this...
Code:
echo '<pre>'; print_r($plan_fract); echo '</pre>';

foreach ($plan_fract as $pf):
        echo "partial=".$pf['plan_fraction_partial_fraction']."<br />";
    if ($pf['plan_fraction_partial_fraction'] == 'No'){
        $fract = $pf['plan_fraction_num']+1; // increase 1 to 2, 2 to 3....
    }
    if ($pf['plan_fraction_partial_fraction'] == "Yes"){
        $len = strlen($pf['plan_fraction_num']);// the num can also be 1m1, 1m2...
        if ($len == 1){
            $fract = $pf['plan_fraction_num'].'m1';//if previous was 1, the new is 1m1
        }
        if ($len == 3){//if 1m1, 1m2, 1m3, 2m1....
            $frac_NUMs = substr($pf['plan_fraction_num'], 0,2);// get the first two char 1m
            $frac_NUMe = substr($pf['plan_fraction_num'], -1);//get the last char
                $fract = $frac_NUMs.($frac_NUMe+1);// increase the last only
        }
            
            }
            //echo $fract;
        endforeach;

I get this as an output...
partial=1
partial=N
partial=
partial=


I never get into my if loops, so I added the echo partial to see what it was seeing....


Something wierd here - El Forum - 05-11-2011

[eluser]n0xie[/eluser]
What's there to not understand?

It loops over each value in your array (see your first code snippet) and echo's that, so you are seeing exactly what you are supposed to see, namely each value inside your array...

Hence the output is exactly the same as the content of your array. Any alteration to your values is done after the echo...


Something wierd here - El Forum - 05-11-2011

[eluser]Todlerone[/eluser]
code]Array
(
[plan_fraction_num] => 1
[plan_fraction_partial_fraction] => No
[plan_fraction_pct_deli] =>
[plan_fraction_pct_total] =>
)
[/code]

I'm trying to do this...
Code:
echo '<pre>'; print_r($plan_fract); echo '</pre>';

foreach ($plan_fract as $pf):
    if ($pf['plan_fraction_partial_fraction'] == 'No'){
        $fract = $pf['plan_fraction_num']+1; // increase 1 to 2, 2 to 3....
    }
    if ($pf['plan_fraction_partial_fraction'] == "Yes"){
        $len = strlen($pf['plan_fraction_num']);// the num can also be 1m1, 1m2...
        if ($len == 1){
            $fract = $pf['plan_fraction_num'].'m1';//if previous was 1, the new is 1m1
        }
        if ($len == 3){//if 1m1, 1m2, 1m3, 2m1....
            $frac_NUMs = substr($pf['plan_fraction_num'], 0,2);// get the first two char 1m
            $frac_NUMe = substr($pf['plan_fraction_num'], -1);//get the last char
            $fract = $frac_NUMs.($frac_NUMe+1);// increase the last only
        }
            
            }
        endforeach;
     echo $fract;
If I do it this way I get a Undefined variable: fract. It doesn't get into either if loop so the variable is not set. However the array value I'm checking is one of the loops?

TY


Something wierd here - El Forum - 05-11-2011

[eluser]n0xie[/eluser]
Yeah because when you foreach your array, $pf['plan_fraction_partial_fraction'] doesn't exist since $pf gets assigned the values of your array as shown in the output of your first example.

if you want to make your code work do this:
Code:
echo '<pre>'; print_r($plan_fract); echo '</pre>';
$my_array = array($plan_fract);

foreach ($my_array as $pf):
...



Something wierd here - El Forum - 05-11-2011

[eluser]InsiteFX[/eluser]
You need to define your variable before you can use it.
In your Controller:
Code:
public $fract;

// access it in your view
$this->fract = 'something';

InsiteFX


Something wierd here - El Forum - 05-11-2011

[eluser]Todlerone[/eluser]
[quote author="n0xie" date="1305141374"]Yeah because when you foreach your array, $pf['plan_fraction_partial_fraction'] doesn't exist since $pf gets assigned the values of your array as shown in the output of your first example.

if you want to make your code work do this:
Code:
echo '<pre>'; print_r($plan_fract); echo '</pre>';
$my_array = array($plan_fract);

foreach ($my_array as $pf):
...
[/quote]

TY n0xie. That was the trick. Funny though, I haven't had this problem before with passing arrays to my view. I have to work through the reason again for this so that I truely understand it.
CHEERS