1

I have a query which as only one row has a result and I am trying to store its content inside an array. The problem is that the code is putting inside the array $vs31 five times the real result and I don't understand why.

$query1 = "SELECT PROCESSO,DATAMSG, CATEGORIA_DESC, VALOR, CRITICO FROM TABLEX";
$result1 = oci_parse($connect, $query1);
oci_execute($result1);

$c1=array();
oci_fetch_all($result1, $c1, null, null, OCI_FETCHSTATEMENT_BY_ROW);

$f=0;

foreach ($c1 as $row) {
    foreach ($row as $key => $value) {

        $vs3=array();
        $f++; 
        $vs3['id']= $f;
        $vs3['title']='VSC - '.$row['CATEGORIA_DESC'];
        $vs3['startdate']=$row['DATAMSG'];
        $vs3['enddate']=$row['DATAMSG'];
        $vs3['description']= 'VALOR - '.$row['VALOR'];
        $vs3['date_display']='ho';
        $vs3['icon']='plus_blue.png';
        $vs3['importance']='30';
        $vs3['css_class']='hot-event';

        array_push($vs31,$vs3);
    }
}

print_r($vs31);

What I get:

Array
(
    [0] => Array
        (
            [id] => 103
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
    [1] => Array
        (
            [id] => 104
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
    [2] => Array
        (
            [id] => 105
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
    [3] => Array
        (
            [id] => 106
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
    [4] => Array
        (
            [id] => 107
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
)

What it want:

Array
(
    [0] => Array
        (
            [id] => 103
            [title] => VSC - ECG_HR                        
            [startdate] => 2015-04-27 23
            [enddate] => 2015-04-27 23
            [description] => VALOR - 59
            [date_display] => ho
            [icon] => plus_blue.png
            [importance] => 30
            [css_class] => hot-event
        )
)
3
  • Your query is returning 5 results and you're looping over them and pushing them into $vs31 array. That's because your table has 5 entries and you're not using a WHERE clause to filter them. If you want a different output, try changing your query or use LIMIT 1. Commented Oct 7, 2015 at 14:21
  • Not true the table has only 1 entry not 5 so I don't need a WHERE clause for anything. Commented Oct 7, 2015 at 14:27
  • You're right, I'm sorry, I was looking only to id field. If you do echo '<pre>'; print_r($c1); before the for statement, what do you get? Commented Oct 7, 2015 at 14:31

1 Answer 1

2

You are taking only one value and pushing it multiple times and You are not using $value any where in the loop. Try this.

foreach ($c1 as $row) {


        $vs3=array();
        $f++; 
        $vs3['id']= $f;
        $vs3['title']='VSC - '.$row['CATEGORIA_DESC'];
        $vs3['startdate']=$row['DATAMSG'];
        $vs3['enddate']=$row['DATAMSG'];
        $vs3['description']= 'VALOR - '.$row['VALOR'];
        $vs3['date_display']='ho';
        $vs3['icon']='plus_blue.png';
        $vs3['importance']='30';
        $vs3['css_class']='hot-event';

        array_push($vs31,$vs3);

}

Let me know if that works fine..

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.