Well it has been about six weeks since I have had a chance to work on this. But I am back now!
I have now added “select” => FALSE
to the $fields array, as can be seen in this HTML below. And there is now no error in error_log.
The issue now is that when I hit submit, the form returns to itself.
The search.html.php
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildnotes.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<body background="/artgibney/includes/retro_intro_@2X.png">
<p><a href="..">physCMS home</a> ⇒ <a href="/artgibney/admin/notes/">Manage Daily Notes</a></p>
<?php $fields = array(
1 => array("type" => "absence", "id" => "1", "selected" => FALSE),
2 => array("type" => "late", "id" => "2", "selected" => FALSE),
3 => array("type" => "equip", "id" => "3", "selected" => FALSE),
4 => array("type" => "effort", "id" => "4", "selected" => FALSE),
5 => array("type" => "comment", "id" => "5", "selected" => FALSE),
6 => array("type" => "date", "id" => "6", "selected" => FALSE),
7 => array("type" => "time", "id" => "7", "selected" => FALSE)
); ?>
<form action="?searchview" method="post">
<div>
<label for="student">By student:</label>
<select name="id" id="id">
<?php foreach (array_reverse($learners) as $learner): ?>
<option value="<?php htmlout($learner['id']); ?>"><?php
htmlout($learner['learner']); ?></option>
<?php endforeach; ?>
</select>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
</div>
<fieldset>
<legend>Fields to include:</legend>
<?php foreach($fields as $field):?>
<div><label for="field<?php htmlout($field['id']); ?>">
<input type="checkbox" name="fields[]" id="field<?php htmlout($field['id']); ?>" value="<?php htmlout($field['id']);?>"
<?php
if($field['selected'])
{
echo 'checked';
}
?>><?php htmlout($field['type']); ?>
</label>
</div>
<?php endforeach; ?>
</fieldset>
<div>
<input type="hidden" name="action" value="searchview">
<input type="submit" value="Search">
</div>
</form>
</body>
</html>
Then the index.php controller file is,
if (isset($_GET['search']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
include 'search.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'searchview')
{
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
/* echo "<pre>";
print_r($_POST);
echo "</pre>";*/
/*foreach($_POST['fields'] as $k => $v):
$col[] = $v;
endforeach;
$build = "array(";
foreach($_POST['fields'] as $key => $value):
$build .= "'" . $value . "' => \$row['" . $value . "'], ";
endforeach;
$build = substr($build, 0, -2);
$build .= ")";
echo $build . "<br>";*/
$select = implode(", ", $_POST['fields']);
//echo $select;
try
{
$sql = "SELECT $select FROM notes WHERE
userid = :id";
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching by fields list of notes.';
include 'error.html.php';
exit();
}
$result = $s->fetchAll(PDO::FETCH_ASSOC);
try
{
$sql = "SELECT learner FROM learners WHERE
id = :id";
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching by fields list of notes.';
include 'error.html.php';
exit();
}
$learner = $s->fetchAll();
include 'searchview.html.php';
exit();
}
I think the problem may be in the HTML file. I am not sure if these two lines are correct or compatible.
<form action="?searchview" method="post">
........
<input type="hidden" name="action" value="searchview">
I thought they should direct to the index.php file at
if (isset($_POST['action']) and $_POST['action'] == 'searchview')
{
...
If anyone has any ideas I would be very grateful.
Thanks,
Shane