Hi,
My controller file index.php creates a string called $build which should replace,
$newNotes[] = array('userid' => $row['userid'], 'effort' => $row['effort']);
with
$newNotes[] = $build;
but it seems this array $build is not being read as the function that it is.
Here’s is an excerpt from the controller file index.php
if (isset($_POST['action']) and $_POST['action'] == 'searchview')
{
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
echo "<pre>";
print_r($_POST);
echo "</pre>";
$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();
echo "<pre>";
print_r($result);
echo "</pre>";
foreach ($result as $row)
{
//$newNotes[] = array('userid' => $row['userid'], 'effort' => $row['effort']);
$newNotes[] = $build;
}
include 'searchview.html.php';
exit();
}
This creates two arrays $results and $newNotes.
The array $results produces extra rows which i don’t want and that is why i am try to create the array $newNotes The html file should display $newNotes
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/artgibney/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Topics: Search Results</title>
</head>
<body>
<p><a href="..">physCMS home</a> ⇒ <a href="/artgibney/admin/notes/">Manage Daily Notes</a></p>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
<h1>Search Results</h1>
<?php echo "<pre>";
print_r($result);
echo "</pre>";
echo "<pre>";
print_r($newNotes);
echo "</pre>"; ?>
</body>
</html>
You can see the repeated rows in $result here,
Array
(
[0] => Array
(
[equip] => 0
[0] => 0
[effort] => 1
[1] => 1
[date] => 2014-11-29
[2] => 2014-11-29
)
[1] => Array
(
[equip] => 1
[0] => 1
[effort] => 3
[1] => 3
[date] => 2014-11-30
[2] => 2014-11-30
)
[2] => Array
(
[equip] => 0
[0] => 0
[effort] => 2
[1] => 2
[date] => 2014-11-30
[2] => 2014-11-30
)
[3] => Array
(
[equip] => 1
[0] => 1
[effort] => 0
[1] => 0
[date] => 2014-12-02
[2] => 2014-12-02
)
[4] => Array
(
[equip] => 0
[0] => 0
[effort] => 1
[1] => 1
[date] => 2014-12-02
[2] => 2014-12-02
)
[5] => Array
(
[equip] => 0
[0] => 0
[effort] => 0
[1] => 0
[date] => 2014-12-02
[2] => 2014-12-02
)
[6] => Array
(
[equip] => 1
[0] => 1
[effort] => 1
[1] => 1
[date] => 2014-12-02
[2] => 2014-12-02
)
[7] => Array
(
[equip] => 1
[0] => 1
[effort] => 3
[1] => 3
[date] => 2014-12-04
[2] => 2014-12-04
)
)
and then $newNotes just comes out like this,
Array
(
[0] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[1] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[2] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[3] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[4] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[5] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[6] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
[7] => array('equip' => $row['equip'], 'effort' => $row['effort'], 'date' => $row['date'])
)
Why are there no values when i display $newNotes ?
I am aware that a whitelist needs to be added to this code to sanitize against injections.
Thanks,
Shane