Jquery Email Field Validation

[js]
$(‘#signup_form’).submit(function(){
var hasError = false;
var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
var emailaddressVal = $("#pay_1_email").val();
if(emailaddressVal == ”) {
$("#pay_1_error").html(‘<span class="error">Please enter your email address.</span>’);
hasError = true;
}

else if(!emailReg.test(emailaddressVal)) {
$("#pay_1_error").html(‘<span class="error">Enter a valid email address.</span>’);
hasError = true;
}

if(hasError == true) { return false; }
});
[/js]

Number of Seconds to HH:MM:SS Javascript Function

[js]

function seconds_to_time(secs){
var hours = Math.floor(secs / (60 * 60));
if (hours < 10) hours = "0" + String(hours);
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
if (minutes < 10) minutes = "0" + String(minutes);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
if (seconds < 10) seconds = "0" + String(seconds);
var obj = {
"h": hours,
"m": minutes,
"s": seconds
};
return obj;
}

[/js]

Testing it: console.log(seconds_to_time(100));

Convert number of seconds to readable time format

The php version:

[php]

function formatTime($secs) {
$times = array(3600, 60, 1);
$time = ”;
$tmp = ”;
for($i = 0; $i < 3; $i++) {
$tmp = floor($secs / $times[$i]);
if($tmp < 1) {
$tmp = ’00’;
}
elseif($tmp < 10) {
$tmp = ‘0’ . $tmp;
}
$time .= $tmp;
if($i < 2) {
$time .= ‘:’;
}
$secs = $secs % $times[$i];
}
return $time;
}

[/php]

And the JS version:

[js]
function formatTime(secs){
var times = new Array(3600, 60, 1);
var time = ”;
var tmp;
for(var i = 0; i < times.length; i++){
tmp = Math.floor(secs / times[i]);
if(tmp < 1){
tmp = ’00’;
}
else if(tmp < 10){
tmp = ‘0’ + tmp;
}
time += tmp;
if(i < 2){
time += ‘:’;
}
secs = secs % times[i];
}
return time;
}
[/js]

PHP Simple Paginator Code

This is a simple example of mysql table pagination using PHP code.

Please see attached zip for refference.

This is the index.php file bellow:

[php]

/*
in config.php you define the database connection.
$limit defines the limit of items per page
$adjacents number of adiacent items on paginator
*/
include(‘config.php’);

$tbl_name="mapstories_stories";

$adjacents = 3;

$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query)) or die(mysql_error());
$total_pages = $total_pages[‘num’];

$targetpage = "index.php";
$limit = 5;
$page = $_GET[‘page’];
if($page)
$start = ($page – 1) * $limit;
else
$start = 0;

$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

if ($page == 0) $page = 1;
$prev = $page – 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage – 1;

$pagination = "";
if($lastpage > 1)
{
$pagination .= "

n";
}
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Lat</th>
<th>Lng</th>
</tr>
<?php $i = 0;
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row[‘lat’];?></td>
<td><?php echo $row[‘lng’];?></td>
</tr>
<?php }?>
</table>
<?=$pagination?>

</body>
</html>

[/php]

And the config.php:

[php]

mysql_connect(‘localhost’, ‘root’, ”) or die(mysql_error());
mysql_select_db(‘mapstories’) or die(mysql_error());

[/php]

And a small style.css – this one is using Cakephp’s default table styles 😉

[css]

body{
font-family:arial;
font-size:11px;
}
div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;
zoom: 100%;
text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;

* zoom: 100%;

font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;

* zoom: 100%;

color: #DDD;
}

* span.elipsis {zoom:100%}

/** Tables **/
table {
background: #fff;
border-right:0;
clear: both;
color: #333;
margin-bottom: 10px;
width: 100%;
}
th {
border:0;
border-bottom:2px solid #555;
text-align: left;
padding:4px;
}
th a {
display: block;
padding: 2px 4px;
text-decoration: none;
}
th a.asc:after {
content: ‘ ⇣’;
}
th a.desc:after {
content: ‘ ⇡’;
}
table tr td {
background: #fff;
padding: 6px;
text-align: left;
vertical-align: top;
border-bottom:1px solid #ddd;
}
table tr:nth-child(2n) td {
background: #f5f5f5;
}
table .altrow td {
background: #f5f5f5;
}
td.actions {
text-align: center;
white-space: nowrap;
}
table td.actions a {
margin: 0px 6px;
padding:2px 5px;
}[/css]

Click the following link to download all this code: php_pagination