|
*dhtml
|
This thread is not going to talk about javascript as well as ajax. Strictly server sided stuffs.
So far, it seems all i have been posting so far have been mostly javascript / ajax solutions. And that has caused some people to brand me as a javascript guru (and that even includes the admin).
And i find that distressing to say the list because dhtml actual comprise both client and server side. So in short, i am attempting to justify that on this thread.
Scriptlets are open for review, comments, questions, and everyone is free to participate, newbies, professionals, supporters, friends and others.
|
|
|
|
|
|
*dhtml
|
Maybe i forgot to mention this, but i specialize in writing / modifyin classes in php / javascript. But this thread is strictly php / mysql. This is a very simple class that shows how to perform some dates operation in php such as you want to add 30 days to today, I made a class out of this, discussion is going to be based on this class. You will find the class on: http://mwebng.net/demos/phpdate/days.class.php.txtDownload it and remove the .txt extension so that it becomes days.class.php (the name was because i was dealing with days initially). Example1.php <?php include_once 'days.class.php'; $days = new Days(); echo $days->dateplus2("25/3/2008","30"); ?> This example will add 30 days to 25/03/2008 and will give you 24/04/08. Example2.php <?php include_once 'days.class.php'; $days = new Days(); echo $days->datediff("25/3/2008","20/3/2008"); ?> This example will give you the days difference between 2 dates. The answer to this example will be 5 Example3.php <?php include_once 'days.class.php'; $days = new Days(); echo $days->getdate(16+30,3,2008); ?> This function here kinda evaluate what the actual date should be. This should give you 15/04/08. Example4.php <?php include_once 'days.class.php'; $days = new Days(); echo $days->dateplus(4); ?> This adds 4 days to the current date. Example5.php <?php include_once 'days.class.php'; $days = new Days(); echo $days->dateminus(4); ?> This will reduce 4 days to the current date. There are some other functions in the class which i think are redundant. This will do for now.
|
|
|
|
|
|
*dhtml
|
This example show how to get the name of months from position of month: <?php function getMonthByName($month) { $month=$month+1; return strftime("%B",mktime(0, 0, 0, $month, 0, 2000)); }
for($i=1;$i<13;$i++) { $m=getMonthByName($i); echo "<li>{$m}</li><br>"; } ?> This will yield: January February March April May June July August September October November December
What i am saying here is that echo getMonthByName(1); will give january. You can study the code and modify it to whatever you want.
|
|
|
|
|
|
*dhtml
|
How to convert string to html output - i am focusing on hyperlinks here - dont know how to explain it properly, but look index.php <?php function transformHTML($dataTxt) { $msgBody = ereg_replace("\n","<br>",$dataTxt); $msgBody = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i","$1http://$2", $msgBody); $msgBody = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<A TARGET=\"_blank\" HREF=\"$1\">$1</A>", $msgBody); $msgBody = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$msgBody); return $msgBody; }
$sample="Hi, please check out my website http://www.mwebng.net and also check out www.google.com "; echo transformHTML("$sample"); ?>
Output: http://mwebng.net/demos/htmlconvert/This will make hyperlinks visible in posts - i used this in a forum i made some years ago.
|
|
|
|
|
|
*dhtml
|
Newbies, i know i have pasted so much codes at once, if you are not clear on any part please talk o, so we go resolve am.
|
|
|
|
|
|
Seun (m)
|
It's a cool module, but doesn't PHP have built-in date functions too?
|
|
|
|
|
|
*dhtml
|
PHP does, but to perform some of the functions i created in that class is not so straight forward. If php could handle them so easily, i would not have bothered to create the class.
|
|
|
|
|
|
webdezzi (m)
|
very cool. let me check my archive and see one that may help
|
|
|
|
|
|
webdezzi (m)
|
guys, why is it that one can no longer modify post, or is it just me? here is one, they are 2 functions developers trying to intergrate with interswitch may find useful, might also help on some other logic calculates 1.5% of an end result (sorry for my english, i dont know how else to explain this) EXAMPLE USAGE: lets say a user purchased something worth N2000, and you as the seller needs to add x naira to it so that when your payment processor will be charging 1.5% of (2000+x) naira, you will end up with N2000 Note that 1.5% will be easier to deal with in this format, 0.015 function percentCal($counter, $amount) { $counter++; //adds 1 to the counter each time this script runs $percent=0.015; //the percentage simplified $result= $amount*$percent; //1.5% of the amount
//this percentCal function will run 10 times, once at the 10th time, it will return the result, for extreme accuracy, increase 10 to like 20 if($counter>10){ return $result; }else{ return $newResult=$amount+percentCal($counter, $result); } }
function getInterswitchPercent($amount) { $percent=0.015; $result= $amount* $percent; //this is the result of the 1st percentage calculation. $calcPercent=interswitchCal(0,$result); $newAmount=$amount+$calcPercent; // new amount shud be real amount plus 1.5% of percentage and so on
return round($newAmount); }
|
|
|
|
|
|
webdezzi (m)
|
there is a more efficient way to do this, but i posted that as it will help with some other logic
|
|
|
|
|
|
yawa-ti-de (f)
|
Hmm, how can you have dhtml without javascript (or any other client-side scripting language)? Doesn't make sense to me. Even the wiki, which I seldom reference, disagrees with the premise: http://en.wikipedia.org/wiki/Dhtml . And by the way, if this is a thread on "strictly server side stuff", then IMHO, the title of this post should be modified to reflect such. Sure, you can have server side code mixed in with dhtml. Having said that, dhtml doesn't require server side code.
|
|
|
|
|
|
*dhtml
|
Thanks for the contribution, i decided to pause. . .too much offline load. . .i will try post some stuffs here later. . .maypage a simple pagination script
This is about coding some non-javascript dependent stuff . . . and i can see webdezzi understands this. . .
The scripts here are pure server-sided scripts from my library, but of course, they can be combined with javascript, ajax and all that to get the task done.
|
|
|
|
|
|
*dhtml
|
On second thoughs, maybe u can help me change the title of the thread, since it seems i no longer have the right to rename or even modify my posts again not to talk of delete sef.
|
|
|
|
|
|
yawa-ti-de (f)
|
Then remove the "dhtml (without javascript)" part of the title so it isn't misleading to those new to the game. Webdezzi might understand but not the new borne babe, born into the game. Next thing he/she is going around proclaiming to the world that one can do dhtml without javascript and get laughed out of town. Again, show me dhtml without javascript (or any other client-side script) and I will show you a river that doesn't ebb and flow. Like it or not, the premise is wrong. Of course you have the right to disagree and the more you do, the more I will post links to show that one can't have dhtml without javascript (or any other client-side scripting languge). I am wiling to lose my moderatorship, be banned and be reported over this 
|
|
|
|
|
|
yawa-ti-de (f)
|
i think only the author of a thread can edit titles. I could be wrong though. I tried to do it but couldn't, though I could change the titles where I made responses to you.
|
|
|
|
|
|
*dhtml
|
The board moderator reserves the right to modify a title of a thread to make it more meaningful, omnipotens has done that alot to many of my threads, including this one sef, so that way, all i know is, i want to make a post, and that is it, some of us are not too good with coming up with title of threads really. . .
|
|
|
|
|
|
yawa-ti-de (f)
|
Again, I can't seem to change them. Maybe moderator get levels or the ogogoro I drank earlier is making me see double. I have contacted omnipotens for a tutorial. As far as coming up with titles are concerned, rather than come up with something potentially off-topic, ask someone for suggestions. Again, not good to mislead the new ones coming behind us. That's all I am saying.  Thanks,
|
|
|
|
|
|
OmniPotens (m)
|
I hope the change in the topic has helped solve some discussion issues here.
|
|
|
|
|
|
nitation (m)
|
@ All
If it was nitation's posts' to a thread, I know those who would have painted it "Off-Topic". Guys I sincerely believe there was no need for this thread to be opened in the first place. No one is interested if you are a Javascript Guru or Php evangelist. If you are getting less attention these days, I can advertise your website for you to the people of JUPITER.
kpsheeew!!!
- nitation
|
|
|
|
|
|
nitation (m)
|
Less I forget, whoever cares should report me to the admin for speaking out my mind. DHTML must stop feeding us with crap.
- nitation
|
|
|
|
|
|
twinstaiye (m)
|
guys, why is it that one can no longer modify post, or is it just me?
It is just for a while, am sure the admin is working on this. Meanwhile, you can make do with a new post to correct the one with an error.
|
|
|
|
|
|
webdezzi (m)
|
It is just for a while, am sure the admin is working on this. Meanwhile, you can make do with a new post to correct the one with an error.
they better fix this cos my english is seasonal, i need to be able to go back and edit my grammar (no mind me, na gramma school i go) @Nitation, take it easy ooo No one knows it all, and i have not seen dhtml profess he knows all @yawa, thanx for pointing that out, i must av overlooked it
|
|
|
|
|
|
yawa-ti-de (f)
|
no problem webdezzi, any time. I was basically looking out for dhtml's interests. After all, one would presume that if there is one thing he should know well, it should be the very thing that is in essence, his username - dhtml  I figured it was an oversight but I couldn't resist and had to point it out, for posterity's sake.
|
|
|
|
|
|
nitation (m)
|
@ Webdezzi
I wasn't referring to you. Also, I don't think you 're in a good position to speak for whoever. I also didn't say I know it all ok! I have directed my point to the person in question
- nitation
|
|
|
|
|
|
*dhtml
|
Hmm, looks like i am getting more attention than necessary. . .aaaaaaand loads of off-topics too. . .
|
|
|
|
|
|
*dhtml
|
So the next useless oops useful post will be how to do pagination - that means how to do all them next and previous stuffs. . . Online demo: http://mwebng.net/demos/pager/index.phpThe table being queries is the country2 table which has stuffs like: id | title | text1 | NG | Nigeria 2 | GN | Ghana 3 | NT | Nitation 4 | NL | Nairaland 5 | NLS | Nairalist 6 | OM | Omnipotens 7 | SS | Segsalerty 8 | WZ | My Friend! Gerrit? i assume so. . . So now, let us take a peek into the files: index.php <style> body {font:12px tahoma;line-height:5mm} a:link,a:visited,a:active{text-decoration:none;font:11px tahoma;} .pager {color:black;font:11px tahoma;margin-top:10px;padding-top:5px;border-top:1px solid blue;} .pager a:link,a:visited,a:active{color:blue;} .pager a:hover{color:red;} </style> <title>Dynamic Pager Lite</title>
<?php include_once "connect.php"; include_once "dbview.php";
$dv=new dbview("select * from country2",25);
foreach($dv->rows as $rows) {$dv->garray($rows); print<<<end <li>$id | $title | $text</li> end; }
print<<<end <div class='pager' style="width:400px;"> < $dv->prev | $dv->next > Showing <b>$dv->pos_start</b> to <b>$dv->pos_pause</b> out of <b>$dv->maxlength</b> </div> end; ?> It looks quite simple iight? but the main pager file is a class i wrote some while back. . . //let us examine this code: $dv=new dbview("select * from country2",25); It means that, the pager should select from the country2 table and display 25 items per view. Gbam! Of course, the class uses alot of my library functions, which i had to import in. . . As for the connect.php, it is just the same regular dbconnector: connect.php <?php $link = mysql_connect('localhost', 'db_user', 'db_pass'); mysql_select_db("country",$link) or die ("Could not select database"); ?>
Aaaand finally, the main one dbview.php <?php class dbview {
function href() {$sfile="http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];return "$sfile";} function qhref() {$q=$this->fetchQuery('start');if($q!="") {$q="?$q&start=";} else {$q="?start=";};$href=$this->href().$q;return $href;}
function garray($arr,$search="",$replace="") {if(!is_array($arr)) return; foreach (array_keys($arr) as $key) {$val=$this->unesc(stripslashes($arr[$key])); $val= $val=="$search" ? "$replace" : "$val"; $key='$GLOBALS["'.$key.'"]="'.$val.'";';eval("$key"); };return; }
function unesc($text) {$replace = array("'");$find = array("%27");return str_replace($find, $replace, $text);}
function instrEx($str,$substr) {if (substr_count(strtolower(trim(($str))), trim(strtolower($substr)))>0) {return true;} {return false;}}
function fetchQuery($exclude) { if(!isset($_SERVER['QUERY_STRING'])) {return "";} $arr=spliti("&",$_SERVER['QUERY_STRING']); $xclude=spliti(",",$exclude); $result=array(); foreach($arr as $temp) {$add=true; foreach($xclude as $x) {if (substr_count(strtolower(("$temp")), strtolower("$x"))>0) {$add=false;}} if($add) {$result[]=$temp;} } return implode("&", $result); }
function dbview($search,$maxitems=10) { //init engine start $r=mysql_query("$search") or die("$search\n<br>".mysql_error()); $maxlength=mysql_num_rows($r);
$start= intval($_REQUEST['start'])<=0 ? "0" : $_REQUEST['start'];
if($start>=$maxlength) {$start=$maxlength-$maxitems;}
$search="$search limit $start,$maxitems";
$result=mysql_query("$search") or die("$search\n<br>".mysql_error()); //init engine stop
//create array start $rows=Array(); while($rows[]=mysql_fetch_array($result)) {} $rows=array_splice($rows,0,-1);
$this->rows=$rows; //create array stops
//parse result start $posa = $start-$maxitems;$posb = $start+$maxitems;
$url=$this->qhref(); if($posa >=0) {$prev_lnk="<a href='$url$posa'>Previous</a>";$prev_txt="";} else {$prev_lnk="";$prev_txt="Previous";} if($start+$maxitems+1<=$maxlength) {$next_lnk="<a href='$url$posb'>Next</a>";$next_txt="";} else {$next_lnk="";$next_txt="Next";}
$prev= $prev_txt=="" ? $prev_lnk : $prev_txt; $next= $next_txt=="" ? $next_lnk : $next_txt;
$pos_start=$start+1;$pos_pause=$pos_start+$maxitems-1;
if($pos_pause>$maxlength) {$pos_pause=$maxlength;}
$this->prev=$prev; $this->prev_lnk=$prev_lnk; $this->prev_txt=$prev_txt;
$this->next=$next; $this->next_lnk=$next_lnk; $this->next_txt=$next_txt;
$this->pos_start=$pos_start; $this->pos_pause=$pos_pause; $this->maxitems=$maxitems; $this->maxlength=$maxlength; //parse result ends }
} ?>
Dont bother with the explanation, just regular php codes used in a class definition. . .o pari. . . Aaaand, that is the end of this particular pager. . .
|
|
|
|
|
|
*dhtml
|
next post is how to rotate images dynamically with php. We are looking at: http://localhost/mwebng.net/demos/rotate/index.php <table> <tr> <td> <img src="rotate.php?src=lol.jpg°rees=0"> </td> <td> <img src="rotate.php?src=lol.jpg°rees=180"> </td> </tr> <tr> <td> <img src="rotate.php?src=lol.jpg°rees=45"> </td> <td> <img src="rotate.php?src=lol.jpg°rees=90"> </td> </tr> </table> rotate.php <?php $filename=$_REQUEST['src']; $degrees=$_REQUEST['degrees'];
header('Content-type: image/jpeg'); $source = imagecreatefromjpeg($filename); $rotate = imagerotate($source, $degrees, 0); imagejpeg($rotate); ?> Pretty simple huh? but i needed to use this in a particular website rather than store loads of images, coding was the answer. . . Aaaand that is it for the day. . . @all, decide for yourself if this thread has been useless so far, except for maybe the off-topic posts. . .
|
|
|
|
|
|
*dhtml
|
[side-comments] Ehm, if anyone find my stuffs useless, please raise up your hand o. . .really, this place is turning to a zoo or rather market place sef [/side-comments]
|
|
|
|
|
|
webdezzi (m)
|
i love your image rotate technique, it relies on php's GD library which some naija hosts don't have installed (can u beat that?)
i remember calling a naija host and imagine, telling you there are other ways your programmer can do it.
@Dhtml, i believe u can still modify that to accept pngs, and gifs and security wise, do you think think wont be dangerous?
rotate.php?src=C:\Users\sledjama\Desktop\emel\onstream2.gif i ran that and i loaded an image from my desktop, am not sure it's a security threat but tech keep advancing these days
Did u write that dbview class urself?
|
|
|
|
|
|
*dhtml
|
Thanks, i will modify the image rotate later on to support other image formats such as png, and others that php can handle. As for nigerian host, i dont really use them per se, and well, where there is no good programming platform, we have to manage.
And as for the dbview class, i wrote it myself (i downgraded the one i actually use), i did mentioned more than once that i work more with objects rather than procedural stuffs.
I will look for the makings of that viewer class and post it later. The makings of course is procedural, but longer to use from pages to pages, due to changes made here and there. . .
|
|
|
|
|
|
*dhtml
|
This is the makings of that dbase viewer. . . dbview.php <style> body {font:12px tahoma;line-height:5mm} a:link,a:visited,a:active{text-decoration:none;font:11px tahoma;} .pager {color:#000080;font:11px tahoma} .pager a:link,a:visited,a:active{color:blue;}
</style>
<?php include_once "connect.php"; include_once "lib.php";
//config start $table="country2"; $maxitems=10; //10 items per page $search="select * from $table"; //config ends
$start=$_REQUEST['start']=="" ? "0" : $_REQUEST['start']; //$maxitems+=$start; $search="$search limit $start,$maxitems"; $maxlength=docount("$table","sectorId","");
$result=mysql_query("$search") or die("$search\n<br>".mysql_error());
while($rows=mysql_fetch_array($result)) { garray($rows); print<<<end <li>$title | $text</li> end; }
$posa = $start-$maxitems;$posb = $start+$maxitems; if($posa >=0) {$prev="<a href='dbpager.php?start=$posa'>Previous</a>";} else {$prev="Previous";} if($start+$maxitems+1<=$maxlength) {$next="<a href='dbpager.php?start=$posb'>Next</a>";} else {$next="Next";}
$pos_start=$start+1; $pos_pause=$pos_start+$maxitems-1;
print<<<end <div class='pager'> <hr size='1' width='500' align='left'> <b>$prev</b> | <b>$next</b> Showing <b>$pos_start</b> to <b>$pos_pause</b> out of <b>$maxlength</b> </div> end; ?>
And i had to borrow some general functions from my library. . .something like that. . .
|
|
|
|
|
|