...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
June 14, 2010

method vs function vs procedure vs class?

Question by 2easylogic

I know the basics of this methods,procedures,function and classes but i always confuse to differentiate among those in contrast of Object oriented programming so please can any body tell me the difference among those with simple examples ?

Answer by JUST MY correct OPINION

A class, in current, conventional OOP, is a collection of data (member variables) bound together with the functions/procedures that work on that data (member functions or methods). The class has no relationship to the other three terms aside from the fact that it “contains” (more properly “is associated with”) the latter.

The other three terms … well, it depends.

A function is a collection of computing statements. So is a procedure. In some very anal retentive languages, though, a function returns a value and a procedure doesn’t. In such languages procedures are generally used for their side effects (like I/O) while functions are used for calculations and tend to avoid side effects. (This is the usage I tend to favour. Yes, I am that anal retentive.)

Most languages are not that anal retentive, however, and as a result people will use the terms “function” and “procedure” interchangeably, preferring one to the other based on their background. (Modula-* programmers will tend to use “procedure” while C/C++/Java/whatever will tend to use “function”, for example.)

A method is just jargon for a function (or procedure) bound to a class. Indeed not all OOP languages use the term “method”. In a typical (but not universal!) implementation, methods have an implied first parameter (called things like this or self or the like) for accessing the containing class. This is not, as I said, universal. Some languages make that first parameter explicit (and thus allow to be named anything you’d like) while in still others there’s no magic first parameter at all.


Edited to add this example:

The following untested and uncompiled C++-like code should show you what kind of things are involved.

class MyClass
{
  int memberVariable;

  void setMemberVariableProcedure(int v)
  {
    memberVariable = v;
  }

  int getMemberVariableFunction()
  {
    return memberVariable;
  }
};

void plainOldProcedure(int stuff)
{
  cout << stuff;
}

int plainOldFunction(int stuff)
{
  return 2 * stuff;
}

In this code getMemberVariableProcedure and getMemberVariableFunction are both methods.

Answer by Starx

Procedures, function and methods are generally alike, they hold some processing statements.

The only differences I can think between these three and the places where they are used.

I mean ‘method’ are generally used to define functions inside a class, where several types of user access right like public, protected, private can be defined.

“Procedures”, are also function but they generally represent a series of function which needs to be carried out, upon the completion of one function or parallely with another.


Classes are collection of related attributes and methods. Attributes define the the object of the class where as the methods are the action done by or done on the class.

Hope, this was helpful

Read more

How to run Javascript code before document is completely loaded (using jQuery)

Question by eliza sahoo

I am sharing a tip with you all.Please add on to this discussion.

JQuery helps faster page load than javascript. JQuery functions are fired when the related elements are loaded, instead of complete pageload.
This is a common practice to call a javascript function when page is loaded like

window.onload = function(){ alert("Mindfire") }

or

<body onload="javascript:document.getElementById('user_id').focus();">

Inside of which is the code that we want to run right when the page is loaded. Problematically, however, the Javascript code isn’t run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is due to the fact that the HTML ‘document’ isn’t finished loading yet, when you first try to run your code.
To circumvent both problems, jQuery has a simple statement that checks the document and waits until it’s ready to be manipulated, known as the ready event

$(document).ready(function()

{
   // Your code here
 });

Answer by Starx

if you want to run a script before a document is ready then simply put inline script like

<div> .........................</div>
<div> another division </div>
<script>alert('hi');</script>
<div id="this">...................</div>
.
.
.
.
.
Read more

How to use jquery ui slider to create a pagination effect and load the content in a <DIV>?

Question by user366106

I want to create a pagination script using jquery UI’s slider widget. So far I have got the slider working but I dont know how to send request to the server to fetch new content.

So far this is my HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>jQuery UI Slider - Range slider</title>
    <link type="text/css" href="themes/base/jquery.ui.all.css" rel="stylesheet" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.ui.core.js"></script>
    <script type="text/javascript" src="jquery.ui.widget.js"></script>
    <script type="text/javascript" src="jquery.ui.mouse.js"></script>
    <script type="text/javascript" src="jquery.ui.slider.js"></script>      
    <style type="text/css">
        body { padding:5em; }
    </style>
    <script type="text/javascript">
    $(function() {
        $("#slider-range").slider({
            min: 1,
            max: 14,
            values: [1],
            slide: function(event, ui) {
                $(".values").html(ui.values[0];);
            }
        });
    });
    </script>
</head>
<body>
<div class="values" style="padding:2em;"></div>
<div id="slider-range"></div>

<div class="info" style="margin-top:2em; background:#CCC;padding:2em;">
Here is where the content will be displayed.
</div>

</body>

Thanks in Advance

Answer by Starx

Well, You can send a request at the slide event of your slider to send a request to the server at put the fetched data inside a div

  $("#slider-range").slider({
   min: 1,
   max: 14,
   values: [1],
   slide: function(event, ui) {
    var newPage = ui.values[0];
    $(".info").load("content.php", { page: newPage });
                                //load the content into a division
   }
  });

UPDATED
a sample of content.php

<?
     $recordsperpage = 15;
     if(!isset($_POST['page'] or empty($_POST['page']) { $page =1 ; }
     else { $page = $_POST['page']; }

     $limit = $page * $recordsperpage.",".$recordsperpage;
     $query = "SELECT * FROM yourtable LIMIT ".$limit;
     $result = mysql_query($query) or die(mysql_error());
     while($row = mysql_fetch_array($result)) {
          //Display the records in your pattern
     }
?>
?>
Read more
June 13, 2010

Opinions sought on the best way to organise classes in PHP

Question by jax

I am pretty much set on using the Java package naming convention of

com.website.app.whatever

but am unsure about the best way of doing this in PHP.

Option 1:

Create a directory structure

com/
    mysite/
          myapp/
               encryption/
                         PublicKeyGenerator.class.php
                         Cipher.class.php
               Xml/
                         XmlHelper.class.php
                         XmlResponse.class.php

Now this is how Java does it, it uses the folders to organize the heirarchy. This is however a little clumsy in PHP because there is no native support for it and when you move things around you break all includes.

Option 2

Name classes using a periods for the package, therefore names are just like in Java but can all be in the same directory making __autoload a breeze.

classes/
        com.mysite.myapp.encription.PublicKeyGenerator.class.php
        com.mysite.myapp.encription.Cipher.class.php
        com.mysite.myapp.xml.XmlHelper.class.php
        com.mysite.myapp.xml.XmlResponse.class.php

The only real disadvantage here is that all the classes are in a single folder which might be confusing for large projects.

Opinions sought, which is the best or are there other even better options?

Answer by allenskd

You could follow Zend Framework standards like

Option1 with autoload

new App_Encryption_Cipher

in autoload magic callback replace the _ to ‘/’ and do further checking (the file exists? garbage in the filename being seek? symbols?)

This is however a little clumsy in PHP because there is no native support for it and when >> you move things around you break all includes.

Depends on how you plan/design your application. there is no escape when it comes to refactoring anyway 🙂

I know you are used to java naming conventions, but if you do something like (new com_mysite_myapp_encryption_cypher) well, it kinda becomes a pain to write all the time

Answer by Starx

I would suggest Option 1, because in that layout in particular is the separation of codes, which will end up as a much manageable and flexible system.

Read more

How to export a mysql database using Command Prompt?

Question by Starx

I have a database it is quite larger so I wanted to export it using Command Prompt, but I dont know how to.

Please help
I am using WAMP

Answer by Starx

Ok,

First check if your DOS recognizes mysql command. If not go to command and type in

set path=c:wampbinmysqlmysql5.1.36bin

(I just made a guess, since you are using WAMP)

Then use this command to export your database

mysqldump -u youruser -p userpassword yourdatabase > wantedsqlfile.sql

This exports the database to the path you are currently in, while executing this command

Here is some detail instruction regarding both import and export

http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/#more-184

Read more
June 11, 2010

Database web application

Question by Watergaite

How would i go about creating a php application for my web page that can extract data from my database (i currently get the data in a CSV file). id also like the user to be able to filter the data by certain parameters. can u help

Answer by Starx

There are many ways to start with like

A language

1. Asp.net c#
2. PHP Mysql
3. JSP Oracle

A server

1. Apache
2. Windows

A Framwework (like explained in another answer by Dolph Mathews )

1. Ruby on Rails (Ruby)
2. CakePHP (PHP)
3. Grails (Java)
4. Seam (Java)
5. Spring MVC (Java)
6. Symphony (PHP)
7. Zend (PHP)

Since you want it in PHP, you have to use php mysql and apache server. You can use WAMP to install all three of this in one go.
http://www.wampserver.com/en/

With wamp, you will also get phpMyAdmin, through which you can manage your database, like create, delete, manipulate ………..

Coming to the coding part, that you are going to have to google

Here are some links of W3schools to get started with

http://www.w3schools.com/php/php_mysql_intro.asp

http://www.w3schools.com/php/php_mysql_connect.asp

http://www.w3schools.com/php/php_mysql_create.asp

http://www.w3schools.com/php/php_mysql_insert.asp

http://www.w3schools.com/php/php_mysql_select.asp

http://www.w3schools.com/php/php_mysql_where.asp

http://www.w3schools.com/php/php_mysql_order_by.asp

http://www.w3schools.com/php/php_mysql_update.asp

http://www.w3schools.com/php/php_mysql_delete.asp

http://www.w3schools.com/php/php_db_odbc.asp

Read more
June 10, 2010

Jquery show div on hover then when user hovers out of div hide it help?

Question by user342391

I have a menu and when I hover one of the links a div shows. I want the user to be able to hover over this div but when the user hovers out of the div (mouseout i think its called) I want it to hide.

Imagine a dropdown menu in css, the user hovers over the link and the sub nav is shown, when the user hovers out or away from the link and sub nav the sub nav dissapears. How can this be done with jquery???

this is what I have:

 $(document).ready(function(){
 //when user hovers over plans the mainnavbottom is shown
$(".plans").hover(
    function() {
    $(".mainnavbottom").show("fast");
  }, function(){
    $(".mainnavbottom").mouseout.hide("slow");
  });


 });

Answer by realshadow

Try something like this:

$('.mainnavbottom').bind('mouseenter mouseleave', function(event) {
    switch(event.type) {
        case 'mouseenter':
           // when user enters the div
           $(".mainnavbottom").show("fast");
        break;
        case 'mouseleave':
          // leaves
          $(".mainnavbottom").hide("slow");
        break;
    }
});

This code works particulary good if you want to append a div that has e.g. ajax loaded content, but it should very well work with your css menu.

Hope this helps

Answer by Starx

Try

 $(document).ready(function(){
      //when user hovers over plans the mainnavbottom is shown
      $(".plans").mouseover(function() {
           $(".mainnavbottom").show("fast");
      }).mouseout(function(){
           $(".mainnavbottom").hide("slow");
      });    
 });
Read more
June 9, 2010

Is it Possible to change the Default Margins of Browser through JavaScript?

Question by Govind KamalaPrakash Malviya

I want to change the default margins of Browser through JavaScript because I want to print the displayed document on page but the margins are different on different browser??? plz help me to change default margins of browser. if you have any solution tell me.

Answer by Jaroslav Záruba

I would recommend adding reset.css to your page, and probably to all of your pages/projects.
This way you should be able to eliminate all differencies in default style values amongst browsers.

EDIT: check out one of the most valuable threads at StackOverflow.com

Answer by Starx

use jquery

$(document).ready(function() {
       $("body").css("margin","0");
});

Using CSS

body { margin:0; }
Read more
June 8, 2010

Why is this mail going straight to SPAM box?

Question by Starx

I am using the following script to send mail

<?
extract($_POST);
$subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
$mail = @mail($send,$subject,$content);
if($mail) { echo "Your feedback has been sent"; }
else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>

But this is always ending up in the spam Folder. Why?

Answer by Starx

You have to use headers while you send mail, to prove that the mail arrives from a genuine source and not a bot.

Try this!

<?
  extract($_POST);
  $subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
  $headers  = 'MIME-Version: 1.0' . "rn";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
  $headers .= 'From:'.$email."rn";
  $headers .= 'Reply-To: '.$email;
  $mail = @mail($feedback,$subject,$content,$headers);
  if($mail) { echo "Your feedback is send"; }
  else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>
Read more

PHP and Permissions

Question by Moe

I recently moved my website to a new host and now am experiencing some broken code..

I have an uploading script that is now returning this:

move_uploaded_file() failed to open
stream: Permission denied in *..

I’ve set the upload directory to 777 which worked fine, but my script is needed to have top level permissions..

(As the script itself sets permission to directories, does lots of copying etc)

Is there a way in apache I can set the PHP script to the owner of all the folders on my server?

Thanks

Also
When looking in phpInfo()

Under
apache2handler

User/Group  nobody(99)/99 

Is this related?

Answer by Starx

well,
When you are trying to set the permission like “0777”, you must be running on same authority.

What I mean is.
For example, your script tells to change a folder/file permission to 0777, but the folder or file already has a permission and that is ‘0755’ so you are not authorised to make that change. as the user have only 5 authority.

Either, you need to login to FTP and change the folder permission to 0777 and then you have full control over it or you have to stick with using 0755 or similar.

Read more
...

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