1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to join fields to their array of values?

Discussion in 'PHP' started by gilgalbiblewheel, Feb 5, 2018.

  1. #1
    The first while has all the field/column names and the 2nd while has the values. But how can I join the fields so that they represent to their array of values?
    SEMrush
      $showTables = "SHOW COLUMNS FROM ".$dbTable11;
      if($output = $conn->query($showTables)){
        $fields = array();
        //while($result = mysqli_fetch_assoc($output)):
        while($row = $output->fetch(PDO::FETCH_ASSOC)){
          $fields[] = $row['Field'];
        }
      }
      echo '<pre>';
      print_r($fields);
      echo '</pre>';
     
     
      $sql1 = "SELECT * FROM ".$dbTable11." WHERE book='".$getBook."' AND chapter='".$getC1."'";
      $result1 = $conn->query($sql1);
      while($row = $result1->fetch(PDO::FETCH_ASSOC)) {
        if($row['text_data']!=NULL){
         
          $id[] = $row['id'];
          $book[] = $row['book'];   
          $bookTitle[] = $row['book_title'];
          $recordType[] = $row['recordType'];   
          $chapter[] = $row['chapter'];
          $verse[] = $row['verse'];
          $bookSpoke[] = $row['book_spoke'];
          $chapterSpoke[] = $row['chapter_spoke'];
          $verseSpoke[] = $row['verse_spoke'];
          $textData[] = $row['text_data'];   
          $strongs[] = $row['strongs'];
          $transliteration[] = $row['transliteration'];
          $etymology[] = $row['etymology'];
          $etymStrongs[] = $row['etym_strongs'];
          $etymDesc[] = lcfirst($row['etym_desc']);
          //$hebWord[] = hebrevc($row['heb_word']);
          $hebWord[] = $row['heb_word'];
          $greek_word[] = $row['greek_word'];
          $added[] = $row['added'];
          $also[] = $row['also'];
         
    /*      $fields[0] = $row['id'];
          $fields[1] = $row['book'];   
          $fields[2] = $row['book_title'];
          $fields[3] = $row['recordType'];   
          $fields[4] = $row['chapter'];
          $fields[5] = $row['verse'];
          $fields[6] = $row['book_spoke'];
          $fields[7] = $row['chapter_spoke'];
          $fields[8] = $row['verse_spoke'];
          $fields[9] = $row['text_data'];   
          $fields[10] = $row['strongs'];
          $fields[11] = $row['transliteration'];
          $fields[12] = $row['etymology'];
          $fields[13] = $row['etym_strongs'];
          $fields[14] = lcfirst($row['etym_desc']);
          //$hebWord[] = hebrevc($row['heb_word']);
          $fields[15] = $row['heb_word'];
          $fields[16] = $row['greek_word'];
          $fields[17] = $row['added'];
          $fields[18] = $row['also'];  */   
        }
      }
    
          $fields[0] => $id;
          $fields[1] => $book;   
          $fields[2] => $bookTitle;
          $fields[3] => $recordType;   
          $fields[4] => $chapter;
          $fields[5] => $verse;
          $fields[6] => $bookSpoke;
          $fields[7] => $chapterSpoke;
          $fields[8] => $verseSpoke;
          $fields[9] => $textData;   
          $fields[10] => $strongs;
          $fields[11] => $transliteration;
          $fields[12] => $etymology;
          $fields[13] => $etymStrongs;
          $fields[14] => etymDesc;
          $fields[15] => $hebWord;
          $fields[16] => $greek_word;
          $fields[17] => $added;
          $fields[18] => $also;
         
    
    PHP:
     
    gilgalbiblewheel, Feb 5, 2018 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,108
    Likes Received:
    1,691
    Best Answers:
    239
    Trophy Points:
    515
    #2
    1) You're using PDO, USE IT!!! -- and that means prepare/execute not derping your variables directly into your query string.

    2) WHY are you copying everything around into "variables for nothing" -- particularly blindly copying to an array where the indexes could end up jacked up. To what end is all this "code for nothing" trying to accomplish?!? What's your usage scenario apart from "Let's make it use three times the memory neccessary"?

    3) you could probably save some processing time by adding the null test to the query instead of hard-coding it in the loop.
     
    deathshadow, Feb 6, 2018 IP
  3. voon

    voon Active Member

    Messages:
    23
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    63
    #3
    I think you can get rid of your first While loop. You already have the field/column names associated with the values with this line of code in your second While loop.
    while($row = $result1->fetch(PDO::FETCH_ASSOC)) {  }
    PHP:
    You can display the field/columns names and values like this.
    
    while($row = $result1->fetch(PDO::FETCH_ASSOC))
    {
      foreach($row as $column_name => $column_value)
      {
        echo $column_name . " = " . $column_value . PHP_EOL;
      }
      echo PHP_EOL;
    }
    
    PHP:
    And yes, you can improve your code by what's been suggested by deathshadow.
     
    voon, Feb 6, 2018 IP