Thursday, July 06, 2006

switch order of two records in a table

When in need to change the order of a list use a field to store the order as an int:
Change the order of the records by switching the order value between consecutive elements.

case 'orderdown':
case 'orderup':{
//get all list and retain two elements: if moveup operation then I keep prev and current
//otherwise I keep current and next
$rec_list = $this->getChildren($record->id_parent);//all records on this level
$switch = array();
for($i = 0; $i < count($rec_list) ; $i++){
if($rec_list[$i]['id'] == $record['id']){
if('orderup' == $what && $i != 0){
//not first
$switch[$rec_list[$i-1]['id']] = $rec_list[$i]['corder'];
$switch[$rec_list[$i]['id']] = $rec_list[$i-1]['corder'];
break;
}elseif('orderdown' == $what && $i != (count($rec_list)-1)){
//not last
$switch[$rec_list[$i+1]['id']] = $rec_list[$i]['corder'];
$switch[$rec_list[$i]['id']] = $rec_list[$i+1]['corder'];
break;
}
}
}

//valid elements found. Switch them
if(count($switch) > 0){
foreach ($switch as $id=>$corder) {
$sql = "UPDATE page SET corder=$corder WHERE id=$id";
$result = mysql_query($sql) or die("SQL ERROR " . mysql_error() . " [$sql] on " . __FILE__ . " at line " . __LINE__);
}
}

Monday, July 03, 2006

Instant popup on mouseover


<STYLE TYPE="text/css">
<!--
#dek {POSITION:absolute;VISIBILITY:hidden;Z-INDEX:200;}
//-->
</STYLE>
<DIV ID="dek"></DIV>

<SCRIPT TYPE="text/javascript">
<!--

Xoffset=0; // modify these values to ...
Yoffset= 20; // change the popup position.

var old,skn,iex=(document.all),yyy=-1000;

var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all

if (ns4)
skn=document.dek
else if (ns6)
skn=document.getElementById("dek").style
else if (ie4)
skn=document.all.dek.style
if(ns4)document.captureEvents(Event.MOUSEMOVE);
else{
skn.visibility="visible"
skn.display="none"
}
document.onmousemove=get_mouse;

function popup(msg,bak){
var content="<table><tr><td";

content += " bgcolor='"+bak+"'";

content += ">"+msg+"</td></tr></table>";

yyy=Yoffset;
if(ns4){skn.document.write(content);skn.document.close();skn.visibility="visible"}
if(ns6){document.getElementById("dek").innerHTML=content;skn.display=''}
if(ie4){document.all("dek").innerHTML=content;skn.display=''}
}

function get_mouse(e){
if(ns4||ns6){
skn.left=e.pageX+Xoffset+'px';
skn.top=e.pageY+yyy + 'px';
}else{
if (document.documentElement){
// IE6 +4.01
skn.left=event.x+document.documentElement.scrollLeft+Xoffset+'px';
skn.top=event.y+document.documentElement.scrollTop+yyy + 'px';
}else if (document.body){
// IE5 or DTD 3.2
skn.left=event.x+document.body.scrollLeft+Xoffset+'px';
skn.top=event.y+document.body.scrollTop+yyy + 'px';
}
}
}

function kill(){
yyy=-1000;
if(ns4){skn.visibility="hidden";}
else if (ns6||ie4)
skn.display="none"
}

//-->
</SCRIPT>


Usage:


<a href="mylinkhere.php" ONMOUSEOVER="popup('Popup text','yellow')"; ONMOUSEOUT="kill()">click here</a>

Sunday, July 02, 2006

Centered popup window


function popUp(theURL, myWidth, myHeight) {
var winName = 'feedback';
var features='toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0';
//var myWidth = 290;
//var myHeight = 360;
var isCenter = 'true';
if(window.screen)if(isCenter)if(isCenter=="true"){
var myLeft = (screen.width-myWidth)/2;
var myTop = (screen.height-myHeight)/2;
features+=(features!='')?',':'';
features+=',left='+myLeft+',top='+myTop;
}
var handle = window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
handle.opener = window;
handle.focus();
return handle;
}