...

Hi! I’m Starx

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

eZend_Json::encode or json_encode with special characters

Question by user1252306

I’m trying to encode an array which has special characters such as ñ. When I do that it returns null. I tried to use this option in Zend:

Zend_Json::$useBuiltinEncoderDecoder = true: 

And it doesn’t return null but, for example, in this string “something with ñ” return “”something with u00f1”

And if a I use for json_encode, for example:

<?
$string = "something with ñ";
echo "<pre>";
print_r($string);
$encode_json = json_encode($string) ;
print_r($encode_json);
?>

return is the same:

something with ñ

something with u00f1

And if I use utf8_enconde(), return this character u00c3 for ñ.

Some ideas to solve my problem? I need to save in the database at list words with ñ, if I can save another special character would be great.

Answer by Starx

You can encode Unicode Characters using json_encode() function as well

echo json_encode($yourdata, JSON_UNESCAPED_UNICODE);

Note: This is only available PHP 5.4 onwards

Read more

how to button enable when all textbox has value in jquery?

Question by nic

i am new in programming.here i have given description of my problem
i have one pair of two text box. one text box is for url and other is for instruction but all text-boxes created dynamically depending on what value comes from database.

for example $testing_type_id=2 so i get total two pair of text boxes ,let it called bundle.$i is used for indentify textbox uniquely.

  <input type = "text"  size="33"   class="uploadtxt1 url" name="txturl_<?php echo $testing_type_id ; ?>" id = "txturl_<?php echo $testing_type_id; ?>_<?php echo $i ; ?>" value="" />
             <input type = "text"  class="uploadtxt2" size="33" name="txtinstruction_<?php echo $testing_type_id ; ?>" id = "txtinstruction_<?php echo $testing_type_id; ?>_<?php echo $i ; ?>" value=""  /> <br/>
             <input type="submit" name="checkout" id="checkout"   class="graybtn1 ptr  button_float_left_checkout" disabled="disabled" value="Proceed To Checkout" />

here what i wanted to do that if all bundle has value, either in one textbox or both textbox so and so enable submit button.if i removed value so disable submit button using jquery.

need a help urgent.Thanks in Advance.

Answer by Sheikh Heera

I think this will work

$(document).ready(function(){
    $("input[class^='uploadtxt']").keyup(function(e){
        var alltxt=$("input[class^='uploadtxt']").length;
        var empty=true;
        $("input[class^='uploadtxt']").each(function(i){
            if($(this).val()=='')
            {
                empty=true;
                $('#checkout').prop('disabled', true);
                return false;
            }
            else
            {
                empty=false;
            }
        });
        if(!empty)  $('#checkout').prop('disabled', false);                    
    });
});​

An example is here.

Answer by Starx

I am going to suggest a minor markup changes so that will be easy to handle the traversing.

Wrap the elements in a <div>

<div class="item">
    <input type = "text"  size="33"   class="uploadtxt1 url" name="txturl_<?php echo $testing_type_id ; ?>" id = "txturl_<?php echo $testing_type_id; ?>_<?php echo $i ; ?>" value="" />
    <input type = "text"  class="uploadtxt2" size="33" name="txtinstruction_<?php echo $testing_type_id ; ?>" id = "txtinstruction_<?php echo $testing_type_id; ?>_<?php echo $i ; ?>" value=""  /> <br/>
    <input type="submit" name="checkout" id="checkout"   class="graybtn1 ptr  button_float_left_checkout" disabled="disabled" value="Proceed To Checkout" />
</div>

Then, Attach a snippet on the change event

$("input[type=text]", $(".item")).change(function() {
    var allField = true;
    $(this).closest(".item").find("input[type=text]").each(function() {
         allField = allField && ($(this).val().length > 0)
    });
    if(allField) {
        $(this).closest(".item").find("input[type=submit]").prop('disabled', false);
    }
});

Demo of Working Solution

Read more

Why is this css overriding this other one?

Question by leora

I have 2 css files in my page:

  1. Site.css
  2. jquery-ui.css

Site.css is listed BELOW the jquery-ui css

I have a link that looks like this on my page:

 <a class='closeClueTipLink'>close</a>

and the issue is that the link is showing up black instead of the normal blue text. When i use firebug to figure out why, i see this:

enter image description here

I don’t understand why .ui-widget-content (which has the color #222222) is overriding .closeClueTipLink (which as color:blue) given that site.css is below the jquery one.

Any ideas or suggestions why this ordering is happening ?

Answer by BoltClock

Because there’s an a selector just after .ui-widget-content:

.ui-widget-content a

Making it more specific than .closeClueTipLink, even though .closeClueTipLink is found in a later stylesheet.

You could easily balance this out by adding the same type selector to your other rule so you get a.closeClueTipLink, making both selectors equally specific (1 type and 1 class). Then that, being the rule that’s defined and loaded later, will apply and your link text will be blue.

Answer by Starx

Because .ui-content-content a { } is loaded after the previous style .closeClueTipLink.

I am pretty sure jquery...tom.css is loaded after site.css, so the styles defined later overrides previously defined styles.

There are ways you are amend this problem:

  1. Pinpoint the selector like .ui-content-content a.closeClueTipLink
  2. Use !important at the end of color defination. color: #222 !important;[not recommended]
Read more

Is it correct to use DIV inside FORM?

Question by demas

I’m just wondering what are you thinking about DIV-tag inside FORM-tag?

I need something like this:

<form>
  <input type="text"/>
  <div> some </div>
  <div> another </div>
  <input type="text" />
</form>

Is it general practice to use DIV inside FORM or I need something different?

Answer by Starx

No, its not

<div> tags are always abused to create a web layout. Its symbolic purpose is to divide a section/portion in the page so that separate style can be added or applied to it. [w3schools Doc] [W3C]

It highly depends on what your some and another has.

HTML5, has more logical meaning tags, instead of having plain layout tags. The section, header, nav, aside everything have their own semantic meaning to it. And are used against <div>

Read more

find texbox value using javascript?

Question by pradip

hear is my HTML:

   <div>
   <input type='text' />
  <input id='mybutton' type='button' value='button'/>
  </div>

I want to find text box value when button is clicked:

Answer by Starx

You can select the textbox using input[type=text] but I do not recommend doing so as this is more of a genaralised selector and cover other textboxes too if they are present.

Instead, Give an id to the textbox, like:

<input type='text' id="mytextbox" />

Using jQuery:

$("#mybutton").click(function(){
    var text = $('#mytextbox').val();
    console.log(text);
});

Using javascript

textbox = document.getElementById("mytextbox");
mybutton = document.getElementById("mybutton");
mybutton.onclick = function() {
   console.log(textbox.value);
};
Read more

How to display a link in <description> tag of rss feed

Question by Vinay

I am generating a rss(xml) output form a php file. I have got a link in <description> tag of rss file, I want a link to be displayed inside the description, I have written code as below.

<description><a href='http://www.google.com'>Google</a></description>

But its not displaying the link in the mozilla browser but in the IE, the text is getting printed without link, But google reader and feedburner says that it is not valid,

When I view the file source code, it looks as below

<description><a href=http://www.google.com>Google</a></description>

I know using below methods works

1) I know using htmlentities() function works, but when i view the source “<” replaced by "&lt;" , and “>” by "&gt;"

2) Using CDATA, Instructing the interpreter to not to parse the data enclosed in CDTA

In above cases the rss feed gets generated, As xml file is used to carry the data and not any presentation information, So both the cases violates the xml concept

So. Is there any way to write a valid rss(xml) file.

Answer by Starx

Check how the feed of stackoverflow works. It uses the exact same way as you are using. There is something else causing the problem probably.

Read more

How to create a "listening(waiting?)" method

Question by Andrius Naruševičius

Lets say I have:

class X
{
    function a()
    {
        echo "Hello, ";
    }
    function b()
    {
        echo "John!";
    }
}

and then

Y = new X();
Y->a();

but once the method a is called, I also want the method b called immediately after it (so it kind of listens(waits?) till the moment when a is called and finished), so the output is

Hello, John

Is it possible to do that, and how that should look?
And no, calling $this->b(); at the end of method a is NOT a solution to what I want to do 🙂 Thanks in advance 🙂

Answer by Starx

You are searching for observer pattern. Read some example from the internet, you should be able to do what you are attempting.

However, A very simple example of using observer pattern:

class X
{
    private $observer;
    public function __construct() {
        $this -> observer = new XObserver();
    }

    function a() {
        echo "Hello,";      
        $this -> observer -> aExecuted($this);
    }

    function b() {
        echo "John!";
    }
}

class XObserver {

    public function aExecuted($obj) {
        return $obj -> b(); 
    }
}
Read more

giving top positions to li using variables in jquery

Question by user1302513

hello friends i want to set position to every <li> using jquery i have tried following code but its not working as i want

<script>
$(document).ready(function(){

var t = 10;
var j = 0;

function cycle(){

var l = $('ul#move li').length - 1;
alert(l);
 $('ul#move li:eq('+j+')').css('top',t + 'px');

 if(j == l)

 {
     j=0;
     t = 10;
 } 
 else
 {
 j++;
 t = t+10;
 }

cycle();
}
})
</script>

html

<ul id="move">
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
  <li>five</li>
  <li>six</li>
</ul>

CSS

<style>
ul#move
{
    position:relative;
    width:300px;
    border:solid;
    font-size:3em;
    height:300px;
}
ul#move li
{
    list-style:none;
    margin:0;
    padding:10px;
    position:absolute;
}
</style>

i want to set top positions to every <li> the gap shuld be 10 px in every <li>

Please help

thanks in advance

Answer by Starx

I have a very strong feeling you are trying to do this

var lis = $('ul#move li')
var l = lis.length - 1;
var j, t;
lis.each(function(index) {
   if(j ==1) {
      j = 0;
      t = 0;
   } else { 
      j++;
      t += 10;
   }
   $(this).css('top',t + 'px');
});

Demo

However, If gap is the only problem. A simple CSS can do the trick

#move li { margin-top: 10px; }

Demo


Update

Animate li can be done like this

var lis = $('ul#move li')
var step = 30;
lis.each(function(index) {
    $(this).animate({
        'top': (index*step)+ 'px'
    });
});

Demo

Read more

Parsing text files in javascript/php?

Question by user851262

Here is my file: http://www.mediafire.com/?17bggsa47u4ukmx
It is basically a text file with a different extension.

It is a test that i am building a test reader for in html/javascript/php.
I want it to read the $$0 meaning the first question and then get the next line of content after it, which will always be a question.

My code:
In php:

$lines = file("hlm08.dat"); //file in to an array
// Make it search the whole file possible using a while loop by replacing the $$0
if (in_array("$$0", $lines)) {
    echo "I found first Question Number";
    /// Code to find the question right after it
}

Part of file:

Hotel and Lodging Management
I
$$0

What do some hotel chains develop to establish formal relationships with employees?

A. Applications 
B. Regulations 
C. Policies
D. Contracts


/D
Contracts. Contracts are agreements between two or more people or organizations
stating that one party is to do something in return for something provided by
the other party. Employment contracts usually specify what an employee is
expected to do in exchange for being compensated by the hotel chain. These
contracts are often considered legal agreements that establish formal
relationships with employees. Hotel chains often develop regulations and
policies that employees are expected to follow, but these do not establish
formal relationships with the employees. Applications are forms that potential
employees fill out to apply for jobs.
$$1

An impact of antitrust legislation on business is that it prevents hospitality
businesses from

A. experiencing growth. 
B. being competitive. 
C. raising prices.
D. forming monopolies.

I am not sure how to make it scan the whole file and put all the questions in an array. the questions come right after each $$0 (number of the question).

Answer by Starx

I suppose foreach can do what you are trying

$lines = file('hlm08.dat');

foreach ($lines as $line_num => $line) {
    if($line == '$$0') {
         echo $lines[$line_num+1]; //'get the next line
    }
}

Update:

But after you update, extract the content using regex might be better.

$text = file_get_contents('hlm.dat');
preg_match_all('/$$[0-9](.*?)$$/', $text, $matches);
print_r($matches);
Read more
March 29, 2012

first child in jquery

Question by Chapsterj

HTML:

        <div class="wrapper" style="display:none">
          <div class="panel"></div>
        </div>

JS CALL

var element = $(".wrapper");
element.toggle().children(":first").custom();
$(".wrapper .panel").custom();

CUSTOM JQUERY METHOD

  $.fn.custom = function() {  
        return this.each(function() {
           console.log(" this = ", this);
               // do something to each dom element.
        });
    };

Whats odd here is when I do a console.log in IE its showing the first call to the panel element as a

[object HTMLGenericElement]

but the second call shows it as a [objectHTMLDIVElement]

Why is this and how to have the first call be a [objectHTMLDIVElement]

Answer by Starx

You should use $(this) instead of this inside the each function.

return this.each(function() {
    console.log(" this = ", $(this));
    // do something to each dom element.
})

Besides, I checked this with chrome, safari, firefox and everywhere I am receiving the <div> element

Read more
...

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