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();
}

$mysqli->close();
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); // Outputs 120

// Nested Array Summing Example
$example = array(1, 2, array(10,20,30), 4);

function sum_array($array) {

	$total = 0;
	foreach ($array as $element) {
		if(is_array($element)) {
			$total += sum_array($element);
		} else {
			$total += $element;
		}
	}
	return $total;

}
echo sum_array($example); // Outputs 67
Posted in PHP | 1 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  . ': ' .  $value . '<br />';

}
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 = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);

echo $content; ?>
Posted in Tips & Tricks, Wordpress | 4 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();

// Some basic wordpress template tags
the_title();
the_content();

// Echo the current index position of the loop (0,1,2,3..etc)
echo $wp_query->current_post;

endwhile;?>


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(); ?> from your theme, but this removes comments completely. If you just want to remove/edit the message only where comments have been disabled, just find your comments.php file and scan the code for something that looks similar to this…

	<?php if ( comments_open() ) : ?>
		<!-- If comments are open, but there are no comments. -->

	 <?php else : // comments are closed ?>
		<!-- If comments are closed. -->
		<p class="nocomments">Comments are closed.</p>

	<?php endif; ?>

*Note: its likely your code won’t be exactly the same, because each theme may have the comments template setup a little different. The basic idea is to find the “message” that you want to get rid of. In this example, the line we’re looking for is “<p>Comments are closed.</p>”.

All we have to do now is just delete the following line..

<p class="nocomments">Comments are closed.</p>

And that’s it!

Posted in Tips & Tricks, Wordpress | 2 Comments