I am try to build with recursive function that show option menu categories doing indent. It is working perfect in mysqli query, but how can i do that in PDO query?
For any helps thanks.
I am changed some lines and i also changed correct database connection to PDO, but not worked.
changed lines:
$dbc = $db->prepare("SELECT * FROM categories ORDER BY title");
$dbc->execute(array());
while (list($id, $parent_id, $category) = $dbc->fetchAll(PDO::FETCH_ASSOC)) {
My mysqli query need change to PDO:
$db = mysqli_connect("localhost","","","");
echo '<select name="parent_id">
<option value="">Select</option>';
function make_list ($parent,$depth) {
global $option;
foreach ($parent as $id => $cat) {
$whitespace = str_repeat(' - ', $depth * 1);
echo '<option value="' . $cat['id'] . '">'. $whitespace . $cat['category'] . '</option>';
if (isset($option[$id])) {
make_list($option[$id], $depth+1);
}
}
}
$dbc = mysqli_query($db, "SELECT * FROM categories ORDER BY title");
$option = array();
while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
$option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);
}
make_list($option[0], $depth = 0);
echo '</select>';
Here error messages:
Line 36 : foreach ($parent as $id => $cat) {
Line 56: while (list($id, $parent_id, $category) = $dbc->fetchAll(PDO::FETCH_ASSOC)) {
Line 58: $option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);
Line 61: make_list($option[0], $depth = 0);
<select name="parent_id">
<option value="">Select</option><br />
<b>Warning</b>: Illegal offset type in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>58</b><br />
<br />
<b>Notice</b>: Undefined offset: 0 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>: Undefined offset: 1 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>: Undefined offset: 2 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>: Undefined offset: 0 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>61</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>36</b><br />
</select>