Replace line break with
This code is used to replace line break saved in database to
in html display
$astring = str_replace(chr(13) . chr(10), "", $astring);
This code is used to replace line break saved in database to
in html display
$astring = str_replace(chr(13) . chr(10), "", $astring);
If you have to update especially resize and watermark multiple images fast, this tool is useful.
FastStone Image Viewer
The best part is it is free (for personal use)
Sometime you want to automatically do something when people selecting an option from a combo box. Here is the HTML + js code
1 2 3 4 5 6 7 | <form name="form">
<select name="mybox" onchange="window.location.href=this.options[this.selectedIndex].value;">
<option selected>Please Select
<option value="http://www.example.com/">example site
<option value="http://www.google.com/">google site
</select>
</form> |
If want to open it in new window just change the window.location.href to window.open(xxx)
Check box handling is bit different compared to other form objects because it can receive multiple values. Here is the HTML code for the form
1 2 | <input type="checkbox" name="fruit[]" value="a" />Apple<br /> <input type="checkbox" name="fruit[]" value="o" />Orange<br /> |
and PHP code to handle the checkbox values.
1 2 3 4 5 6 7 8 9 | $fruit = $_POST['fruit']; if(!empty($fruit)) { foreach($fruit as $value) { echo($value); echo("<br />"); } } |
I’ve been looking for a simple and clean date picker for few of my applications. I found this website very useful, listing 10 javascript date picker you can choose on.
One of my favorite one is this jQuery date picker
I faced problem uploading a huge sql file to my shared hosting server. The size was not that big. It was only 1.8MB in size with about 30,000 records but phpMyAdmin (provided in cpanel) still failed to fully upload and run the file.
I then googled for a solution.
One of the solutions suggest to use SSH but I’m not familiar with it.
Another solution is by using mysqldumper but the configuration seems quite tedious.
Then I found another solution called BigDump which is very straight forward. You upload a PHP script together with your huge sql file via FTP. Then just run the PHP script as usual. In a second all the data has been restored in your database.
Automate is the key. If you have a dynamic site, you can use this code to post to your twitter account automatically (can use cron job or trigger by certain event). By doing this, you can have your twitter account updated automatically and become your external marketing tool with content rich.
PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php include_once 'twitterlib.php'; $t= new twitter(); $t->username='xxxxxxxx'; $t->password='xxxxxxxxx'; $res = $t->update('testing post via php code'); if($res===false){ echo "ERROR<hr/>"; print_r($t->responseInfo); }else{ echo "SUCCESS.<hr/>Status Posted"; } ?> |
Class: (original source)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | <?php /////////////////////////////////////////// // // twitterPHP // version 0.1 // By David Billingham // david [at] slawcup [dot] com // http://twitter.slawcup.com/twitter.class.phps // // // Example 1: // // $t= new twitter(); // $res = $t->publicTimeline(); // if($res===false){ // echo "ERROR<hr/>"; // print_r($t->responseInfo); // }else{ // echo "SUCCESS<hr/>"; // print_r($res); // } // // // Example 2: // // $t= new twitter(); // $t->username='username'; // $t->password='password'; // $res = $t->update('i am testing twitter.class.php'); // if($res===false){ // echo "ERROR<hr/>"; // print_r($t->responseInfo); // }else{ // echo "SUCCESS<hr/>Status Posted"; // } // // ////////////////////////////////////////// class twitter{ var $username=''; var $password=''; var $user_agent=''; /////////////// // // I don't know if these headers have become standards yet // but I would suggest using them. // more discussion here. // http://tinyurl.com/3xtx66 // /////////////// var $headers=array('X-Twitter-Client: ', 'X-Twitter-Client-Version: ', 'X-Twitter-Client-URL: '); var $responseInfo=array(); function twitter(){} ///////////////////////////////////////// // // Twitter API calls // // $this->update($status) // $this->publicTimeline($sinceid=false) // $this->friendsTimeline($id=false,$since=false) // $this->userTimeline($id=false,$count=20,$since=false) // $this->showStatus($id) // $this->friends($id=false) // $this->followers() // $this->featured() // $this->showUser($id) // $this->directMessages($since=false) // $this->sendDirectMessage($user,$text) // // If SimpleXMLElement exists the results will be returned as a SimpleXMLElement // otherwise the raw XML will be returned for a successful request. If the request // fails a FALSE will be returned. // // ///////////////////////////////////////// // Updates the authenticating user's status. // Requires the status parameter specified below. // // status. (string) Required. The text of your status update. Must not be // more than 160 characters and should not be // more than 140 characters to ensure optimal display. // function update($status){ $request = 'http://twitter.com/statuses/update.xml'; $postargs = 'status='.urlencode($status); return $this->process($request,$postargs); } // Returns the 20 most recent statuses from non-protected users who have // set a custom user icon. Does not require authentication. // // sinceid. (int) Optional. Returns only public statuses with an ID greater // than (that is, more recent than) the specified ID. // function publicTimeline($sinceid=false){ $qs=''; if($sinceid!==false) $qs='?since_id='.intval($sinceid); $request = 'http://twitter.com/statuses/public_timeline.xml'.$qs; return $this->process($request); } // Returns the 20 most recent statuses posted in the last 24 hours from the // authenticating user and that user's friends. It's also possible to request // another user's friends_timeline via the id parameter below. // // id. (string OR int) Optional. Specifies the ID or screen name of the user for whom // to return the friends_timeline. (set to false if you // want to use authenticated user). // since. (HTTP-formatted date) Optional. Narrows the returned results to just those // statuses created after the specified date. // function friendsTimeline($id=false,$since=false){ $qs=''; if($since!==false) $qs='?since='.urlencode($since); if($id===false) $request = 'http://twitter.com/statuses/friends_timeline.xml'.$qs; else $request = 'http://twitter.com/statuses/friends_timeline/'.urlencode($id).'.xml'.$qs; return $this->process($request); } // Returns the 20 most recent statuses posted in the last 24 hours from the // authenticating user. It's also possible to request another user's timeline // via the id parameter below. // // id. (string OR int) Optional. Specifies the ID or screen name of the user for whom // to return the user_timeline. // count. (int) Optional. Specifies the number of statuses to retrieve. May not be // greater than 20 for performance purposes. // since. (HTTP-formatted date) Optional. Narrows the returned results to just those // statuses created after the specified date. // function userTimeline($id=false,$count=20,$since=false){ $qs='?count='.intval($count); if($since!==false) $qs .= '&since='.urlencode($since); if($id===false) $request = 'http://twitter.com/statuses/user_timeline.xml'.$qs; else $request = 'http://twitter.com/statuses/user_timeline/'.urlencode($id).'.xml'.$qs; return $this->process($request); } // Returns a single status, specified by the id parameter below. The status's author // will be returned inline. // // id. (int) Required. Returns status of the specified ID. // function showStatus($id){ $request = 'http://twitter.com/statuses/show/'.intval($id).'.xml'; return $this->process($request); } // Returns the authenticating user's friends, each with current status inline. It's // also possible to request another user's friends list via the id parameter below. // // id. (string OR int) Optional. The ID or screen name of the user for whom to request // a list of friends. // function friends($id=false){ if($id===false) $request = 'http://twitter.com/statuses/friends.xml'; else $request = 'http://twitter.com/statuses/friends/'.urlencode($id).'.xml'; return $this->process($request); } // Returns the authenticating user's followers, each with current status inline. // function followers(){ $request = 'http://twitter.com/statuses/followers.xml'; return $this->process($request); } // Returns a list of the users currently featured on the site with their current statuses inline. function featured(){ $request = 'http://twitter.com/statuses/featured.xml'; return $this->process($request); } // Returns extended information of a given user, specified by ID or screen name as per the required // id parameter below. This information includes design settings, so third party developers can theme // their widgets according to a given user's preferences. // // id. (string OR int) Required. The ID or screen name of a user. // function showUser($id){ $request = 'http://twitter.com/users/show/'.urlencode($id).'.xml'; return $this->process($request); } // Returns a list of the direct messages sent to the authenticating user. // // since. (HTTP-formatted date) Optional. Narrows the resulting list of direct messages to just those // sent after the specified date. // function directMessages($since=false){ $qs=''; if($since!==false) $qs='?since='.urlencode($since); $request = 'http://twitter.com/direct_messages.xml'.$qs; return $this->process($request); } // Sends a new direct message to the specified user from the authenticating user. Requires both the user // and text parameters below. // // user. (string OR int) Required. The ID or screen name of the recipient user. // text. (string) Required. The text of your direct message. Be sure to URL encode as necessary, and keep // it under 140 characters. // function sendDirectMessage($user,$text){ $request = 'http://twitter.com/direct_messages/new.xml'; $postargs = 'user='.urlencode($user).'&text='.urlencode($text); return $this->process($request,$postargs); } // internal function where all the juicy curl fun takes place // this should not be called by anything external unless you are // doing something else completely then knock youself out. function process($url,$postargs=false){ $ch = curl_init($url); if($postargs !== false){ curl_setopt ($ch, CURLOPT_POST, true); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs); } if($this->username !== false && $this->password !== false) curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); $response = curl_exec($ch); $this->responseInfo=curl_getinfo($ch); curl_close($ch); if(intval($this->responseInfo['http_code'])==200){ if(class_exists('SimpleXMLElement')){ $xml = new SimpleXMLElement($response); return $xml; }else{ return $response; } }else{ return false; } } } ?> |
This simple javascript code can be used to prompt user whether they want to proceed with an action or not. Usually be used when user click on delete or remove button/link.
The javascript code
1 2 3 4 5 6 7 8 9 10 11 12 | <script LANGUAGE="JavaScript"> <!-- function confirmSubmit() { var agree=confirm("Are you sure you want to DELETE this record?"); if (agree) return true ; else return false ; } // --> </script> |
The HTML code
1 | <a href="?act=delete&id=1234" onClick="return confirmSubmit()">Delete</a> |
I’ve been looking for PHP code to read RSS feed from blog. It is a simple XML reader code actually and can use a ready class available on the net.
I found this class magpierss which is quite easy to use. You just need to include one file in your code and that one file will call few other library files.
The PHP code you need to use to get the XML data is just like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php define('MAGPIE_DIR', 'rssreader/'); require_once(MAGPIE_DIR.'rss_fetch.inc'); $url = 'http://www.azwan.net/blog/feed/'; if ( $url ) { $rss = fetch_rss( $url ); echo "Channel: " . $rss->channel['title'] . "<p>"; echo "<ul>"; foreach ($rss->items as $item) { $href = $item['link']; $title = $item['title']; echo "<li><a href=$href>$title</a></li>"; } echo "</ul>"; } ?> |
This is a dynamic header if you use include ‘header.php’ in your programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />'; // set title if ($title == '') $title = 'My home page'; // default title $title .= ' - mydomain.com'; // append branding (e.g. your domain name) echo '<title>'.$title.'</title>'; // set description if any if ($description != '') echo '<meta name="description" content="'.$description.'" />'; // set keyword if ($keywords != '') echo '<meta name="keywords" content="'.$keywords.'" />'; // insert css if any if(!empty($css)){ foreach($css as $item){ echo '<link rel="stylesheet" href="'.$item.'" type="text/css" />'; } } // can have static if only one css available echo '<link rel="stylesheet" href="style.css" type="text/css" />'; // insert javascript if any if(!empty($js)){ foreach($js as $item){ echo '<script language="javascript" src="'.$item.'"></script>'; } } echo '</head>'; |
From the file you include, you can set something like this
1 2 3 4 5 6 7 8 9 | //set variables $title = 'My home page'; $description = ''; $keyword = ''; $js = array('formvalidation.js','datecalc.js'); //include javascript $css = array('style.css','form.css') // include css //include header include 'header.php'; |