August 30, 2013

Shortened url redirect?

Huyimin’s Question:

I need help to get this done.

My webpages have formatted urls in this pattern: “” for example – if there are more pages, only the last page id number will be different.

I want to get this part “index.php?page=item&id=” out, and shortened like ““, when a user input the shortened url into to address bar, it will be directed to the right page, which is the true url.

I am thinking to save the STATIC part “index.php?page=item&id=” into a string variable, and append the DYNAMIC page id – which is 4 in this example, then use Javascript or PHP to direct to the right page. But I don’t know how the steps, pls help. Thanks.

Actually an htaccess will be very good for this purpose.

For urls like do the following: Create a file with name .htaccess and place at your root directory with the following content.

RewriteEngine On
RewriteRule ^s([^/]*)$ /index.php?page=item&id=$1 [L]

Override if condition fulfills in sleep() function in php

MR.Test’s Question:

Is it possible to write sleep function with condition in php?

Public function sleepy() {
If(condition a) { 
} else {
 Echo 'some thing'

So that the sleep(50) must be override the 100 if condition full fill?

How could I achieve similar concept, in proper programming practice?

By the way, I am typing from my iphone, sorry for any unclear code, if any…

If that is only thing you are trying to do, then just move the the sleep(100) inside the else portion.

public function sleepy() {

    if(condition a) { 
    } else {
        echo 'some thing'

If you are really trying to override the parameter like that, you can design you own function to execute the sleep afterward instead.

August 29, 2013

What does 'x' mean as the last letter in `src` attribute

MyTitle’s Question:

In some projects I noticed that javascripts included into HTML like this:

<script type="text/javascript" src="./js/score.js?x"></script>

What’s mean last ‘x’ symbol?

It is not a symbol. It is a piece of query string like on web scripts it would be something like test.php?reload=true

Such techniques might be helpful to overcome the caching problem as mentioned by SLaks on the comments.

Execute query that normally runs on a single ID for every ID

Damien.Bell’s Question:

So I have this query:

(SELECT    MAX(`Time`) 
FROM       ddHistorical 
WHERE      ID = '5')))

I want to run that on every ID in my database, how would I do that?

Just remove the WHERE part, it will run on every rows.


Verifying Email address

Liondancer’s Question:

I’m trying to create a form that verifies that an email address has been entered. Not necessarily check its validity but basically I was wondering how to go about doing it in PURE JavaScript and no RegEx.

    <!DOCTYPE html>

    <html lang="en" xmlns="">
        <meta charset="utf-8" />
        <title>Test File</title>
            function submitForms() {
                email_format = document.getElementById('email_input')       // want to return T or F
                if (email_format) {
                    //print email successful
                else {
                   //print error message


            <input type ="email" id ="email_input" />

        <button type = "button" onclick = "submitForms;"> Submit All!

Here is a previously answered regular expression answer. Its pretty good.

function validateEmail(email) { 
    var re = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(
    return re.test(email);


However, the only good to verify an email address is by sending a confirmation email and hearing back.

How to change content of website loaded in iframe?

Ivan.daragan’s Question:

I need to change content of website using jQuery loaded in iframe from other domain such this:

    <iframe src="></iframe>

Also I added target link to whitelist.
Could any helps? Thanks.

Your script is running during runtime so it will not find the DOM of the iframe and will break. What you can do is create a function on your parent page like:

//On Your Parent page
function modifyIframeContent() {

Then call this function from the iframe after it loads.

// On Your Iframe page
window.onload = function() {

Of course: Your iframe must be of same domain for this work.

August 26, 2013

How to check date range more than 6 months in php?

Oscar’s Question:

What I want to do is check the date range cannot more than 6 months, else will return false

here is my sample code

$date_string1 = "2013-01-01";
$date_string2 = "2013-08-01";
$date1 = date('Y-m-d',strtotime($date_string1));
$date2 = date('Y-m-d',strtotime($date_string2));

if ($date1 and $date2 range more than 6 months, so will){
   return false;
   return true;

here is my GUI

enter image description here

Any idea how to solve my problem? thanks

$date1 = DateTime::createFromFormat('Y-m-d', "2013-01-01");
$date2 = DateTime::createFromFormat('Y-m-d', "2013-08-01");
$interval = $date1->diff($date2);
$diff = $interval->format('%m');

if($diff > 6){
 echo 'false';
 echo 'true';

You can use DateTime class and calculate interval based on months:

$begin = new DateTime( '2013-01-01' );
$end = new DateTime( '2013-08-01' );
$interval = $begin -> diff($end);
if($interval -> m < 6) { 
     return false;
} else {
    return true;

A simple airthmatic way is this

if ((strtotime('2013-01-01')-strtotime('2013-08-01')) < 5184000){
   return false;
   return true;

Can we add class attribute in option element?

Ifan Iqbal’s Question:

I want to add class for my option element. Is that valid to add class attribute in HTML option element?

Yes it is valid.

From the W3Schools website:

The <option> tag also supports the Global Attributes in HTML.

From which the class attribute is a part of. Please note that often, the option tag has formatting issues regarding the browser you are using, so styling it can be a little tricky.

Yes class belongs to global attributes. Any element can have it.


version update script for a script

Marlboroman213’s Question:

I’m going to try making this easy to understand and hope it makes sense.

I have a PHP script / template and I want the end user to be able to know when I updated something, (eg. template change or a bugfix) and they can click a link to download the updated version from a remote host. I tried the scripts posted on
PHP – How to check a script version and I sorta got this script working:

<?php define('REMOTE_VERSION',');
    define('VERSION', '2.0.1');
    $script = file_get_contents(REMOTE_VERSION);
    $version = VERSION;
    if($version == $script) {
        echo "<div class=success> 
    <p>You have the latest version!</p> 
    } else {
        echo "<div class=error> 
    <p>There is a update available!</p> 

Well sort of… The .txt file on my remote server just has 2.0.1. Since they are the same version (both 2.0.1), it should read “You have the latest version!” In this case it says “There is a update available!” no matter what number I put in.

define('VERSION', '2.0.1'); //in php above 

2.0.5 //in .txt file on remote server

Says same things as it should because on the remote server is showing a new update (eg. 2.0.5). Can anyone tell me what I am doing wrong?

This might be a typo, but there is an error at your constant definition.

                     // ^ Missing quote
August 25, 2013

MYSQL IN Clause error

NewPHP’s Question:

I have used the below code in mysql query:

foreach($pID as $p)
$table_tsk  = new Timesheets_Table_Tasks();
$select_tsk = $table_tsk->select()
            ->from($table_tsk, array
           ->where('timesheets_tasks.ProjectID IN ('.$AIDS.')')

But using the above code I am getting the following error:

“An error has occured
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘))’ at line 1”

I have added a quotation mark(") for IN clause. But the problem is the query only displays for the first $AIDS number. Could someone help me to clear the error?

It should be specified as:

->where('timesheets_tasks.ProjectID IN (?)', $all_PIDs)

so you’re passing an array of integers, not the comma-separated list of it

On your codes the quotes are not part of your MySQL query but only your PHP portion. DO this

$AIDS= "'".implode("','",$all_PIDs)."'";

And then

>where('timesheets_tasks.ProjectID IN ('.$AIDS.')'

Please fill the form - I will response as fast as I can!