Author Archives: thecitizen

MySQLi OOP connection example PHP

$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT * FROM users WHERE user_id=’$userid’"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_assoc()) { echo $row['name'] ."<br />"; } $result->close(); … Continue reading

Posted in PHP | Leave a comment

Recursive Functions in PHP (Simple Examples)

A recursive function is a regular function which calls itself.. // Factorial example (ie 5 * 4 * 3 * 2 * 1) function factorial($n) { if ($n == 1) return 1; return $n * factorial($n-1); } echo factorial(5); // … Continue reading

Posted in PHP | Leave a comment

Switch Statement PHP (Simple Example)

$i = 1; switch($i) { case 0: echo ‘i equals 0′; break; case 1: echo ‘i equals 1′; break; default: echo ‘i equals the default value’; }

Posted in PHP | Leave a comment

Do-While Statement PHP (Simple Example)

$i = 0; do { echo $i++; } while ($i <= 10);

Posted in PHP | Leave a comment

While Statement in PHP (Simple Example)

$i = 0; while ($i <= 10) { echo $i++; }

Posted in PHP | Leave a comment

For Loop vs Foreach Loop in PHP (Simple Example)

// For Loop for ($i = 1; $i <= 10; $i++) { echo $i; } // Foreach Loop $array = array("sample" => "value", "sample2" => "value2", "sample3" => "value3"); foreach ($array as $key => $val) { echo $key  . ‘: … Continue reading

Posted in PHP | Leave a comment

WordPress get_the_content function no paragraph tags

Did you just use the WordPress get_the_content function and realize that all of the line breaks from the visual editor are not being returned as paragraph tags? Solution: Apply the appropriate filters as outlined in the WordPress codex… <?php $content … Continue reading

Posted in Tips & Tricks, Wordpress | 2 Comments

Get current index position in loop in WordPress

Need to get the current index position while iterating through a WordPress loop? Simple… just use $wp_query->current_post Here’s a very stripped down example loop. Note the output starts at 0 (ie 0, 1, 2, 3…) <?php while (have_posts()) : the_post(); … Continue reading

Posted in Tips & Tricks, Wordpress | 1 Comment

Get image path in child theme in WordPress

To get an image path (or any other file for that matter) inside your child theme in WordPress you can use the get_bloginfo function with the “stylesheet_directory” parameter. <img src="<?php echo get_bloginfo(‘stylesheet_directory’); ?>/yourpath/to/an/imagefile.jpg" id="my-new-image" alt="My New Image" />

Posted in Tips & Tricks, Wordpress | Leave a comment

Remove “comments are closed” from a WordPress theme

On a client’s site, needed to remove “comments are closed” from post/pages where comments have been manually disallowed (by the client on the bottom of the edit screen). Most blogs say just to remove the call to <?php comments_template(); ?> … Continue reading

Posted in Tips & Tricks, Wordpress | 2 Comments