Reading Programming

Jun 28, 2005 19:48 # 36826

ginsterbusch *** rants...

PHP trouble

?% | 1

Well, sooner or later I'm getting insane - in this case probably sooner: I'm trying to get this piece of code running, and it SHOULD work properly, but it DOES NOT. and I dont know why. Maybe somebody else than me has got some more clues than me.

So here we go:

init.php

// navigation / modes - syntax: mode => title
$page_navigation = array(
	'main' => 'Wunschkonfiguration',
	'address_data' => 'Adressdaten',
	'contract' => 'Vertragsdaten'
);

// check for mode and generate page title if needed
if(!$mode || empty($mode))
	$mode = 'main';

if(array_key_exists($mode, $page_navigation)) {
	$step = array_keys($page_navigation, $mode);
	echo "<!-- debug:\n";
	echo "mode = $mode, \n";
	print_r($step);
	echo "-->\n";
	$page_title = 'Schritt #' . $step[0] . ': ' . $page_navigation[$mode];
}

This script part should basically do the following: look up $mode and if it's in the array, first create a page title-string and then add the position where its key is to that string.
According to php.net, array_keys() should work that way, but my script always and ever returns only this:

<!-- debug:
mode = main, 
Array
(
)
-->

It's called from start, no other mode, just plainly included into the main file, and as you can see, $mode is automatically set to 'main' before executing the other code - but it STILL DOES NOT WORK. ARGH.

Sumbody hlp me plz!!!!!!!!!

cu, w0lf.

NP: Gorerotted - Pain As A Prelude To Death

Fuck off the 30 seconds posting limit!

This post was edited by ginsterbusch on Jun 28, 2005.

Jun 28, 2005 22:07 # 36829

Jaz *** replies...

Re: PHP trouble

This script part should basically do the following: Look up $mode and if it's in the array, first create a page title-string and then add the position where its key is to that string.
According to php.Net, array_keys() should work that way, but my script always and ever returns only this:

Array_keys($page_navigation, $mode) returns the (string) keys for all pairs in $page_navigation where the value is $mode. Because there is no value "main" anywhere in $page_navigation, you get back an empty array.

'Yeah, That's what Jesus would do. Jesus would bomb Afghanistan. Yeah.' - snowlion

This post was edited by Jaz on Jun 28, 2005.

Jun 28, 2005 23:27 # 36834

ginsterbusch *** agrees...

Re: PHP trouble

Array_keys($page_navigation, $mode) returns the (string) keys for all pairs in $page_navigation where the value is $mode. Because there is no value "main" anywhere in $page_navigation, you get back an empty array.

Damn. I knew I did something wrong. Anyway, it's always better if somebody else who is kind of neutral overlooking your stuff if you simply cant progress further. Thanks a lot, Jaz! ;)

The solution to my problem is to be found in following function I wrote after reading your post:

function findArrayKeyPosition($needle, $array) {
	$return = false; $n = 0;
	foreach($array as $key => $value) {
		if($key == $needle) {
			$return = $n;
			break;
		}
		$n++;
	}
	return $return;
}

This function indeed does find the key 'main' and returns its position inside the array, which is in this case '0'. ;)

cu, w0lf.

Fuck off the 30 seconds posting limit!

Jun 28, 2005 23:35 # 36835

Jaz *** replies...

Re: PHP trouble

?% | 1

This function indeed does find the key 'main' and returns its position inside the array, which is in this case '0'. ;)

But be careful - I'm not very familar with PHP, but in all languages I know the keys in an associative array are not guaranteed to be ordered in any particular way.

'Yeah, That's what Jesus would do. Jesus would bomb Afghanistan. Yeah.' - snowlion

Jul 01, 2005 02:10 # 36869

ginsterbusch *** replies...

Re: PHP trouble

?% | 1

But be careful - I'm not very familar with PHP, but in all languages I know the keys in an associative array are not guaranteed to be ordered in any particular way.

Well, in this case, they are. It's part of a so-called webspace / webhosting configurator, which allows you to individually configure the webhosting plan you are longing for.

You may see it live in action at some time between friday noon and saturday evening. Depends on how quick them folks get it online and integrated (also depends heavily on the whois check-feature I'm currently integrating into this script, too).

cu, w0lf.

Fuck off the 30 seconds posting limit!

Jul 01, 2005 03:28 # 36871

majic *** replies...

Re: PHP trouble

?% | 1

This is offtopic but after being absorbed into Ruby programming for about 2 months I find that PHP looks like total crap and reminds me of spaghetti, crusty old nasty spaghetti, at that!... =/

But I suppose it has it's good points as well. I did find PHP very easy compared with Perl. So that in and of itself is a win win situation! =)

But again I offer no help on your trouble, this has been yet another subjective post with no merit and value.

Jul 01, 2005 17:15 # 36889

Jaz *** replies...

Re: PHP trouble

This is offtopic but after being absorbed into Ruby programming for about 2 months I find that PHP looks like total crap and reminds me of spaghetti, crusty old nasty spaghetti, at that!... =/

Oh yes, Ruby does that to you. I even believe it makes you write better code because everything is already so neat and sexy that you wouldn't want to spoil the beauty.

'Yeah, That's what Jesus would do. Jesus would bomb Afghanistan. Yeah.' - snowlion

Jul 01, 2005 18:04 # 36890

majic *** throws in his two cents...

Re: PHP trouble

I kinda brushed Ruby off for a long long time. The I stumbled on Ruby on Rails and brushed that aside for a few months as well. The one day I desided to try out Python, a weel later I decided to really look at Ruby. Then I realized just how nice it was. I read a little about the language, tried some stuff out, started learning, It was fun! So I looked at Ruby's C interface for extending the language with C code and got the shock of my life. I had been attempting to wrap some of the X Windows System interface (Xlib) for quite a while in Java, then Mono C# and those went over okay but very very tough. I fell in love with Ruby's C Interface because it was so damn easy to pass data back and forth between the two languages. I now have a working Ruby Xlib layer that is fully capable of writing window managers as well as small Xlib applications. Incidentally I have been toying with Ruby on Rails as well and I really like it!

Jul 02, 2005 17:25 # 36909

ginsterbusch *** replies...

Re: PHP trouble

This is offtopic but after being absorbed into Ruby programming for about 2 months I find that PHP looks like total crap and reminds me of spaghetti, crusty old nasty spaghetti, at that!... =/

It depends on how you write your code, if you set yourself some standard rules. Since I've been trying to get into OOP again, its way more fun hacking code, and I'm again open-minded for other programming languages, too - like PERL or Python.

My personal rules for hacking well-structured, easily readable codes are:

a) Use tabulator insertions
b) Seperate difficult parts, like when doing some string manipulations and similar stuff.
c) comment difficult parts and maybe even put some structural overview into the header comment
d) start each file with a comment that tells you at least shortly what this (part of a) script is going to be used for
e) export difficult parts into seperate modules during development stage
f) re-integrate above mentioned modules into the main program/script when the product / script / work / whatever is reaching final stage / release stability
g) in PHP, use double quotes ("<!-- debug: $title\n -->") only if the content of the string should be parsed - ie. only when needed - else use single quotes ('string')

There are some more rules, but these are the ones I explicitly use in every piece of code I write. Take my initial post: I had to remove tab insertions, else it wouldnt have been displayed properly in NAO ;)

cu, w0lf.

Fuck off the 30 seconds posting limit!


Favorites (edit)

Small text Large text

Netalive Amp (Skin for Winamp)