1

I have a foreach loop to display data from a query, when the query does not return any results - which will be the case sometimes I want it to not display any data but I get the following error:

Warning: Invalid argument supplied for foreach() in /home/u913241783/public_html/editmeeting.php on line 29

The segment to display the data is:

<?php
    foreach($activities as $k=>$v) {
?>
<tr class="table-row">
    <td><?php echo $k+1; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'title', 'activities', 'activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["title"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'description','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["description"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'leaders','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["leaders"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'time','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["time"]; ?></td>
</tr>
<?php
    }

EDIT:

Full code is:

<?require_once("dbcontroller.php");
$db_handle = new DBController(); ?>
<h1 class="regular brown bottom_line">Activity Test</h1>
<div class="clear"></div>
</header>

<?
include'connect_db.php';
//get variables
$input1 = $_SESSION[ 'unitid' ];
$id=($_GET['id']);
$activities=array();

        $sql = "SELECT activityid, activitynumber, title, description, time, leaders FROM activities WHERE meetingid='$id'";
        $activities = $db_handle->runQuery($sql);?>
       <table class="tbl-qa">
          <thead>
              <tr>
                <th class="table-header" width="10%">Activity No.</th>
                <th class="table-header">Title</th>
                <th class="table-header">Description</th>
                <th class="table-header">Leaders</th>
                <th class="table-header">Time</th>

              </tr>
          </thead>
          <tbody>
          <?php
          print_r($activities);
          foreach($activities as $k=>$v) {
          ?>
              <tr class="table-row">
                <td><?php echo $k+1; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'title', 'activities', 'activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["title"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'description','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["description"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'leaders','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["leaders"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'time','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["time"]; ?></td>
              </tr>
        <?php
        }
        ?>
          </tbody>
        </table>
    </body>
</html>

dbcontroller.php

<?php
class DBController {
    private $host = "***********";
    private $user = "***********";
    private $password = "***************";
    private $database = "***********";



    function __construct() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database) OR die ( mysqli_connect_error() );
    }

    function runQuery($query) {
        $conn = mysqli_connect($this->host,$this->user,$this->password);
        mysqli_select_db($conn, $this->database);
        $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($conn, $query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
}
?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Laura Morris
  • 222
  • 1
  • 10

1 Answers1

1

The problem is with your method runQuery, because it doesn't return anything if the results are empty. Simply add an else case where you just return an empty array, so that the foreach loop doesn't run e.g.

function runQuery($query) {
    $conn = mysqli_connect($this->host,$this->user,$this->password);
    mysqli_select_db($conn, $this->database);
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
    else
        return array();
}

Also I would recommend you to save your connection into a property and then use the property as connection variable, e.g.

function __construct() {
    $this->conn = mysqli_connect($this->host,$this->user,$this->password,$this->database) OR die ( mysqli_connect_error() );
  //^^^^^^^^^^^
}   

Then you can use it in other methods with $this->conn.

You also might want to take a look into mysqli_* prepared statements, it's much safer.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Please explain the downVote, so I can improve my answer! – Rizier123 May 03 '15 at 21:27
  • I'm trying to upvote it but it keeps saying -1 when I press the up arrow. Thank you for all your help - I'll look into the things you have suggested about improving my connection methods too. – Laura Morris May 03 '15 at 21:31
  • @LauraMorris See: http://stackoverflow.com/help/privileges/vote-up This might help – Rizier123 May 03 '15 at 21:32
  • No, just return the `$result`. Set the fetchmode to assoc. It's traversable, you can directly foreach it. If it's empty, there will be no loops. It's false on failure and true on queries that just return success (e.g. insert operations). Does greatly reduce complexity and error situations, also saves memory. – hakre May 04 '15 at 19:44
  • @hakre That's correct, but I didn't wanted to change to much from OP's code, so I just added the else statement, which would resolves in: `return $resultset` without any conditions – Rizier123 May 04 '15 at 20:19
  • If so, all was missing was the initialization of the `$resultset` variable to an empty array before processing the `$result` ;) But like with the first comment I left, it's just FYI. – hakre May 04 '15 at 21:01