2016-11-26 43 views
0

我搞砸了一點與我的PHP,只是想得到一些幫助。此刻,我的代碼將從下拉框中選擇的值,並將這些值與其相應的行一起放入表格中,然後放入3D數組中。但是,它只需要是2D數組,但我不知道如何擺脫3D數組的第一維。 PHP代碼:爲什麼我的陣列是3D而不是2D?

<?php 
function startmatching(){ 
    define("DB_DSN", "mysql:host=localhost;dbname=FoodMatching"); 
    define("DB_USERNAME", "root"); 
    define("DB_PASSWORD", ""); 

    // define the empty array to be filled from db 

    $results = array(); 
    // any other php tasks that dont needthe ingcats 
    // store sql 

    $IngName = $_POST['IngredientName']; 
    $IngCounter=0; 
    while($IngCounter<count($IngName)){ 
     $sSQL = "SELECT IngID, IngName, Texture, Colour, Bitter, Sweet, Sour, Salty, Umami FROM IngredientCharacteristics WHERE IngName='$IngName[$IngCounter]'"; 
     // create an instance of the connection 
     $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
     // prepare 
     $st = $conn->prepare($sSQL); 
     // execute the connection 
     $st->execute(); 
     $counter=0; 
     // while myslq has rows loop over them and store 
     while($row = $st->fetch()){ 
      $results[$counter][] = $row; 
      $counter++; 
     } 
     $IngCounter++; 
    } 
    print_r($results); 
} 
?> 

HTML:

<form method='post' role="form" autocomplete="off"> 

    <div class="entry input-group col-xs-3"> 
     <?php //this function takes the ing id and ingname from ingredientcharacteristics table. 
        // if there are results stored create the select and loop over 
        if(!empty($aIngList)){ 
         echo "<span class='form-control' style = 'float:left; font-size: 20px;font-family:'Helvetica Neue' ><select name = 'IngredientName[]' class = 'custom-dropdown__select custom-dropdown__select--white'>"; 
         //class = 'custom-dropdown custom-dropdown--white' 
         echo "<option value='' default >Choose an Ingredient</option>"; 
         foreach ($aIngList as $iIngID => $sIngName) { 
          echo "<option value='".$sIngName."' >".$sIngName."</option>"; 
         } 
         echo "</select></span>"; 
        }else{ 
         echo "<p>No results available!</p>"; 
        } 
       ?> 

     <span class="input-group-btn"> 
      <button class="btn btn-success btn-add" type="button"> 
       <span class="glyphicon glyphicon-plus"></span> 
      </button> 
      <button type="submit" class="hidden" name="startmatch" value ="startmatch" id="submit-form"></button> 
     </span> 
    </div> 


</form> 

1.4.3 $結果給出如下:

Array 

    (
     [0] =&gt; Array 
      (
       [0] =&gt; Array 
        (
         [IngID] =&gt; 2 
         [0] =&gt; 2 
         [IngName] =&gt; Apples Fresh 
         [1] =&gt; Apples Fresh 
         [Texture] =&gt; 4 
         [2] =&gt; 4 
         [Colour] =&gt; 8 
         [3] =&gt; 8 
         [Bitter] =&gt; 7 
         [4] =&gt; 7 
         [Sweet] =&gt; 3 
         [5] =&gt; 3 
         [Sour] =&gt; 4 
         [6] =&gt; 4 
         [Salty] =&gt; 8 
         [7] =&gt; 8 
         [Umami] =&gt; 9 
         [8] =&gt; 9 
        ) 

       [1] =&gt; Array 
        (
         [IngID] =&gt; 2 
         [0] =&gt; 2 
         [IngName] =&gt; Apples Fresh 
         [1] =&gt; Apples Fresh 
         [Texture] =&gt; 4 
         [2] =&gt; 4 
         [Colour] =&gt; 8 
         [3] =&gt; 8 
         [Bitter] =&gt; 7 
         [4] =&gt; 7 
         [Sweet] =&gt; 3 
         [5] =&gt; 3 
         [Sour] =&gt; 4 
         [6] =&gt; 4 
         [Salty] =&gt; 8 
         [7] =&gt; 8 
         [Umami] =&gt; 9 
         [8] =&gt; 9 
        ) 

      ) 

    ) 

我不知道如何擺脫多餘的陣列,因此任何幫助將不勝感激:)

+0

此行'$ results [$ counter] [] = $ row;' –

+0

您的腳本存在[SQL注入攻擊]的風險(http://stackoverflow.com/questions/60174/how-cani-i-預防SQL注入在PHP) 看看發生了什麼[小Bobby表](http://bobby-tables.com/)即使 [如果你逃避投入,它不安全!]( http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) 使用[prepared參數化語句](http://php.net/manual/en/ mysqli.quickstart.prepared-statements.php) – RiggsFolly

+0

'$ results [$ counter] [] = $ row;'應該是'$ results [] = $ row;'$ counter'變量是完全不必要的 – RiggsFolly

回答

0

問題是這條線:

$results[$counter][] = $row; 

的解決方案是將其更改爲

$results[] = $row; 

PHP會自動追加到$row意味着$results,你不需要你$counter可變的。