-2

here my code in class.php:

 public function select(){
        $stmt = $this->conn->prepare("SELECT country FROM `country`") or die($this->conn->error);
        if($stmt->execute()){
            $result = $stmt->get_result();
            return $result;
        }
    }


public function read(){
        $stmt = $this->conn->prepare("SELECT segment FROM `segments`") or die($this->conn->error);
        if($stmt->execute()){
            $result = $stmt->get_result();
            return $result;
        }
    }

and now in index.php i did this:

                            <th class="text-center">country</th>

                            <th class="text-center">segments</th>
            </thead>
            <tbody>
            <?php

                require 'class.php';

                $conn = new db_class();
                $read = $conn->select();
                                    $test = $conn->read();

                while($fetch = $read->fetch_array(MYSQLI_ASSOC)&& $fetch1 = $test->fetch_array(MYSQLI_ASSOC)){ 
                                        foreach ($fetch1 as $field => $values) {


                                      foreach($fetch as $field=>$value){
   echo '<tr><td>' . $value . '</td>'
           . '<td>'. $values .'</td>';
}

                                        }
}       


            ?>




            </tbody>
        </table>

im just trying to fetch data from 2 tables and put them in one html table

i get this error for 6 times:

Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\segments\countrySegment.php on line 59

im just trying to fetch data from 2 tables and put them in one html table and if any one know how, i want for every country a drop down menu with segment values inside any idea? thank u in advance

  • can you print `$fetch` && `$fetch1` in the loop? – Agam Banga May 09 '17 at 13:31
  • it came with this error :Array to string conversion @AgamBanga – eddy abikhalil May 09 '17 at 13:33
  • do `print_r($fetch)` && `print_r($fetch1)` instead of `echo` – Agam Banga May 09 '17 at 13:33
  • You'd have to `print_r()` in order for it to print – Raphael Cunha May 09 '17 at 13:34
  • Are the 2 tables you're selecting from related? Like do they have a common column? – Raphael Cunha May 09 '17 at 13:34
  • same error as before but this what it shows: Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\segments\countrySegment.php on line 61 Array ( [segment] => food ) 1 @AgamBanga – eddy abikhalil May 09 '17 at 13:36
  • yes i have created another table and i joined then using this query: select * from countrysegments inner join country on countrysegments.country_id = country.id inner join segments on countrysegments.segment_id = segments.id @RBCunhaDesign – eddy abikhalil May 09 '17 at 13:37
  • it means you are getting array in `$fetch` but only `1` in `$fetch1` as per output from print. Check the query – Agam Banga May 09 '17 at 13:38
  • 2
    You have too many questions with none accepted and all within 24 hours. If you wish to remain in good standings, you should mark the ones that provided you with solutions as solved. *Be a good neighbour* ;-) – Funk Forty Niner May 09 '17 at 13:41
  • 1
    Seeing your question history; it appears that you are basically asking the same question over and over again, but using code borrowed from other answers. That isn't how things work on Stack and you could get flagged for moderation. @eddyabikhalil – Funk Forty Niner May 09 '17 at 13:53

1 Answers1

1

This because of array pointer issue

foreach ($fetch1 as $field => $values) {
   foreach($fetch as $field=>$value){
      echo '<tr><td>' . $value . '</td>'
           . '<td>'. $values .'</td>';
   }
}

first time second foreach prints everything next time it has no value it points beyond the value..

so you need to reset the array using reset

foreach ($fetch1 as $field => $values) {
   reset($fetch)
   foreach($fetch as $field=>$value){
      echo '<tr><td>' . $value . '</td>'
           . '<td>'. $values .'</td>';
   }
}

or you can save it a temporary variable

foreach ($fetch1 as $field => $values) {
   $fetchtmp = $fetch;
   foreach($fetchtmp as $field=>$value){
      echo '<tr><td>' . $value . '</td>'
           . '<td>'. $values .'</td>';
   }
}
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77