Loop in PHP to create array with PHP and Mysql

Asked

Viewed 528 times

1

I want to fill an array with data coming from the database with PHP and Mysql, the array is being filled, but not in the format that would be most functional for the application.
Some hint to change the loop to get to the Idel format of the array?

This is the ideal format for the application:

Array
(
    [0] => survey
    [1] => processing
    [2] => factoring
    [3] => closed
    [4] => developing
)


Loop to create the array:

public function fillArrayPrStatus() {
        try {   
            $this->conn = parent::getConnection();
            $this->pQuery = $this->conn->prepare(   'select status_pr_status '.
                                                    'from pr_status '.
                                                    'where   status_pr_lang=:sessionLanguage'
                                                );
            $this->pQuery->execute(array(   ':sessionLanguage' => session::sessionLanguage()    ));
            if($this->pQuery->errorCode() == 0) {
                $this->get_rows = $this->pQuery->fetchAll();
                foreach($this->get_rows as $this->get_row)  {
                    $this->arrayPrStatus[] = $this->get_row;    
                }   
                return; 
            }   
            else     {
                throw new Exception();
            }   
        }   
        catch (Exception $e)    {
            $tException = new tException();
            $tException -> newException($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
        }   
    }



Result after the loop:

Array
(
    [0] => Array
        (
            [status_pr_status] => processing
        )

    [1] => Array
        (
            [status_pr_status] => closed
        )

    [2] => Array
        (
            [status_pr_status] => developing
        )

    [3] => Array
        (
            [status_pr_status] => survey
        )

    [4] => Array
        (
            [status_pr_status] => factoring
        )

)
  • I think $this->get_row is already an array, right? Give me an example of the format of this, print one of them

  • Miguel.. $this->get_row is set to array, but with no defined value, use this variable only in loops. Just like the $this->arrayPrStatus variable.

1 answer

1


Experiment on the line where you insert the new value into the array, inside the foreach:

...
$this->arrayPrStatus[] = $this->get_row['status_pr_status'];
...
  • Miguel... thanks. Now you’re right. <br> Array ( [0] => Processing [1] => closed [2] => Developing [3] => Survey [4] => factoring )

  • You’re welcome @RRV

Browser other questions tagged

You are not signed in. Login or sign up in order to post.