CodeIgniter Forums
I can not figure out how to call the function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: I can not figure out how to call the function (/showthread.php?tid=55209)

Pages: 1 2


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
HellO!

Problem exists, there is the private function "delivery", which must have a value that comes into it.

Code:
class Catalog extends CI_Controller {

    public function __construct() {

        parent::__construct();

            $this->load->model('mCatalog');

    }

    public function index() {

        redirect(base_url(), 'location', 301);

    }

    public function id($catId = NULL, $num = 0) {

        $data['catalog'] = $this->mCatalog->getCatalog($catId);
     //   $data['deliver'] = $this->delivery($num);
        if ($data['catalog']  == FALSE) {
            $data['error'] = "Category not found";
        }
            $this->load->view("vCatalog", $data);

    }

private function delivery($num) {

        if($num > 0) {
            return $num."%";
        } else {
            return "Free!";
        }

}

It input parameter must be passed out of the array, which is obtained through the model database. In viewer this parameter is obtained from

Code:
<?php foreach ($catalog as $row): ?>

            <td>&lt;?=$row['delivery']?&gt;</td>

&lt;?endforeach?&gt;

Here it's the problem, can not figure out how to make that "delivery" in the viewer took the $num. Thank you!


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]noslen1[/eluser]
I think you should create and use a Helper function instead of your Controller function :

/helpers/myhelper_helper.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('delivery')) {
    function delivery($num) {
        if($num > 0) {
            return $num."%";
        } else {
            return "Free!";
        }
    }
}


/controllers/catalog.php
Code:
public function id($catId = NULL, $num = 0) {
    $data['catalog'] = $this->mCatalog->getCatalog($catId);
  
    if ($data['catalog']  == FALSE) {
        $data['error'] = "Category not found";
    } else {
        $this->load->helper('myhelper');
        foreach ($data['catalog'] as $catalog) {
            $catalog['delivery'] = delivery($catalog['delivery']);
        }
    }
    $this->load->view("vCatalog", $data);
}



I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
Thank you!

But as we now bring in the viewer to the bulkhead viewer in "foreach" array did not take the last entry in the array controller parameter "delivery"?

Code:
xdebug_var_dump($catalog['delivery']);

gives value to the controller

Code:
string '5%' (length=2)
string '4%' (length=2)
string 'Free!' (length=5)
string 'Free!' (length=5)

yes - this is the correct data.

but in the viewer now insert with loop through the array $date['catalog']?


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]noslen1[/eluser]
Your view should remain the same :

Code:
&lt;?php foreach ($catalog as $row): ?&gt;

    <td>&lt;?=$row['delivery']?&gt;</td>

&lt;?endforeach?&gt;

The "delivery" field displayed here has been changed into your controller, through the helper function, and will output "5%" or 'Free!" like in your var_dump()


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
But in your case, the controller is "delivery" is written to the array $catalog and viewer displays the loop array of $data.

That's what gives me var_dump in your example:

Code:
string '5' (length=1)
string '4' (length=1)
string '0' (length=1)
string '0' (length=1)

From the controller just an array $catalog does not get in the viewer. that's the problem


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]noslen1[/eluser]
In your controller you should have
Code:
foreach ($data['catalog'] as $catalog) {

Meaning that $catalog is part of $data!
$catalog is each element of the $data variable passing through the loop.

So when next line you got
Code:
$catalog['delivery'] = delivery($catalog['delivery']);
the array $data['catalog'] is getting impacted.

So in the foreach loop, when you change $catalog, $data changes too, and as it's passed to your view, it should display the right data.

Gimme your code from Helper/Model/Controller/View please. I don't get it.


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
please get me your email, forum not posting my message, аorum writes - I spammer))


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]noslen1[/eluser]
Not sharing my email for you mate.

Just wait until you can post again.
And why could you post that last one ?
...


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
Error Message

Due to a heavy increase of spam within our community, the privilege of posting certain content is currently reserved for active community members and those who have purchased our software.

If you feel you have received this in error, please contact [email protected] with a support key of bgp68s9y-grsogk0s-8kc8ggcs-k4w48w for more information.


I can not figure out how to call the function - El Forum - 10-15-2012

[eluser]seobot[/eluser]
Hmmm

I made a call to a helper in the "viewer" and it worked as it should. That is processing in the controller is not needed, since necessary data to be passed in a single cycle.

View
Code:
$this->load->helper('catalog_helper');

foreach ($catalog as $row) {
     <td>&lt;?=delivery($row['delivery'])?&gt;</td>
}
But it would be nice if the controller handles all ... You can do that?