Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to do a dynamic csv export based on data posted from a previous page.

My query page looks like this:

    <?php
    $hssql = "SELECT `COLUMN_NAME` as hscolname FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='thisone' AND `TABLE_NAME`='hs'";

    echo '
  <script> function moveData(clicked) {
  var txt=document.getElementById("result").value;
  txt=txt + clicked + "|";
  document.getElementById("result").value=txt;
  }
  </script>

  <select size=10 multiple name="hs[]" ondblclick="moveData(this.value)">
  ';
     $hsresult = $mysqli->query($hssql);
     while ($hsrow = $hsresult->fetch_assoc()) {
      echo '<option value="'.$hsrow["hscolname"].'">'.$hsrow["hscolname"].'</option>';
     }
     echo '
        </select>
       <textarea name="result" id="result" rows="4" cols="95"></textarea>';

There are about 6 of these select boxes which correspond to different tables. So this basically gives me a | delimited list of field names that I want to select from. All of the field names have a unique start to tell what table it comes from. Eg: college table all fields start with coll (collname, collid, colladdress) hs table all fields start with hs. $_POST["result"] = "|collcity|collzip|collstate|colladdress|collname|newsname|newsid|newscity|newsstate|newszip|hsid|hsname|hsaddress|hscity|hsstate|hszip ";

So now I want to take this | delimited variable $_POST["result"] and do a dynamic select statement.

    $reqfields ="";        
    $fieldarray = preg_split("/\|/", $_POST["result"]);
     foreach( $fieldarray as $field) {
      $reqfields .= "$field,";
     }

     $tables = array();
     if (preg_match("/\|coll/",$_POST["result"])) { array_push($tables,"college"); }
     if (preg_match("/\|hs/",$_POST["result"])) { array_push($tables,"hs"); }
     if (preg_match("/\|news/",$_POST["result"])) { array_push($tables,"newspaper"); }



     $sql = "SELECT ".$reqfields." FROM ".$tables[0]." LEFT JOIN ".$tables[1]." ON ...

OK.. so here is my impasse. how do I determine what to join them on... should I put an array of possible joins... and go from there? person.collid = college.collid, person.hsid = hs.hsid, but then.. what if they select nothing from the person table....

A little guidance on a better way to do this would be welcome.

share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.