1

i have PHP code with loop:

$explodeResult=explode(", ",$serviceName);
        foreach($explodeResult as $value)
        {
              $sql = "SELECT id,parent,code,name,xs1 as DOWNLOAD, xs2 as UPLOAD
                       FROM sc_params
                      WHERE rfen = 'SERVICE_REQUESTED'
                      AND code = :param1 
                        OR code IN
                        ( 
                            SELECT code 
                             FROM sc_params 
                             WHERE rfen = 'SERVICE_REQUESTED'
                             AND id = (
                            SELECT parent 
                                 FROM sc_params 
                                 WHERE rfen = 'SERVICE_REQUESTED' 
                                 AND code = :param1 )
                        )
                    ";          
            $stmt = oci_parse($this->_conn,$sql);
            oci_bind_by_name($stmt, ":param1", $value);
            $rs = oci_execute($stmt);
            if (!$rs) {
                $error = oci_error();
                $this->_err = $error;
                return false;
            }

$product    = Array();
$idx        = $idx2 = 0;
    while ($data = oci_fetch_array($stmt, OCI_BOTH)) {
    if($data['PARENT'] == 0) {
        $idx++;
        $product[$idx]['id']        = $data['ID'];
        $product[$idx]['name']      = $data['NAME'];
    }
    else {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 
   }
    print_r ($test_array);

Data from query:

ID NAME      PARENT DOWNLOAD UPLOAD
1  INTERNET   0      null     null 
10 SPEED      1      2048     512
2  VOICE      0      null     null
12 PSTN       2      null     null

the array result is:

Array ( [1] => Array ( [id] => 1 [name] => INTERNET [0] => Array ( [attributeName] => DOWNLOAD [attributeValue] => 2048 )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 512
                )

        )

    [2] => Array
        (
            [id] => 2
            [name] => VOICE
            [0] => Array
                (
                    [attributeName] => DOWNLOAD
                    [attributeValue] => 
                )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 
                )
        )
)

But i just want show the attributeName and attributeValue for internet only.. i tried to add if($data['NAME'][0]='INTERNET') below while, but it doesn't work, VOICE still has attributeName and attributeValue. Please Help. Thanks

3
  • Could you provide a vardump of $data? Commented Jan 30, 2015 at 2:05
  • if you want INTERNET only, why don't set WHERE condition when query database? so you want this loop to return just one node? not array of INTERNET nodes? just one? Commented Jan 30, 2015 at 2:45
  • Two things i can think of looking at if($data['NAME'][0]=='INTERNET') 1) Shouldn't your conditional be using '==' vs '=' and 2) you be comparing to $data['NAME'] instead of $data['NAME'][0] ? Commented Jan 30, 2015 at 3:07

1 Answer 1

1

So with your data you have many possibilities how to organize that:

1st just check null:

elseif (!($data['DOWNLOAD']==null && $data['UPLOAD']==null)) {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 

2nd Check current parent name so:

if($data['PARENT'] == 0) {

    $idx++;
    $product[$idx]['id']        = $data['ID'];
    $product[$idx]['name']      = $data['NAME'];
    $curParent = $data['NAME'];
} elseif ($curParent == 'INTERNET') {

    $product[$idx][0]['attributeName']   = 'DOWNLOAD';
    $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
    $product[$idx][1]['attributeName']   = 'UPLOAD';
    $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
}

and 3rd - you can fix your query to get DOWNLOAD and UPLOAD right to the INTERNET row. If you really need that I can help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.