...

Hi! I’m Starx

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

Magento Category with Category Images

Question by darshan.dodiya

I am facing one problem, So i am here to get Some solutions form you! I want to display category menu with category images near to each categories with in that menu.

So when i click on menu if that category contain sub-category it show the name of all those sub-categories with images near to it’s name like

example

parent1
  =>sub1 <Image of sub1 category>
  =>sub2 <Image of Sub2 category>
  =>sub3 <Image of sub3 category>

Same way i have parent2, parent3 etc., which contain sub-categories same way as i mention here.

So when i click on any parent category it will listed me sub-categories with it’s images!

If anybody having Idea about this then let me know how to do this.

Waiting for your response.

Thanks in advance.

Answer by Starx

You can get the image link using

$_imageUrl=$this->getCurrentCategory()->getImageUrl()

Follow this wiki from detailed walk-through.

Read more
October 13, 2012

Check whether an input string contains number

Question by Udara S.S Liyanage

I want to check whether an input string contains number.

I am trying to validate an input field. I want it to contain only alphabet, not numbers.

Answer by Starx

You can do this using javascript. No need for Jquery or Regex

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

While implementing

var val = $('yourinputelement').val();
if(isNumeric(val)) { alert('number'); } 
else { alert('not number'); }

Update: To check if a string has numbers in them, you can use regular expressions to do that

var matches = val.match(/d+/g);
if (matches != null) {
    alert('number');
}
Read more
October 11, 2012

How to use Insert into query for joomla 2.5?

Question by Durgesh Sonawane

Iam using query for joomla.

$query = "INSERT INTO '#__demo'( 'id', 'fname', 'mname', 'lname' ) VALUES ( '$val', '$post['fname']', '$post['Mname']', '$post['Lname']' );";

It is giving error

syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING 

Answer by Starx

There are two mistakes on your query.

  1. You haven’t escaped your quotes in $_POST values.

    '$post['fname']'
    //     ^ here and other places
    
  2. You are using single quotes ' to represent tables and field names.

     .. INTO '#__demo'( ..       
    //       ^ here and other places
    

Now after removing all such problems. You query becomes:

$query = "INSERT INTO `#__demo` ( `id`, `fname`, `mname`, `lname` ) VALUES ( '$val', '$post[fname]', '$post[Mname]', '$post[Lname]' );";
Read more

javascript / php > Is there a way to use a regular button to submit information and call a javascript function?

Question by Edna Ramirez

I’ve built a function that rotates a picture when you click a button. I would like to tell this function how many degrees I want it to rotate with a text field or something like that and also call a function that’s being executed in php that moves a stepper motor. Is there a way to do so? I’ve tried with a submit button but it doesn’t show the animation the way I want to.

Answer by Starx

Submit button are generally used to submit forms. Although you can use them too, its not recommend. You can simply use <button /> element. And you can pass the function parameter on the onclick event handler of the button.

<button id="rotate" onclick= "rotate('35')">Rotate</button>

If you want to get value of text field and send in the form.

<button id="rotate" onclick= "rotate(document.getElementById('textboxid').value)">Rotate</button>

Demo

However, calling a function being executed over PHP is a tricky part. There are different ways you can do that:

  • Submit the form to the PHP page and assign URI parameters like page.php?function=test
  • Direct to the PHP page with passing the values in the URI parameters similar to above.
  • Use AJAX request to send and receive data.
Read more
October 10, 2012

capture submit() method

Question by Steffen Maas

I have a form like:

<form method="POST" action="page.html" onsubmit="alert('foo'); // validate();" name="foobar">
...
</form>

and a link like:

<div onclick="document.foobar.submit();">click me</div>

How can I capture the submit() method and call my validate function?

JsBin example

Answer by Sushil

You can call method from html itself. modified jsbin. Instead of submitting form on click you can call submitHandler and pass form reference.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
  <script type="text/javascript">
    function validate(){

    }

    function submitHandler(form){
      validate();
      alert('foo');

}
    window.onload = function(){
        document.foobar.submit = submitHandler;
    }
</script>

</head>
<body>
    <form method="POST" action="page.html" onsubmit="submitHandler(this);" name="foobar">
      <input type="text" />
    </form>

    <div onclick="document.foobar.submit()">click me</div>
</body>    
</html>

Answer by Starx

Uniquely identify your form with an id

document.getElementById('formid').onsubmit = function() {
    // You have captured the submit method now do what you need
};

Demo

Read more

AJAX Internal server error

Question by user751828

I couldnt find out what is the error.

 <script type="text/javascript">
         $(document).ready(function () {
             $("#btnsumbit").click(function (e) {
                 e.preventDefault();
                 $.ajax({
                     type: 'POST',
                     data: '{"username":"' + $("input#txtuser").val() + '","password":"' + $("input#txtpwd").val() + '"}',
                     url: 'http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data',
                     contentType: 'application/json; charset=utf-8',
                     dataType: 'json',

                     success:
                  function (data, textStatus, XMLHttpRequest) {

                      var status = data.Status;
                      alert(data.d);




                  },
                     error:
                  function (XMLHttpRequest, textStatus, errorThrown) {
                      alert(textStatus);
                  }

                 });

             });
         });


    </script>

I am getting 500 internal server error.How to call this webservice.I have passes the method with the url.Thanks for any help…

Answer by Starx

First thing, the way you are sending is wrong, send it like this

data:  {

    "username": $("input#txtuser").val(),
    "password": $("input#txtpwd").val()

}

Next make sure, url: http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data is returning JSON output.

Read more
October 9, 2012

Positive integer data type in java

Question by user1724891

I want to make a code where I can only accept positive integers, how would you recommend to do this?

I was thinking to set the Constructor int mySddads = 0 then writing in the accessor an if condition that if the value entered was less than 0, then it returns a system print out saying error blah blah blah, but was there any better ideas to do this?

Answer by Starx

This should be very simple I think

public setPostiveInt(int i) {
   if(i > 0) {
       this.postiveInt = i; 
   } else {
      //handle error or throw exception
   }
}
Read more

Load php_oauth.dll into local server

Question by Nich

I’m facing problem to load the php_oauth.dll extension for my xampp. I downloaded the php_oauth.dll and added extension=php_oauth.dll in php.ini , but when i restart my apache, it cannot start the server anymore.

I’m using window 7 ,64bits.
Initially, I download the file from
http://downloads.php.net/pierre/

but i found that it was 32bits file , so I’m searching fo 64bit and found out
http://www.mediafire.com/php-win64-extensions

unfortunately,I still cannot load the oauth. Any solution to solve it? I look through many article, but it seems like dont have it, anyone can provide ,so that, others that using win 64bit can follow

Thank you!

Answer by Starx

This looks to be like a path problem. Make sure the extension is store in the correct extension directory.

The settings of this can be found inside php.ini as something like

extension_dir = C:phpextensions

By default this is ext folder inside PHP installation.

Or, follow the instruction from PHP manual while installing extensions, if you are not sure what you are doing.

Read more
October 8, 2012

MySQL run query inside a query

Question by Daniel O

I have a query that gets 5 lines of data like this example below

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}

I want to run a query inside each results like this below

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

but for some reason it’s only display the first line when I add the query inside it. I can seem to make it run all 5 queries.

Answer by user1109719

You change the value of your $query in your while loop.
Change the variable name to something different.

Ex:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

Answer by Starx

This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.

Explanation:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];

    //Using same $query does not affect that much
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    

    //But here you are overriding the previous result set of first query with a new result set
    $result = mysql_query($query) or die(mysql_error());
    //^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified

//..............

Solution 1:

Avoid using same variable names for inner result set

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    
    $sub_result = mysql_query($query) or die(mysql_error());
    // ^ Change this variable so that it does not overrides previous result set

Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)

SELECT 
    ref,user,id 
FROM 
    table t
INNER JOIN 
    anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Read more

Difference Between Variable Declared Inside For Loop and Outside Loop

Question by sagar

How does the jvm manage the memory for i and j? What is the difference between i and j other than their scope?

int i;
for(i=0;i<5;i++)
{
    System.out.println(i)
}
for(int j=0;j<5;j++)
{
    System.out.println(j)
}

Answer by Starx

Its basically the same thing. The only difference is the declaration happens before the loop in the first case and and in the second case it happens before the execution of the loop.

About JVM’s memory management, it is basically managed in the same way.

Read more
...

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