[PHP] Array en keys

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Zoolander
  • Registratie: Januari 2003
  • Laatst online: 23-11-2022

Zoolander

superslim!

Topicstarter
Zit met probleem:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

// validation class
class validate {

        // declare all required variables
        var $result = array();

        // call all required functions
        function check_($data) {

            if(array_key_exists("emptystring_", $data))
                $this->emptystring_($data["emptystring_"]);
            
            if(array_key_exists("passwordmatch_", $data))
                $this->passwordmatch_($data["passwordmatch_"]);
            
            if(array_key_exists("count_", $data))
                $this->count_($data["count_"]);
            
            if(array_key_exists("illegal_", $data))
                $this->illegal_($data["illegal_"]);

        }
                        
        // check for an empty string
        function emptystring_($empty) {

            if ($empty == "") 
                $this->result["emptystring_"] = "false";
                

        } 

        // check if the passwords match
        function passwordmatch_($passwords) {

            if ($passwords[0] != $passwords[1]) 
                $this->result["passwordmatch_"] = "false";
                
        } 

        // check if field contains an allowed amount of carachters
        function count_($count) {

            if (strlen($count[0]) < $count[1] || strlen($count[0]) > $count[2]) 
                $this->result["count_"] = "false";
                
        } 

        // check if field contains illegal characters
        function illegal_($illegal) {

            if(ereg('[^a-z0-9]', $illegal))
                $this->result["illegal_"] = "false";
            
        } 
                
} 

?>


en deze code om de class te testen.
Werkt goed zover, alleen als ik nu dit doe:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

// class test //

$_POST["username"] = "#%e";
$_POST["password"] = "";


// validate input with a class.
include ("..\extend\validate_class.php");
$validate = new Validate;

// check $_POST["username"]
$input = array($_POST["username"], $_POST["password"]);
$process = array("count_" => array($input[0], 5, 10), 
                 "emptystring_" => $input[0] , 
                 "passwordmatch_" => array($input[0], $input[1]), 
                 "illegal_" => $input[0]);

$validate->check_($process);

// see whats up in the array!
foreach ($validate->result as $output) {
   echo "$output. <br>";
}


print "<br><br>*******************************************************<br><br>";

while ($zorf = current($validate->result)) {
   if ($zorf == "false") {
       echo key($validate->result) . "<br>";
   }
   next($validate->result);
}


print "<br><br>*******************************************************<br><br>";


print_r(array_keys($validate->result, "false"));

?>


Dan werkt while-loop niet! Hij print niet de keys uit, terwijl
de arrays (gezien scriptje helemaal onderaan) toch echt wel keys hebben!

Weet iemand wat er aan de hand kan zijn?
Voor duidelijkheid: scriptje tussen de ******* print NIKS!
en dat hadden paar regels moeten zijn!

Dank dank!

mijn naam slaat nergens op, althans niet op mij :P


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Je hebt dit:
PHP:
1
  if ($zorf == "false")

Probeer eens:
PHP:
1
  if ($zorf == false)

of beter:
PHP:
1
  if (!$zorf)


Dan zou het moeten werken. Je bent trouwens vrij schreeuwerig met je uitroeptekens achter iedere zin, kun je in het vervolg beter anders aanpakken. ;)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Acties:
  • 0 Henk 'm!

  • Zoolander
  • Registratie: Januari 2003
  • Laatst online: 23-11-2022

Zoolander

superslim!

Topicstarter
Hee, bedankt voor je tip met de ! - tekens.
Die ga ik meenemen.

Ik heb het net opgelost, moest eerst een

code:
1
 reset($validate->result);


uitvoeren, omdat het bovenste scriptje de pointer naar het laatste element
heeft verplaatst.
Weer wat geleerd! <-- oops ;)

thanx

mijn naam slaat nergens op, althans niet op mij :P


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Ik zie heel veel mensen doen wat jij doet, iets in de trant van
PHP:
1
if (($foo == true) or ($bar == false))

Persoonlijk vind ik dit gewoon gore code, en het slaat nog nergens op ook. Hier volstaat iets als
PHP:
1
if ($foo or !$bar)

Zeg nou zelf, wat staat mooier?

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Acties:
  • 0 Henk 'm!

  • Dennis
  • Registratie: Februari 2001
  • Nu online
NMe84 schreef op 05 maart 2004 @ 00:29:
Zeg nou zelf, wat staat mooier?
Ja, het onderste staat mooier, maar het bovenste is veel makkelijker als je gaat debuggen.

Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Dat is je persoonlijke mening denk ik (net zoals mijn vorige post mijn eigen mening is). Debuggen hoeft echt niet makkelijker te zijn met die 'lange' methode, ik vind die korte zelfs veel leesbaarder, omdat er veel minder mee op haakjes gelet hoeft te worden. En als je de debugger van Zend gebruikt schijnt het allemaal nog makkelijker te gaan. (Wat dat betreft spreek ik niet uit ervaring maar van horen zeggen.)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.

Pagina: 1