More specifically, I'm receiving:
*Notice: Array to string conversion in C:\xampp\htdocs\Project1_Tables\index.php on line 38 Notice: Array to string conversion in C:\xampp\htdocs\Project1_Tables\index.php on line 39*
Here's the code
<?php
//SETUP FOR CONNECTION
$host = "localhost";
$user = "root";
$password = "password";
$database = "test";
$con = mysqli_connect($host, $user, $password, $database);
$tableName = "funding";
$sql = "";
//QUERIES NEEDED TO GATHER DATA FOR DROP-DOWN MENU'S (Research Theme and Institution)
$column = "research_theme";
$query = sprintf("SELECT DISTINCT %s FROM ", $column);
$sql = $query.$tableName;
$ddThemeList = mysqli_query($con, $sql);
$column = "institution";
$query = sprintf("SELECT DISTINCT %s FROM ", $column);
$sql = $query.$tableName;
$ddInstitutionList = mysqli_query($con, $sql);
$list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);
$list2 = mysqli_fetch_array($ddInstitutionList, MYSQLI_NUM);
require_once("support.php");
$body="";
$scriptName = $_SERVER["PHP_SELF"];
$topPart = <<<EOBODY
<form action="$scriptName" method="post">
<p>
Theme
<select name="theme">
<?php if(mysqli_num_rows($list1) != 0) { ?>
<?php while ($list1) { ?>
<option value="<?php echo $list1[0]; ?>">
<?php echo $list1[0]; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</p>
</form>
EOBODY;
$body = $topPart.$body;
$page = generatePage($body);
echo $page;
?>
If I remember correctly, your php instructions are not evaluated in a heredoc statement, but your variables are. Thus your block is just trying to echo "$list1" without the while or if statements that are thought as strings.
With the php statements ignored, the heredoc is just trying to echo "$list1", that is an array and not a string.
Your problem came from , you use an array (list1)
in mysqli_num_rows
instead of a mysqli_result
.
On line 39, you use an array
as a conditional statement in while
instead of a string/int/bool.
<?php
/*
** SOME CODE
**
*/
$list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);
/*
** MORE CODE
**
*/
<select name="theme">
<?php if(mysqli_num_rows($ddThemeList) != 0) { ?>
<?php for($i = 0;$list1[$i]; $i++) { ?>
<option value="<?php echo $list1[$i]; ?>">
<?php echo $list1[$i]; ?>
</option>
<?php } ?>
<?php } ?>
</select>
/*
** MORE CODE
*/
?>
This line is not going to do anything:
<?php while ($list1) { ?>
Something like this is probably more like what you need, though I haven't worked with the mysql or mysqli_ functions in a while:
<?php
//SETUP FOR CONNECTION
$host = "localhost";
$user = "root";
$password = "password";
$database = "test";
$con = mysqli_connect($host, $user, $password, $database);
$tableName = "funding";
$sql = "";
//QUERIES NEEDED TO GATHER DATA FOR DROP-DOWN MENU'S (Research Theme and Institution)
$column = "research_theme";
$query = sprintf("SELECT DISTINCT %s FROM ", $column);
$sql = $query.$tableName;
$ddThemeList = mysqli_query($con, $sql);
$column = "institution";
$query = sprintf("SELECT DISTINCT %s FROM ", $column);
$sql = $query.$tableName;
$ddInstitutionList = mysqli_query($con, $sql);
$list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);
$list2 = mysqli_fetch_array($ddInstitutionList, MYSQLI_NUM);
require_once("support.php");
$body="";
$scriptName = $_SERVER["PHP_SELF"];
$topPart = <<<EOBODY
<form action="$scriptName" method="post">
<p>
Theme
<select name="theme">
EOBODY;
foreach ($list1 as $item) {
$topPart .= "<option value='{$item}'>{$item}</option>";
}
TopPart .= <<<EOBODY
</select>
</p>
</form>
EOBODY;
$body = $topPart.$body;
$page = generatePage($body);
echo $page;