Skip to content
Permalink
Browse files

Read autocomplete values from the database

And add basic header.php and footer.php
  • Loading branch information...
davidnash committed Mar 11, 2019
1 parent b46e183 commit d8e0cffd572667153cb393c12b793830777632fb
Showing with 53 additions and 2 deletions.
  1. +11 −0 footer.php
  2. +11 −0 header.php
  3. +31 −2 index.php
@@ -0,0 +1,11 @@
<footer>
<!-- Quick and dirty spacing -->
<br /><br /><br /><br />
<hr>
<p><small>By David Nash. For the full post, see <a href="https://davidnash.com.au/create-an-autocomplete-field-in-wordpress-2019-update">Create an autocomplete field in WordPress: 2019 Update</a></small></p>
</footer>

<?php wp_footer(); ?>

</body>
</html>
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<title><?php wp_title(); ?></title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body>
<h2>Let's autocomplete!</h2>
@@ -1,8 +1,37 @@
<?php get_header(); ?>

Let's get ready to autocomplete!
<?php
// Show any errors we get from the database. By default WP_DEBUG is set to false and hides these
$wpdb->show_errors();
// Get all post titles from the posts table
$query_string = "
SELECT post_title
FROM $wpdb->posts
WHERE post_status = 'publish'
";
$post_objects = $wpdb->get_results($query_string, OBJECT);
// Output this so we know what to do with the data
echo '<pre style="white-space: pre">get_results() returns: '.print_r($post_objects, true).'</pre>';
// Put that data in a simple array
$post_titles = array();
foreach ( $post_objects as $po ) {
$post_titles[] = $po->post_title;
}
// Output that array so we know it's working
echo '<pre style="white-space: pre">debug: '.print_r($post_titles, true).'</pre>';
// And now put it into a form we can use in the input data-list below
$post_titles_list = implode(', ', $post_titles);
?>

<p>Choose from: <?php echo $post_titles_list; ?></p>

<input class="awesomplete"
data-list="Ada, Java, JavaScript, Brainfudge, LOLCODE, Node.js, Ruby on Rails" />
data-list="<?php echo $post_titles_list; ?>" />

<?php get_footer(); ?>

0 comments on commit d8e0cff

Please sign in to comment.
You can’t perform that action at this time.