Welcome Guest, Not a member yet? Register   Sign In
query results
#1

[eluser]chejnik[/eluser]
Hello, I am having troubles with understanding query results.

I am having this function in model view to get one row of table (just app 24 values)
Code:
function selectStructure($i)
    {
      $tableSettings ='databaseSettings'.$i;  
        // collate is missing
        $this->db->select('databaseRowName');
    $this->db->from($tableSettings);
    $query = $this->db->get();
     return $query->result_array();
    }

I would like to fill an array like bellow with that row of table

Code:
$data = array(  
    '(value of the row)'   => $_POST['(value of the row)']
     );

I dont know how to do it. Thank you
#2

[eluser]xwero[/eluser]
To get only one row of the table you have to use
Code:
$query->row_array();
It will produce an array like this : array('fieldname'=>'value','fieldname'=>'value')
#3

[eluser]chejnik[/eluser]
Hello,
I wrote it wrongly. The query returns a column (not a row). Thats why it is needed result_array not row_array.
Code:
function selectStructure($i)
    {
      $tableSettings ='databaseSettings'.$i;  
        // collate is missing
        $this->db->select('databaseRowName');
    $this->db->from($tableSettings);
    $query = $this->db->get();
      return $query->result_array();
    }

I am still having troubles to work with this array.
I printed the new arrays. This shows
Code:
print_r($structure);
Array ( [0] => Array ( [databaseRowName] => id ) [1] => Array ( [databaseRowName] => headword ) [2] => Array ( [databaseRowName] => numberHeadword ) [3] => Array ( [databaseRowName] => stem ) [4] => Array ( [databaseRowName] => order ))

This shows
Code:
print_r(array_keys($structure));
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4)

How can I make from this array a new array with structure bellow?
Code:
$data = array(
                    
    '(value of first column of the first row)'   => $_POST['(value of first column of the first row)'],
       ..
        '(value of first column of the last row)' => $_POST['(value of first column of the last row)']
     );

Thank you
#4

[eluser]tonanbarbarian[/eluser]
you are just going to have to process the array and put it in the structure you want
#5

[eluser]Elliot Haughin[/eluser]
OK, try something like this...

Code:
// $results is from from the database

foreach( $results as $row) ):
    
    $built_array[$row['databaseRowName']] = $_POST[$row['databaseRowName']];

endforeach;

Then, based off the print_r you gave me, $built_array should be structured like:

Code:
array(
     'id' => $_POST['id'],
     'headword' => $_POST['headword'],
     'numberHeadword' => $_POST['numberHeadword'],
     'stem' => $_POST['stem'],
     'order' => $_POST['order']
);

I hope that's the expected outcome, and this helps.

Elliot
#6

[eluser]chejnik[/eluser]
Thank you Eliot, it helped me very much.See you




Theme © iAndrew 2016 - Forum software by © MyBB