...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 24, 2012

How to run two versions of static website simultaneously for A/B testing?

Question by Jitendra Vyas

For example I have a very simple website only HTML/CSS/Javascript based no php,.net, ruby is being used. Some visitors to my site should see version “A,” while others see version “B.

Site is hosted on Apache server

Answer by Starx

One way is by analyzing the IP Address or range.

Create list of IP Address/Range

$ips = array(
    "a" => array( 'xxx.xxx.xxx.xxx', ....),
    "b" => array( 'xxx.xxx.xxx.xxx', ....),
);

Now, use $_SERVER['REMOTE_ADDR'] and switch the domain

if(in_array($ips['a'], $_SERVER['REMOTE_ADDR'])) {
    header("location: website.com/a");
} else {
    header("location: website.com/b");
}
exit;
Read more

last-child() and :after not working in IE 8

Question by Kamal

Hello friends following is my code

HTML

<ul class="menu-item">
    <li>item</li>
    <li>item</li>
    <li>item</li>
</ul>

CSS

.clear-right{
color:red;
}
.menu-item:last-child{
    color:red;}
.menu-item:last-child:after
    {
content:'hi';
}

my code is working perfectly in all browser but not working in IE m trying to do same thng using jquery

$(".menu-item li:last-child:after").css('content','"hi"');

you can check demo here
but its also not working please help me

Thanks in advance

Answer by Starx

:after and :before are pseudo elements, which cannot be manipulated afterwards. So you can’t do this.

An alternate possible selector would be this

$(".menu-item li").last().css('content', 'hi');

One trick to solve it is.

Create a class with your needed CSS

.newClass {}
.newClass:after { content: 'hi'; }

Now using jQuery switch them.

$(".menu-item li").last().addClass("newClass");
Read more
April 23, 2012

How do i echo php for json string?

Question by Azukah

i need to echo $account into $url so that the url/path is whatever i get from $account plus the extension json. I can’t figure it out with the quotes. i tried single and double quotes but no luck. any ideas?

<?php 
$account = $_POST['account'];
$url = 'echo $account.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

Answer by Brombomb

Like this?

<?php 
$account = $_POST['account'];
$url = $account. '.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

the . operator is used for string concatenation in php. This means take the value of $account and append the string .json to the end, and store that in the variable $url. The rest of the code looks all right from there. There are a few other ways to do this as well with strings in php, but I find this one simplest.

Answer by Starx

You can do this as this

$account = $_POST['account'];
$content = file_get_contents($account.'.json');
$json = json_decode($content, true);

Need I remind you how, vulnerable your codes are to injection?

Read more

how to parse json in php and javascript

Question by Jetoox

http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5

I would like to extract the video thumbnail and video link using PHP and JavaScript from the url above but I’m not sure how to do it, here is my attempt so far:

$json = 'http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5';
$data = json_decode($json);
print_r($data);

Answer by Gäng Tian

in PHP

  $tmp = file_get_contents('http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5/');
  $data = json_decode($tmp,true);

in javascript with jquery

  $.get("http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5/", function(response) { 
       var data = $.parseJSON(response);
  });

about the illegal character, depends on what counts as illegal in your case, just do some string validation as you traverse through the object for the thumbnail and video link you looking for.

Answer by Starx

Using, PHP as you are doing, use json_decode(). but use it one extra paramter

$data = json_decode($string, true); // the true in the last will change into associative array

For JavaScript, as Kolink said, use JSON.parse()

Read more

Converting php4 date() to php5 date_format()

Question by j-man86

I have the following code:

$date = mktime(12, 0, 0, $month, 1, $year); // 1st day of month as unix stamp
for( $day = 1; $day <= date("t", $date); $day++ ) {
     //...
}

When executed it produces the following notice:

Notice: A non well formed numeric value encountered in
/home2/wordprh4/public_html/contenido/themes/bam/events/table-mini.php
on line 53

I would like to convert date(); using php5 date_format() but I’m having some issues…

What’s the correct way to do this?


FYI line 53 is

for( $day = 1; $day <= date("t", $date); $day++ ) {

Answer by Starx

I dont see a need to do this, but You can use date_format() like this

$date = date_create('2012-02-01'); //First you have to create the date
echo date_format($date, 'Y-m-d H:i:s'); //Next simply pass the needed format
Read more

Warning: mysql_fetch_assoc() expects parameter 1 to be resourc

Question by Mahdi_Nine

Possible Duplicate:
mysql_fetch_assoc error, can't seem to figure out what the problem is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

<?php
$Dblink = mysql_connect("localhost", "root", "","Amoozeshgah");
$res = mysql_query("select * from Tbl_About");
$rec = mysql_fetch_assoc($res);


?>

the query is working but has following error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean

why?

Answer by Starx

You are mixing API’s of mysql and mysqli. To be more specific, you use the mysqli_connect() as mysql_connect(). Here are the two different ways to solve the problem.

Using MySQLi

$Dblink = mysqli_connect("localhost", "root", "","Amoozeshgah");
$res = mysqli_query($Dblink, "select * from Tbl_About");
$rec = mysqli_fetch_assoc($res);

But, If you want to keep on using MySQL API, then, you have to use mysql_select_db() to select the DB

$Dblink = mysql_connect("localhost", "root", "");
mysql_select_db("Amoozesh");
Read more

How should I integrate both jQueryUI draggable and sortable in my project?

Question by chaonextdoor

I want to integrate both jQueryUI draggable and sortable in my project and distinguish them using the time difference between the starting time of mousedown and first mousemove events. Part of my code is as follows:

var deltaX = self.mouse.startX - event.pageX,
    deltaY = self.mouse.startY - event.pageY;
if (self.mouse.mousedownTime - self.mouse.mousemoveTime < 700){                   
                // Drag down
                if(Math.abs(deltaX) < Math.abs(deltaY) && deltaY < 0){
                    $(self.el).draggable({disabled: false});
                    $(self.el).sortable({disabled: true});
                    $(self.el).draggable({axis: 'y'});
                    $(self.el).draggable({revert: true});
 ....
}
// Sorting action
else {
            $(self.el).sortable({disabled: false});
            $(self.el).draggable({disabled: true}); // global dragging
            $(self.el).sortable({containment: $(self.el).closest('.content')});
        }

But things are not what I expected. Every time I start dragging the element, the html shown in firebug has the right draggable/sortable class setting but it’s not effective for current dragging events. For example, when you drag for the first time, the element is neither draggable nor sortable although the html has already had the corresponding class setting. It will be draggable or sortable the second time you drag it. And if you set the element draggable/sortable for current dragging event, it only works the next time you drag it.

In a word, the draggable/sortable event you expect for current event will only be effective for the next event. I wanna know if this is what the jQueryUI should be or there is sth wrong with my code. How should I fix it? How should I integrate both draggable and sortable in my project?

Answer by Starx

This is an expected behavior, to hook an plugin onto the element, it has to listen to when it will be dropped and where it will be dragged.

Normally these two plugin work side-by-side. So I dont think there is a way to do this, without making the element only draggable, since droppable would need to be dragged anyhow.

Read more

PHP redirect on exact date

Question by user990175

I’m using this code to redirect based on the hour of the day, and the day of the week.

Here it is ..

<?php

$hour = date('G');
$minute = date('i');
$day = date('w');
$m = $hour * 60 + $minute; // Minutes since midnight.

if (
  $day == 0    // Sunday...
  && $m >= 615 // ... after 10:15…
  && $m <= 700 // ... but before 11:40…
) {
  header("Location: open.php");
}
else
if (
  $day == 3     // Wednesday...
  && $m >= 1125 // ... after 18:45…
  && $m <= 1235 // ... but before 20:35…
) {
  header("Location: open.php");
}

?>

I was wondering if there was a way to redirect to a page based on an exact date in the future like April 25th or November 1st.

Thanks .

Answer by Dan Lee

This is a simple approach, which does not take account the year date:

// 25th april:
if (date('d') == '25' && date('m') == '4') {
  header('Location: open.php');
}
// 1st nov:
if (date('d') == '1' && date('m') == '11') {
  header('Location: open.php');
}

Look at the date() documentation for more exact details.

Answer by Starx

Convert the redirection date into timestamp using strtotime() or date() and you can do it easily

if(strtotime("2012/5/3") <= time()) {
     header("location: toredirect.php");
     exit;
}
Read more

Php cookie only set for 1 second?

Question by Jamie Hutber

Maybe there is a better way around having to use a cookie. But i want to set in my php something to tell the user an error but only have it come on once, if they refresh the page i do no want it there anymore.

$time = (1000);
setcookie("msgtype", $type, time() + ($time), '/');
setcookie("msg", $message, time() + ($time), '/');
print_r($_COOKIE);

So for some reason, if i output the $_cookie directly after it still won’t display this cookie.

Any ideas?

Answer by gopi1410

Instead, when showing the error clear the cookie, rather than having it set for 1 second, so that when it refreshes next time & checks for the cookie it won’t be there and so would not display any error.

Or a better option would be using sessions.

Answer by Starx

You dont need to use cookies for this, using SESSION sounds reasonable.

$_SESSION['error'] = 'soem message';
//Use it when you need and simply remove it afterwards
echo $_SESSION['error'];
unset($_SESSION['error']);
Read more

Is there a way to take off the style off a certain table?

Question by user1250526

Hi guys I have a css file which styles my tables, although I have one table where I would like to use a different style, or no style? is there a way I can do something like and then it is plain and ignores the css?

I have looked but I can not find anything related!

Answer by mert

Use class definitions for table properties in your CSS file. Whenever you want them, use with class property.

CSS

table.myClass {
...
}

HTML

<table class="myClass">...</table>
<table class="anotherTableWithAnotherClass">...</table>

Answer by Starx

CSS are cascading style sheets, they only style an element. Can’t manipulate anything. You will need to use JavaScript.

Best way I know of, is to use CSS classes for different styles. And use javascript to switch them or remove them.

Read more
...

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