Monday, December 6, 2010
MySQL generate random number between range
UPDATE table
SET column = RPAD(CONCAT(7, ROUND(RAND() * 1000)), 4, 0)
This generates a number between 7,000 and 8,000 for example...
Friday, July 17, 2009
For those of you people who use flash intros...
Wednesday, June 10, 2009
Add auto_increment and primary key after table is created and already has rows
set @counter = 0;
update table
set id = (select @counter := @counter + 1 from dual);
This will allow you to replicate the behavior of auto_increment after the fact.
Monday, November 10, 2008
Internet sharing from a Mac to a Windows computer
The solution was found here: http://joerg.li/hobby/macmini.html
but I'll summarize the important part, the missing link if you will.
In the Password field you can enter three different formats:
- an arbitrary string, e.g. please let me in
- double-quotes + 5 or 13 characters + double-quotes, e.g. "apple"
- dollar-sign + 10 or 26 hex digits, e.g. $5544332211
Friday, August 1, 2008
Blackout background javascript login form example
{
var login_width = 400;
var login_height = 250;
var body = document.getElementsByTagName("body")[0];
login_new_div = $('sajax_login_form');
if(!login_new_div) {
login_new_div = document.createElement('div');
body.appendChild(login_new_div);
login_new_div.id = 'sajax_login_form';
login_new_div.style.backgroundColor='#ffffff';
login_new_div.style.border='1px solid #800000';
login_new_div.style.position='absolute';
login_new_div.style.width = login_width + 'px';
login_new_div.style.height = login_height + 'px';
login_new_div.style.zIndex='100';
login_new_div.style.MozBorderRadius='10';
login_new_div.style.margin='auto';
login_new_div.style.left = (document.body.offsetWidth / 2) - (login_width / 2) + 'px';
login_new_div.style.right = (document.body.offsetWidth / 2) + (login_width / 2) + 'px';
login_new_div.style.top = (document.body.offsetHeight / 2) - (login_height / 2) + 'px';
login_new_div.style.bottom = (document.body.offsetHeight / 2) + (login_height / 2) + 'px';
}
login_new_div = $('sajax_login_form');
if(!login_new_div) {
blackout_new_div = document.createElement('div');
body.appendChild(blackout_new_div);
blackout_new_div.id = 'sajax_blackout';
blackout_new_div.style.position='absolute';
blackout_new_div.style.display='none';
blackout_new_div.style.left='0px';
blackout_new_div.style.top='0px';
blackout_new_div.style.backgroundColor='#555555';
blackout_new_div.style.opacity='.9';
blackout_new_div.style.filter='alpha(opacity=90)';
blackout_new_div.style.zIndex='50';
blackout_new_div.style.width='100%';
blackout_new_div.style.height=(document.body.offsetHeight < screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px';
blackout_new_div.style.height='100%';
login_new_div.innerHTML = 'Close';
// and finally display the divs
login_new_div.style.display='block';
blackout_new_div.style.display='block';
}
function hide_login_form()
{
var blackout = $('sajax_blackout');
var login = $('sajax_login_form');
login.style.display='none';
blackout.style.display='none';
}
Monday, April 21, 2008
Dynamic Javascript Rows Example
Through blood, sweat, tears and caffeine I was able to find a half-assed solution, make it better and slap my own logo on it. Here it is for all the world to see, use, abuse, then re-use.
Or, if you want to see it in action, then head on over to my rss dumpster.
<title>Dynamic rows in javascript (client side only).</title>
<script type='text/javascript'>//<![CDATA[
// standard prototype function, you shouldn't need this if you are using prototype.js, but adding it in here for example purposes.
function $()
{
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string') {
element = document.getElementById(element);
}
if (arguments.length == 1) {
return element;
}
elements.push(element);
}
return elements;
}
function add_row(event, row)
{
row = row || this;
var row_number = row.rowIndex + 1;
var table = row.parentNode.parentNode;
var newRow = table.insertRow(row_number);
var cell = newRow.insertCell(0);
cell.colSpan = row.cells.length;
// __data__ is a prefix I use to reference an id to keep things unique
cell.innerHTML = $('__data__' + row.id).innerHTML;
row.onclick = remove_row;
row.default_onmouseout = row.onmouseout;
row.onmouseout = null;
// highlighting of course optional, but gives a better feel to the UI
HighLight(row, true);
}
function remove_row(event, row)
{
row = row || this;
var row_number = row.rowIndex + 1;
var table = row.parentNode.parentNode;
var newRow = table.deleteRow(row_number);
row.onclick = add_row;
row.onmouseout = row.default_onmouseout;
// highlighting of course optional, but gives a better feel to the UI
unHighLight(row);
}
function HighLight(row)
{
if(row.className != 'highlighted') {
row.original_class = row.className;
}
row.className = 'highlighted';
row.style.cursor = 'pointer';
}
function unHighLight(row)
{
row.className = row.original_class;
}
//]]></script>
<style type='text/css'>
.highlighted {
background-color: #ccddff;
}
td {
font-size: 11px;
border: 1px solid gray;
padding: 5px;
}
th {
border: 1px solid black;
font-size: 11px;
color: white;
background-color: black;
padding: 5px;
}
table {
width: 100%;
border-collapse: collapse;
padding: 5px;
}
body {
font-family: "Lucida Grande",Geneva,Arial,Verdana,sans-serif;
}
.white_link {
color: #ffffff;
}
a {
color:blue;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
hr {
border-top: 2px dashed #000000;
border-bottom: 0px solid #ffffff;
color: #ffffff;
background-color: #ffffff;
height: 1px;
width: 100%;
}
pre {
font-family: "andale mono", monaco, courier, "courier new", monospace;
font-size: 9pt;
display: block;
margin: 1em;
border: 1px solid #999;
padding: 1em;
background: #ffc;
color: #000;
}
</style>
<table style='width: 12cm;'>
<tr>
<th>Item</th>
</tr>
<tr
id='1'
onclick="javascript:add_row(null, this);"
class='odd'
onmouseover='HighLight(this)'
onmouseout='unHighLight(this)'>
<td>database</td>
</tr>
<tr
id='2'
onclick="javascript:add_row(null, this);"
class='odd'
onmouseover='HighLight(this)'
onmouseout='unHighLight(this)'>
<td>katz</td>
</tr>
<tr
id='3'
onclick="javascript:add_row(null, this);"
class='odd'
onmouseover='HighLight(this)'
onmouseout='unHighLight(this)'>
<td>etc</td>
</tr>
</table>
<div style='display: none;' id='__data__1'><pre>Some awesome data for row 1!</pre></div>
<div style='display: none;' id='__data__2'><pre>All your base are belong to us.</pre></div>
<div style='display: none;' id='__data__3'><pre>920c1e9267f923c62b55a471c1d8a528</pre></div>
Friday, March 21, 2008
The worst block of PHP code.... ever.
This company just plain sucks.
Their hiring processing must involve questions like: "Have you heard of a computer before?", "Do you know how to spell your own name?"
Ok, I may be exaggerating, a _little_ bit... but you get the point; they are really really crappy.
So they have this software that we have to integrate with which is insanely bad in so many ways that I just can't go into without plain out giving away who they are. HOWEVER, I can show you a somewhat anonymous block of code which highlights the level of skill these "developers" employ.
So here you have it; the worst block of PHP code every written (for a production system that runs thousands of websites)
for ( ; $row = mysql_fetch_row($si); $string = ''):
foreach ($row as $k => $v):
if ($row[0] != $previous[0])
unset($previous);
if ($v && $k && $k < 9 && $previous[$k+4] != $row[$k+4]):
for ($x=0; $x < $k; $x++)
$string = sprintf("%s ", $string);
$string = sprintf("%s%s
", $string, str_func($v));
endif;
endforeach;
unset($previous);
$previous = $row;
if ($row[0] == SOME_CONSTANT)
if ($row[1] != $param):
$anon_str .= ($anon_str ? $sep_str : '') . $string;
$param = $row[1];
else:
$anon_str .= $string;
endif;
else
if ($row[1] != $param):
$cat_str .= ($cat_str ? $sep_str : '') . $string;
$param = $row[1];
else:
$cat_str .= $string;
endif;
endfor;
Saturday, November 10, 2007
A couple cool things Volume I
For loops are dead in PHP, let me show you why you will almost never need a for loop again.
for($i = 0; $i <= 10; $i++) {
echo $i . ' ';
}
vs
foreach(range(1,10) as $i) {
echo $i . ' ';
}
The for loop is ugly and convoluted, and while the foreach with a range probably uses more overhead its usually not a big deal in PHP as app processor usage is usually least of your concerns. We need to move on from C people c'mon...
If you didn't notice, I didn't close the PHP (?>). That's because you don't need to! There is absolutely no reason to close it, in fact, only bad things will happen if you close it, such as accidental whitespaces (which causes weird session errors). PHP will find the EOF and stop there, it won't cause any slowdown, and has no cons.
Another thing I noticed, is a lot of sites out there don't realize how cool the label tag in XHTML/HTML is. By simply adding a tag around an input type of radio or checkbox, it allows you to select the input by clicking the text within the label tag.
Let me give you an example.
Crappy site:
<input name="name" value="123" type="checkbox"> Check
Awesome site:
<label>
<input name="name" value="123" type="checkbox"> Check
</label>
Now, see it in action for yourself....
Crappy site:
Check
Awesome site:
They are very similar, except that on the awesome site, you can actually click the text "click here" and it will select the box, or obviously you can check the box the "old-fashioned" way.
Thats all for now, more cool code-tidbits to come.
Tuesday, April 24, 2007
What is the world coming to?
I check my online banking again later in the day after I had went to the bank to change my pin, and notice another $500 missing from my account, ATM withdrawl, this time in Berkeley. Naturally, my mood shifts from phew to homicidal rage. So once again I'm on the line with BofA and I basically say "wtf", my account is supposed to be on hold, thus you cannot withdraw money? right? isnt that what a hold is?
Apparently, my card wasn't being used, but my Dads card was, and since our accounts are linked this person withdrew money from my account, not knowing the difference. The first Bank of America agent I spoke to NEVER at any point in our conversation mentioned that it wasn't even my card being used, holy crap that seems like a pretty vital piece of information, yet the 3rd person I talked to says it clearly stated that my card wasn't being used. WOW.
So roughly 2 nanoseconds later I am speed dialing my Pops to put a hold on his account, because apparently, putting a hold on my account doesn't prevent HIM with from taking money from my account, it only prevents ME from taking money from my account, and the person willingly put the hold on my account KNOWING that his card was being used and that someone would still be able to withdraw money from my account. OMG!!
In a rage, I'm on the phone with BofA for a final time explaining the situation to them, only to find out that I'm a moron for thinking a hold on my account protects me from others withdrawing from it, and I get a bunch of attitude from the rep because I'm a little bit frustrated that yet another withdrawl was made. I am in disbelief that I trust my money to people so incompetent.
bg Bank of America
Tuesday, April 17, 2007
Parsing Digg's RSS Feeds
So after an hour or so of googling and finding nothing because I had nowhere to start, I decided to pull up telnet and give it a shot the old fashioned way :)
telnet digg.com 80;
get / http/1.1 \n\n
Nada!!
so... ok...
telnet digg.com 80;
Host: Digg.com
get / http/1.1 \n\n
Still nothing!
Finally, I try
telnet digg.com 80;
Host: whatever.com
Referer: something.net
User-agent: some browser
get / http/1.1 \n\n
Zoip!
Wala, one of these is the magic one, through process of elimination, I find the are requiring a referrer. Weird... so... how do you fix this in PHP you ask? Simple...
ini_set('user_agent', 'Anything here');
Well, there you have it, your one line fix for parsing Digg.
Monday, April 16, 2007
Putting down the wrench
Whats important is that now I will have more time to work on things like random projects (long, evil laugh...) =P and do things like writing blogs, woohoo.
Thursday, March 29, 2007
Anti-Windows Rant #1
I hate to be a complainer, but I just have to let people in on what I had to deal with this past weekend, I wasted yet another weekend of my life because engineers at Microsoft didn’t take the time to do a very few simple things that would have made my life easier.
My cousin calls me friday afternoon, to meet up… we hang out yada yada, then we get back home and he asks me to take a look at his laptop… its running slow (big shocker…), I fire up the laptop only to see 14 icons in the taskbar with basically every POS software that can come with your computer and possibly some spyware in there too… Naturally, I disable all his startup stuff, reboot and bam, system registry hive is corruped… after banging my head on the table for an hour in regret of agreeing to work on a windows machine, I try recovery console… I get prompted for a login, crap… reset the password… the new password doesnt work, can’t recover the computer because i can’t log in… okay.. reinstall xp home, his key “works” but when it starts up it forces activation without the nifty 30 day trial period… I go to activate, the screen is blank, activation mysteriously just doesn’t work, no reason why, just fails… Ok, great, can’t change the key because i can’t login, so i have to reinstall with another key, this key also “works” but now I find that key has reached its maximum activation limit.
Wonderful, so I proceed to very annoyed for another hour while I install XP Pro Corporate with my work license just so I can get the crap off of the computer, great everything works, no activation required… am I the only person who find it strange that a company who makes money because of piracy is working so hard to make sure I can’t use this computer…weird… anyway on to Sub-rant about Toshiba’s Support…
So now I go to the Toshiba support website, downloaded the drivers for the laptop, and only 1 of the drivers actually works. You have to understand this is after 5 hours of trying to simply get data off of the laptop, I am practically at my limit… So I have to go and download drivers from the manufacturer (1 of which also didn’t work) only to have a different registry hive corrupted 3 days later causing my cousin to lose more data. One would assume the hard drive is failing, but it showed no signs of it other than random things dying, and it passed all the tests I ran on it…
The lesson learned here is not how crappy Windows or Microsoft is, but moreso that I’ve wasted so many hours of my life dealing with these types of problems, so many precious hours of my life have been spent dealing with issues that shouldnt need to be dealt with. From now on I will go out of my way to aviod dealing with any Microsoft products, be it a mouse, and O/S or really-crappy-defective-non-standard-insecure browser
Sunday, March 4, 2007
Actual web designing?
In other news, I have officially showed two others the light into the world of Apple. My friend Jason decided to get a Macbook, and my Pops jumped on the bandwagon with an iMac, wise choices guys.
