jhunlio
jhunlio

Reputation: 2660

my custom function not working

i create function called empty_fields but i can't figure out, to make it work..

<?php
function empty_fields($field_name)
{
    if(!empty($order[$field_name]))
    {
        $output = "<li>Indigofera - " . $order[$field_name] . "Kg</li>";
    } else { $output = null; }

    return $output;
}

to display in html

<?php empty_fields('indigofera'); ?>

Upvotes: 0

Views: 103

Answers (2)

Raphael M&#252;ller
Raphael M&#252;ller

Reputation: 2200

If you use $order as a global variable, use:

function empty_fields($field_name)
{
    global $order;

    $output = null;
    if(!empty($order[$field_name]))
    {
        $output = "<li>Indigofera - " . $order[$field_name] . "Kg</li>";
    }

    return $output;
}

and use it:

<?php echo empty_fields('indigofera'); ?>

EDIT: the OOP way:

class Orders
{
     private $order = null;

     public function get_order()
     {
         $this->order = //....
     }

     public function empty_fields($field_name)
     {
         if(!isset($this->order) || empty($order[$field_name])) return;

         return "<li>Indigofera - " . $order[$field_name] . "Kg</li>";
     }
}

and use it:

<?php

$orders = new Orders();
$orders->get_order();
echo $orders->empty_fields("indigofera");

Upvotes: 1

Pupil
Pupil

Reputation: 23948

Change

if(!empty($order['$field_name'])){

to

if(!empty($order[$field_name])){

'$field_name' does not evaluate to anything and it will search for

key '$field_name' in the $orders array everytime, hence will not work.

Hope it works.

Upvotes: 2

Related Questions