iThaos 0 Light Poster

Ah yes that was the problem.
Thank you so much :)

I did not know that it took a reference and not a copy.

iThaos 0 Light Poster

Hi everyone,

I'm trying to make a little applet that reads text files. I was planning to load some data into a class and use it later. However, I seem to having a few problems with it.

// DataSet.java
package com.someone.something;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DataSet {

    private String[] header;
    private List<String[]> data;
    private String[] tempArray;
    private int cCounter = 0;
    private int rCounter = 0;

    private String[][] finishedData;

    private boolean finished = false;

    public DataSet(int columns){
        data = new ArrayList<String[]>();
        tempArray = new String[columns];
        Arrays.fill(tempArray,"");
    }

    public void nextRow(){  
        data.add(rCounter,tempArray);   // Seems that this is the problem

        Arrays.fill(tempArray,"");
        cCounter = 0;
        rCounter++; 
    }

    public void add(String k){
        tempArray[cCounter] = k;
        cCounter++;
        finished = false;
    }

    public String[][] getData(){
        if(!finished){
            finish();
        }
        return finishedData;
    }

    public void finish(){
        finishedData = data.toArray(new String[data.size()][]);
        finished = true;
    }

}

This is the part that uses the DataSet class:

// Inside of the main class

DataSet dataSet;
String[][] data;
ArrayList<String[]> dataList;

dataList = new ArrayList<String[]>();

String[] a = {"aaa","aab","aac","aad","aae"};
String[] b = {"bbb","bba","bbc","bbd","bbe"};
String[] c = {"ccc","cca","ccb","ccd","cce"};

dataSet = new DataSet(5);

for(String value : a){
    dataSet.add(value);
}
dataSet.nextRow();
for(String value : b){
    dataSet.add(value);
}
dataSet.nextRow();
for(String value : c){
    dataSet.add(value);
}

dataSet.nextRow();

data = dataSet.getData();

String bufferedString = "";
data = dataSet.getData();
for(String[] rows : data){
    bufferedString = "";
    for(int i = 0; rows.length > i; i++){

        bufferedString += rows[i];
        if(rows.length - 1 != i){
            bufferedString += "\t";
        }

    }
    System.out.println(bufferedString);
}

When I run …

iThaos 0 Light Poster

Oh okay I didn't realise there was the sticky :/

Sorry

Thanks anyway

iThaos 0 Light Poster

Hello fellow developers,

Let me just start by saying I have had an abnormal start to developing. I started first with HTML > PHP > Java.

I don't have much experience with Java but I feel quite confident in making PHP applications. I have already searched the forums but nobody seems to have this pattern.

Can anybody recommend me a good book to get me started? (I am developing specifically for android.)

iThaos 0 Light Poster

Do you understand the purpose of object inheritance?
Usually when using inheritance, if the child class had it's own constructor, you would call the parent constructor from within the child constructor.

I guess I don't. I saw a child class as an extension of the parent class such that the child would just provide extra functionality. I guess that isn't what I needed in my application (not the one in the example).

Thanks

iThaos 0 Light Poster

Hi,

I was wondering whether it is possible to use a parent class' properties without calling its constructor. In the following example:

<?php
class Shop {    
    public $location;   
    public function __construct($location = ''){       
        if(!empty($location)){
            $this->location = $location;
        } else {
            $this->location = 'a place far far away';
        }
        $this->location .= '. ';        
        echo 'Shop constructor has been called. ---- ';        
    }    
    public function check_stock($item){        
        echo 'There are still quite a few '. $item.'s remaining.';        
    }    
}

class Item extends Shop {    
    //public function __construct(){}   
    public function find($item){        
        echo $item . ' can be found at '.$this->location;
        $this->check_stock('Bed');       
    }    
}

$shop = new Shop('Carreyville');
$item = new Item;
$item->find('Bed');

// output: Shop constructor has been called. ---- Shop constructor has been called. ---- Bed can be found at a place far far away. There are still quite a few Beds remaining.

?>

In this script, it can be seen that the constructor has been called twice, since the child is called the parent construct again after the parent had already been instantiated. However, if the child doesn't call the parent constructor, the parent's properties cannot be accessed.

<?php 
class Item extends Shop {    
    public function __construct(){}   
    public function find($item){        
        echo $item . ' can be found at '.$this->location;
        $this->check_stock('Bed');       
    }    
}

// output: Shop constructor has been called. ---- Bed can be found at There are still quite a few Beds remaining.
?>

Is there anyway to access the parent properties without having to call the parent's constructor again since …

iThaos 0 Light Poster

*10 Day Old Bump* (I'll delete this post)

iThaos 0 Light Poster

No, I didn't use defer because when I was playing around with it, Chrome just ignored it, Firefox used it and Opera didn't need it.

What do you mean by "doesn't respond"?

I know that the event is attached. At least, it returns true when I use attachEvent() and the DOM viewers don't show these event listeners. ( addEventListener() doesn't have any return values unfortunately.)

iThaos 0 Light Poster

LOL. Good catch on the typo. That will teach me not to clean up the variable names after testing - or at least to test again :(

DW Things like that happen all the time. Anyway, I'm looking for a solution to my problem (current on http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html and http://davidwalsh.name/image-load-event) but failing to find a solution :(

I've tried setting the src after inserting the element.

iThaos 0 Light Poster

Arrg hopefully this is the final problem.

IE doesn't respond to addEvent(document.getElementById(id),'load',loadRespond) or s.setAttribute('onload','loadRespond')

function addEvent(oElm, sTyp, fRef, bCapture) { // sTyp = 'name' [e.g., 'click']
	if (window.addEventListener) { // all others
		return oElm.addEventListener(sTyp, fRef, bCapture || false);
	} else if (window.attachEvent) { // IE
		return oElm.attachEvent('on' + sTyp, fRef);
	} else return false;
}

function loadJsFile(pathToFile){
	var e = document.getElementsByTagName('script')[0];
	var s = document.createElement('script');
	var id = 'js' + loadJsId;
	s.src = pathToFile;
	s.type = 'text/javascript';
	s.id = id;
	e.parentNode.insertBefore(s,e);
	addEvent(document.getElementById(id),'load',loadRespond);
	loadJsId++;
}
iThaos 0 Light Poster

Wow thanks :)

BTW: Line 6 I think should be return oElm.attachEvent('on' + sTyp, fRef); I guess this thread is solved then.

Thank you for all your help.

iThaos 0 Light Poster

There is considerable debate on that point, especially because the alternative usually shown is feature testing, completely misapplied. In fact, your code has a perfect example of that. You are evidently assuming that this

if (document.all && !window.opera)

will be true only for IE; that is not correct.

hmm I see. Lol the thing is at this point in time, I'm caring too much about IE so I just copy+pasted that in from somewhere. I think I'll try and replace it with your code. It seems pretty stable as long as the user doesn't disable user-agent identification or something.

What functions? In what way?

Functions like element.addEventListener and some other ones that I can't really find at the moment. The thing is I will leave the IE part out and just jquery and/or another set script or just adding additional browser specific code onto the original set.

IE FTL >=(

iThaos 0 Light Poster

Looks good but it seems too focussed on browser identification. As I can recall reading somewhere, browser identification should only be used when nothing else works.

This time I actually understand your script :) A week of coding really helps :D

This script seems to be a load better than mine. I gave up and came up with this

if (document.addEventListener){
	document.addEventListener('DOMContentLoaded', loadFiles, false);
	document.addEventListener('load',loadFiles,false);
}
else if (document.all && !window.opera){

	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
	var contentloadtag=document.getElementById("contentloadtag");
	contentloadtag.onreadystatechange=function(){
		if (this.readyState=="complete"){
			loadFiles();
		}
	}
}
else if(document.attachEvent){
	document.attachEvent('onload',loadFiles);
}

then complimented it with the following. Note the s.setAttribute('onload','loadRespond()');

function loadJsFile(pathToFile){
	var e = document.getElementsByTagName('script')[0];
	var s = document.createElement('script');
	s.src = pathToFile;
	s.type = 'text/javascript';
	s.setAttribute('onload','loadRespond()');
	e.parentNode.insertBefore(s,e);
}

I finally got it working on all browsers:)although the functions that follow it do not support IE. :(
Hate IE now. It was never a problem with PHP but IE needs its own functions for everything. It's like another different language I have to learn.

Anyway, if you can point out any flaws or improvements that can be made, that'll be good. :)

BTW: +1 on your posts :D

iThaos 0 Light Poster

I don't want to make a new thread on some other forum so I think I'll bump this one. :(
Hope someone is here to help me out ;)

iThaos 0 Light Poster

Thanks for the reply.

I'm not sure how to use the readystatechange or the load them into the body. :S

iThaos 0 Light Poster

Hi everyone.

I've run into another problem :(

I have 3 JS files I want to include in my page. The first one is defined statically in the html file. The other two are loaded into the DOM using the following function:

function loadJsFile(pathToFile){
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = pathToFile;
	headID.appendChild(newScript);
}

I then use DOMContentLoaded or onload events to trigger my initiation function combining functions from the 3 files. This works perfectly in Opera, firefox and (I assume) Internet Explorer but doesn't work in Safari or Chrome. For those two browsers, I have resorted to using the onmouseover event.

I then tried and loaded the 3 files statically and that worked.

Any ideas?

btw: I'm a just a beginner to JS

iThaos 0 Light Poster

These notes refer to my 'toy', above.

Without lines 1-2 (or an equivalent declaration that invokes 'standards mode') line 9 [from witerat] won't work in Firefox and line 26 won't work in IE.

Line 30 causes all onclick events to be handled by the javascript function named setBack , so it will not be necessary to put an onclick= action on each element.

Lines 32-40 are the function.

Lines 33-34 are for browser independence:
in IE, [window.]event is defined and its .srcElement property points to the originating element; e [the formal parameter] will be 'undefined' at run time
in all others, a reference to the event is passed in as the calling parameter and its .target property points to the originating element; [window.]event will be 'undefined' at run time

Line 35 limits the operation to 'TD' elements [and could be made much more specific if required by the situation]; since all onclick events are going to pass through this function, it is necessary to avoid doing anything on events from "other" elements.

Small note: I chose the variable name 'that' deliberately; its sense is the opposite of 'this' (the keyword that would be used on "the other side" [in an on___= action]).

mhmm thanks for the explanation. I guess this technique is a lot more advance than the earlier one but I think I'll keep this example in mind for when I need it again.

Thanks again :)

iThaos 0 Light Poster

That won't work in IE8. Sloppy of me :D

document.getElementsByTagName('BODY')[0].onclick = setBack;

works in 'all' browsers.

This little toy

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <style type="text/css">
	.other { background-color: silver; }
	.other:hover { background-color: aqua; }
    </style>
    <title></title>
  </head>
  <body>
    <div class='other'>
      Firefox must not be in 'quirks mode' for this to work as coded.
    </div>
    <table>
      <tr>
        <td class='other' height="100" width="100"></td>
        <td class='cold' style='background-color:blue' height="100" width="100"></td>
        <td class='warm' style='background-color:pink' height="100" width="100"></td>
        <td class='hot' style='background-color:maroon; color:white' height="100" width="100">
          IE must be in 'standards mode' for this to work as coded.
        </td>
      </tr>
    </table>

    <script type="text/javascript">

      document.getElementsByTagName('BODY')[0].onclick = setBack;

      function setBack(e) {
        e = e || event;
        var that = e.srcElement || e.target;
        if (that.tagName == 'TD') {
            if (that.getAttribute('class') == 'hot') {
                that.style.backgroundColor = 'red'
            }
        }
      }

    </script>
  </body>
</html>

combines both approaches.

Where CSS :hover is supported, it is clearly superior to javascript onmouseover= [thank you for the reminder, witerat :)].
In all other cases, javascript is necessary.

Looks nice. Could you please explain each part though? Thanks for all you help

iThaos 0 Light Poster

This

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">

window.onmouseover = setBack;

function setBack(e) {
    e = e || event;
    var that = e.srcElement || e.target;
    if (that.tagName == 'TD') {
        if (that.getAttribute('class') == 'hot') {
            that.style.backgroundColor = 'red'
        }
    }
}

    </script>
    <title></title>
  </head>
  <body>
    <table>
      <tr>
        <td class='cold' style='background-color:blue'   height="100" width="100"></td>
        <td class='warm' style='background-color:pink'   height="100" width="100"></td>
        <td class='hot'  style='background-color:maroon' height="100" width="100"></td>
      </tr>
    </table>
  </body>
</html>

does what scrappedcola did in a way that you may [or may not :)] find useful.

I've looked at both sets of codes and I think I'll go with scrappedcola's one because your one is a lot more advanced (IMO). :)

Thanks everyone :D

iThaos 0 Light Poster

Best to use cascading style sheets for this particular feature.

<style type="text/css">
.MyElement {
 background-color: #861786; /*whatever the normal background should be, optional*/
}
.MyElement:hover {
 background-color: #687168; /* background during mouse roll-over*/
}
</style>

// ....
<div id='my_div' class='MyElement'>My Div</div>

The advantage with CSS is that it still works when javascript is turned off.
If you really need to use javascript:

var element=document.getElementById('my_div');
element.addEventListener('mouseover',function () {
	this.style.backgroundColor = '#687168';
},false)

Of course you will also need to set up and event handler for mouse-out events too.

Thanks for the reply. The onmouseover() was actually just an example because I actually wanted to use onchange() . I appreciate the comment though.

iThaos 0 Light Poster

Thank you very much :)

I'll give those a try. I'll come back with results. :)

iThaos 0 Light Poster

Hi,

I have pretty just started javascript and I have to say it is the hardest language to learn probably due to the lack of official documentation or something like that. If someone can point me in the right direction for documentation, that'll be great. On the other hand, php is so easy to learn because of the official documentation and tutorial sites.

So, here's the thing. I have a multiple elements that are in the same class. How can I get those elements to respond to an event without actually having to with onmouseover="this.style.background = '#687168';" or something for every single tag. I don't want to put them in with php because it is just messy.

iThaos 0 Light Poster

First +1 for both Netbeans & NPP. NPP+ Explorer plugin make good editor for simple project. As project gets bigger NB is a way to go.
As per server, NB will ask for place to put files (point to www equivalent of WAMP) and Address (localhost/yourproject). So I guess it will work with any server (as it doesn't know server types only places files are and URI)

Yes, I guess everyone is going for Netbeans. Guess it wins. :)

*cough* vim/gvim/macvim > all *cough* unfortunately : [img]http://www.terminally-incoherent.com/blog/wp-content/uploads/2006/08/curves.jpg[/img]

LMAO! That made my day. :D

iThaos 0 Light Poster

Thanks for all your opinions then. I'll get Netbean then :)

@cwarn23: I've done it before myself but thanks for the offer. Can I not use it with IIS though?

iThaos 0 Light Poster

IMO if you are going to program then it is best to do it right. That means not using gui software to program for you. Usually it is best to just have a syntax highlighter like notepad++ or if your really inexperienced then a debugger like Netbeans but never drag and drop commands. That's my opinion anyways. Other than notepad++ and Netbeans I haven't heard of many other popular programs or none that I can think of. Enjoy.

Well I don't really get GUI to do it for me. The software just tells me where I'm wrong. I'm not inexperienced. I started coding 2 years ago. I used Notepad back then but I found Notepad++ pretty good but phpDesigner was the best.

I've never used drag and drop commands.

The things really need in an IDE would probably be

  • Intelligent syntax highlighting, switch automatic between PHP, HTML, CSS and JavaScript
  • Code explorer for PHP (support for includes, classes, extended classes, interfaces, properties, functions, constants and variables)
  • Go to any declarations (classes, functions, variables, interfaces etc.) declared in your files, projects or frameworks
  • Code tip for PHP (helps you completing your functions as you type)

Yeah.. it's copied from their page but anyway, those features are pretty much what I'm looking for.

...And I wouldn't use Dreamweaver even if it were free. Just to let you know.

I guess I'll try Netbeans then.

iThaos 0 Light Poster

I've been away from web coding for some time but I feel like coding PHP again. I was phpDesigner a lot and I got use to it. Its features are great but the trial ran out. Can anyone recommend a free PHP IDE that has all or most of the features in phpDesigner?

Thanks.

I'm on Windows btw.

EDIT: Also, what are the disadvantages of using Fast CGI on IIS 7.5 as opposed to ISAPI?

iThaos 0 Light Poster

Oh I see. Anyway, I seemed to have solved my problem even though not the the thread itself.

Also, there was a mistake in my previous function.

<?php
function conf($variable){
	
	if(function_exists($variable)){
		return $variable();
	}
	else{
		if(isset($GLOBALS['config'][$variable])){
			$func = 'return \'' . $GLOBALS['config'][$variable] . '\';';
			$variable = create_function('',$func);
			return $variable();
		}
		else{
			return false;
		}
	}
}
?>
iThaos 0 Light Poster

Well, the thing is that I don't want to create an object for that. This is probably what I want to do.

<?php
function conf($variable){
	
	if(function_exists($variable)){
		return $variable();
	}
	else{
		if(isset($GLOBALS['config'][$variable])){
			$func = 'static $varible;
			$variable = '.$GLOBALS['config'][$variable].'
			return $variable;
			';
			create_function('',$func);
			return $variable();
		}
		else{
			return false;
		}
	}
}
?>

OR

<?php
conf($variable){
// OMG you can't use static variable variables....
}
?>

Oh well, guess I'm left with 2 options.
I'll consider your object method. Thanks.

Off topic: And why do random people come alone and give every thread starter a negative rep?

iThaos 0 Light Poster

Yeah thanks.

I've already seen the create_function(); but there doesn't seem to be a way to catch an undefined function call. The point of this was to create the function when called.

It doesn't matter. I've decided to go with conf($variable) instead of $variable() .

Or should I create all those functions manually?

iThaos 0 Light Poster

Thanks.

Yeah, I've tried that before. It's not really what I want because I want to call the functions as they are. Anyway, I think I'll do thing the is slow way.

Is there any way to catch function call that doesn't exist outside of an object?

iThaos 0 Light Poster

Why would you use echo? That is PHP
If it is a php script, I'd do

//...Bunch of php code
?>
<!-- .... All that HTML here -->
<?php

I wouldn't go into the trouble of echoing all that.

EDIT: Oh wait I see.
You really should do what I said but when it comes to the variables, do ....<?=$var;?>....

iThaos 0 Light Poster

I do all my PHP scripting in phpDesigner. It's features are priceless to me. Well... kinda

iThaos 0 Light Poster

Hello,

Is there a way to create functions dynamically in PHP as such:

<?php

$my_arr_conf = array(
'my_var' => 'This is my variable',
'your_var' => 'This is your variable'
);

// Some code here where I can create the function
// Maybe somehow with create_function(); ?
// I was hoping for something like overloading for procedural
// Just one function that would replace the following 2.

function my_var(){
static $value;
if(isset($value)){
return $value;
}
else{
$value = $my_arr_conf[__FUNCTION__];
return $value;
}
}

function you_var(){
static $value;
if(isset($value)){
return $value;
}
else{
$value = $my_arr_conf[__FUNCTION__];
return $value;
}
}

echo my_var();
echo your_var();

?>

Any help would be appreciated.

Thanks

iThaos 0 Light Poster

For those who are wondering: this is how I implemented it my previous code:

<?php

class One {
	
	public function get_me(){
		echo 'Hello from class One. ';
	}
	
}

class Two {
	
	public function call_me(){
		echo 'Hello from class Two. ';
	}
	
}

class Three {
	
	public function leave_me(){
		echo 'Hello from class Three. ';
	}
	
}

class Core {
	
	public $one;
	public $two;
	public $three;
	
	public function __construct(){
		$this->one = new One;
		$this->two = new Two;
		$this->three = new Three;
	}
	
	public function __call($name,$arguments){
		$obj = array('one','two','three'); // Array of object names. Important ones first. Just in case.
		foreach($obj as $object){
			if(method_exists($object,$name)){
				return($this->$object->$name(implode(',',$arguments))); // Send over to the object.
			}
		}
	}
	
	public function get_me(){
		echo 'You can\'t get through to get_me class One.';
	}
	
}

$core = new Core;
$core->get_me();
$core->call_me();
$core->leave_me();

?>

Reading some other articles, however, some people used call_user_func() and call_user_func_array() instead. This technique, I have read is slow. I'm not sure however of the speed compared to $this->$object->$name(implode(',',$arguments)) .

iThaos 0 Light Poster

Looks cool. Thanks for you help :D

iThaos 0 Light Poster

Hey thanks for reading this.

Say I have 3 classes and I want to combine those class into a core.
Example

<?php

class One {
    
    public function get_me(){
        echo 'Hello from class One. ';
    }
    
}

class Two {
    
    public function call_me(){
        echo 'Hello from class Two. ';
    }
    
}

class Three {
    
    public function leave_me(){
        echo 'Hello from class Three. ';
    }
    
}

class Core {
    
    public $one;
    public $two;
    public $three;
    
    public function __construct(){
        $this->one = new One;
        $this->two = new Two;
        $this->three = new Three;
    }
    
}

$core = new Core;
$core->one->get_me();
$core->two->call_me();
$core->three->leave_me();

?>

This outputs

Hello from class One. Hello from class Two. Hello from class Three.

Normally all these classes would be in different files but you get the point.
I want to know if there is a way to use class One and class Two and class Three 's methods in class Core as if it was it's own so I can use $code->get_me(); instead of $code->one->get_me(); without using class Core extends One because I want to extend them all.

Any help?

iThaos 0 Light Poster

You really shouldn't use $_REQUEST;
1. You don't know if it comes from $_GET or $_POST when you really only want it from $_POST. Mostly to do with security.
2. Depending on the server settings, PHP may register one over the other so may be using a parameter that the user entered into the url.

Example: http://www.example.com/?user_auth=true when you really posted $_POST['user_auth]=FALSE; . Really bad example but that is the main idea.

iThaos 0 Light Poster

Thanks for the reply.

The error $public was created when I was rushing in notepad (no syntax highlighting) to make this example. Thanks however for correcting me.

Anyway, thanks for that response. I think that you might have mistook my question. I wanted to know how to use a child's (in this case Son's) methods or variables. None the less, I'll try out Son::message; or Son::$message; Thanks for the response. You are the first to respond to any of my threads.

iThaos 0 Light Poster

Here's a simple test. Go into a cemetery on the night of Halloween. Stay there for one hour. If you don't see something moving there, that means ghosts don't exists. If you do, chase after it. :)

iThaos 0 Light Poster

This may sound a bit newby but I was wondering if you can use methods and properties from a child object

class Father{
	function Father(){
		// echo the child's $message
	}	
}

class Son extends Father{
	$public $message = 'We\'re in the same family!';
}

If you know what I mean, is there any way to do it?

iThaos 0 Light Poster

I was reading how having globals inside a method was not good.
Something like this.

class one{
	function one(){
		return "Returned one";
	}
}

$ones = new one;

class two{
	
	public $one;
	
	function two(){
		$this->tree();
	}
	
	function tree(){
		global $ones;
		echo $ones->one();
	}	
}

$callme = new two;

That worked well but is not a good way to go.

Now, I did something like this.

class one{
	function one(){
		return "returned one";
	}
}

class two{
	
	public $one;
	
	function two(){
		$this->one = new one;
		$this->tree();
	}
	
	function tree(){
		echo $this->one->one();
	}	
}

$callme = new two;

Hey! Who knew that $this->class->method(); would work. (Well maybe most of you but I didn't.)

Anyway, is this is a good/standard practice though?

iThaos 0 Light Poster

Heya everyone.

You can call me iThaos. :D
I'm currently in high school and in my spare time I usually, do some php scripting. I'm planning on learning C++/C# after I am at an intermediate level in PHP.

It's nice to be here :D