-1

I am currently trying to create a mediawiki extension that has a form with a dropdown list and a submit button. In this form I would like to read all my database objects (tables) in the dropdown. if I run it as a single php script it works fine, and it has the following syntax.

<?php 
...
$stid = oci_parse($conn,$query);                                                      
oci_execute($stid);
?>

//Form
<form method="get" action="generateScript.php">
<select name="getTable">
</form>

<?php 
while ($row = oci_fetch_array($stid)) {
echo "<option value=".$row[0].">".$row[0];      
}
...

In the case of mediawiki extensions, you have to put it in a functions and to use mediawiki core global variables for the output.

Therefore, I created 3 functions:

1 function - builds connection with the DB server

2 function - executes my SQL query and returns the result in an array:

public function readData () {
    global $wgOracleUser;

    //@$_SESSION['getTable'] = $getTableValue;
    $ora_conn = $this -> getConnect ();
    $schema_name = strtoupper($wgOracleUser);
    $stid = oci_parse($this -> getConnect()," SELECT table_name
                                              FROM dba_tables 
                                              WHERE owner ='" .$schema_name ."'
                                              ORDER BY    table_name");

                        if(!$ora_conn) {
                            return "No connection";
                                       }
                        else           {
                            oci_execute($stid);
                            while ($row = oci_fetch_array($stid)) {
                            //fetches into an array
                            $values[] = $row;
                            return $values;
                            //echo var_dump($values);
                                                                  } 

    //Close Oracle connection
    oci_close($ora_conn);
                                      }
                         }  

3 function - shows the mediawiki html-form with the dropdown and submit:

function showForm() {
    global  $wgScript, $wgOut;
    $msgTabList = $this -> readData();

    $wgOut->addHTML('
    <tr style="margin-top: 2em">
        <td align="right">' . $msgSelTable . '</td>
        <td> <select name="formTables"> <option value="">'.$msgTabList.'</option>
        </select></td>
    </tr>

    <tr style="margin-top: 2em">
        <td align="right"></td>
        <td style="padding-top: 1em" align="right">
            <input type="submit" name="Generate wiki script" ' .
                'value="' . $msgSubmitButton . '" />
        </td>
    </tr> :);

Inside the 3 function i try to call the 2nd function, whereas my server throws me an error "Array to string conversion"

How can I get my array values displayed in a dropdown list? or is there another way to display it in the form?

I would appreciate any help.

Bebus
  • 15
  • 1
  • 11
  • If $msgTabList is an array, then you must create a loop there, to loop through all the elements in that array. For each element you should create a new – Lauri Orgla Sep 11 '14 at 14:50

1 Answers1

1

The "Array to string conversion" error is because you are concatenating an array inside of a string, and the proper way to build a dropdown list is to iterate through your array and build your options, something like:

function showForm() {
  global  $wgScript, $wgOut;
  $msgTabList = $this -> readData();

  $options = "";
  foreach ($msgTabList as $element) {
    $options .= "<option value='$element'>$element</option>";
  }


  $wgOut->addHTML('
  <tr style="margin-top: 2em">
      <td align="right">' . $msgSelTable . '</td>
      <td>  
        <select name="formTables"> ' . $options . '
        </select>
      </td>
  </tr>

  <tr style="margin-top: 2em">
      <td align="right"></td>
      <td style="padding-top: 1em" align="right">
          <input type="submit" name="Generate wiki script" ' .
              'value="' . $msgSubmitButton . '" />
      </td>
  </tr> ');
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • 1
    Thanks a lot for suggested solution. I checked it out, and it works...That is what I wanted! – Bebus Sep 12 '14 at 10:14