It seems so easy....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

It seems so easy....

Post by Lord Sauron »

It seems so easy, but it simply doesn't work.

this is NEVER true:
IF ($itemArray[$i] == $itemValue) {

this is ALWAYS true:
IF ($itemArray[$i] = $itemValue) {

But, when I'm printing the values of the variables, it is "Mr" both times.
Why isn't the first comparison working as it should be?

Kind regards,

Antonie
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

== is comparing 2 value
= is assigning right value into left varible.

so the second one will always work as long as you defined itemvalue befor.

== will not work when the value are not the same
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Off course

Post by Lord Sauron »

Yeah, thanks Draco.

Those are the kind of advices I need :wink:

Let me rephrase the question:
How is it possible, that == isn't reacting, when echoing of both variables results in the same echo?

ECHO $itemArray[$i];
// -> Mr
ECHO $itemValue;
// -> Mr
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

want to post your whole code (relevent code) with

Code: Select all

tags plz
I'll be more able to help you (if I can )
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Code: Select all

<?php

// ....
// Creation of an array called $titel
// The array is like [Prof, Mr, Ir, Bac]
// ...

print("<SELECT class="selectBox" name="titel[]" SIZE="3" MULTIPLE>\n");
        
IF (count($titel) == 0) {
    print("                            <OPTION value="" SELECTED>Geen titel</OPTION>\n");
}
fillMultipleSelectList("Prof", $titel);
fillMultipleSelectList("Mr", $titel);
fillMultipleSelectList("Dr", $titel);
fillMultipleSelectList("Drs", $titel);
fillMultipleSelectList("Ir", $titel);
fillMultipleSelectList("Ing", $titel);
fillMultipleSelectList("Ms", $titel);
fillMultipleSelectList("Bac", $titel);

print("                        </SELECT>\n");


// FUNCTION fillMultipleSelectList
function fillMultipleSelectList($itemWaarde, $itemArray) {

    $i= 0;	
    WHILE ($i < count($itemArray)) {
						
        IF ($itemArray[$i] == $itemWaarde) {
            
             // IT NEVER ENTERS THIS IF!				
            print("<OPTION VALUE="$itemWaarde" SELECTED>$itemWaarde</OPTION>\n");

            $itemIsGeselecteerd = "Ja";
            break;
        }
            
        $i++;
    }
	
    IF ($itemIsGeselecteerd != "Ja") {      	
        print("<OPTION VALUE="$itemWaarde">$itemWaarde</OPTION>\n");

    }

} Close fillMultipleSelectList-function

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

where's $titel defined?
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

In the ...

// ....
// Creation of an array called $titel
// The array is like [Prof, Mr, Ir, Bac]
// ...

... part :wink:

I assure you the array is filled correctly. I hope you don't want me to post the complete code. It's about 200.000 lines of code.

Antonie
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could you post part of how $titel is defined? I fail to see a problem in the code itself thus far..
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Are you sure....
suppose 'gebruikerID' = 1
'gebruikerID' is Dutch for userID.

Code: Select all

<?php

$behandelaar    = new Behandelaar();
$behandelaar->loadBehandelaarMetGebruikerID($_SESSION['gebruikerID']);
$titel          = $behandelaar->getTitel();


// FUNCTION loadBehandelaarMetGebruikerID
function loadBehandelaarMetGebruikerID($uID) {

    $behandelaar = voerQueryUit("SELECT * FROM Behandelaar WHERE userID=".$uID.";", "Database kan behandelaar niet laden."); 
    $behandelaarData = vraagAssociatieveRijOp($behandelaar);

    IF ($behandelaarData != null) {
        $this->ID = $behandelaarData['behandelaarID'];
        $this->titel = $behandelaarData['titel'];
    }

}


// FUNCTION voerQueryUIT - perform Query
function voerQueryUit($queryTekst,$queryFoutmelding) {

    $mysqlConnectie = maakDatabaseConnectie();
    $resultaat = mysql_query($queryTekst, $mysqlConnectie);
    if (!$resultaat) {
        die ("<B>".$queryFoutmelding."</B>");
    }

    $GLOBALS["LAATSTE_AUTO_INCREMENT_WAARDE"] = mysql_insert_id($mysqlConnectie);

    sluitDatabaseConnectie($mysqlConnectie);

    return $resultaat;
}

// FUNCTION vraagAssociatieveRijOp
function vraagAssociatieveRijOp($resultaatData) {

    $mysqlConnectie = maakDatabaseConnectie();
    $assocRij = mysql_fetch_assoc($resultaatData);
    sluitDatabaseConnectie($mysqlConnectie);

    return $assocRij;
}


?>
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Perhaps this will help:

print_r($titel) will result in

Array ( [0] => Prof [1] => Mr [2] => Bac)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ok.. so $titel is coming directly from the database without modification, basically.. So then, how does $titel show up as an array? it looks more like it's a string result of a print_r.. :? try var_export($titel), just for kicks..
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Mmm, true. Must have forgotten one line. Otherwise it couldn't print Array ( [0] => Prof [1] => Mr [2] => Bac)

Let's have a look....ah yes, there it is

Code: Select all

<?php

// ....

$behandelaar    = new Behandelaar(); 
$behandelaar->loadBehandelaarMetGebruikerID($_SESSION['gebruikerID']); 
$titel          = $behandelaar->getTitel(); 
$titel = explode(";", $titel);

// .... etc

?>
Sorry, but it isn't that simple.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. that makes more sense.. hmmm

Code: Select all

<?php

$titel = array('Prof','Mr','Ir','Bac');

function fillMultipleSelectList($itemWaarde, $itemArray) {

    $i= 0;   
    WHILE ($i < count($itemArray)) {

        IF ($itemArray[$i] == $itemWaarde) {
           
             // IT NEVER ENTERS THIS IF!            
            print("<OPTION VALUE="$itemWaarde" SELECTED>$itemWaarde</OPTION>\n");

            $itemIsGeselecteerd = "Ja";
            break;
        }
           
        $i++;
    }
   
    IF ($itemIsGeselecteerd != "Ja") {         
        print("<OPTION VALUE="$itemWaarde">$itemWaarde</OPTION>\n");

    }

}

fillMultipleSelectList("Prof", $titel);
fillMultipleSelectList("Mr", $titel);
fillMultipleSelectList("Dr", $titel);
fillMultipleSelectList("Drs", $titel);
fillMultipleSelectList("Ir", $titel);
fillMultipleSelectList("Ing", $titel);
fillMultipleSelectList("Ms", $titel);
fillMultipleSelectList("Bac", $titel); 

?>
outputs

Code: Select all

&lt;OPTION VALUE="Prof" SELECTED&gt;Prof&lt;/OPTION&gt;
&lt;OPTION VALUE="Mr" SELECTED&gt;Mr&lt;/OPTION&gt;
&lt;OPTION VALUE="Dr"&gt;Dr&lt;/OPTION&gt;
&lt;OPTION VALUE="Drs"&gt;Drs&lt;/OPTION&gt;
&lt;OPTION VALUE="Ir" SELECTED&gt;Ir&lt;/OPTION&gt;
&lt;OPTION VALUE="Ing"&gt;Ing&lt;/OPTION&gt;
&lt;OPTION VALUE="Ms"&gt;Ms&lt;/OPTION&gt;
&lt;OPTION VALUE="Bac" SELECTED&gt;Bac&lt;/OPTION&gt;
maybe you have some spaces in your array? try var_dump($titel) to see...
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Do you understand what I am trying to do with this code (off course it is part of a much larger code)?

I'm having many multiple select boxes, like this select box for the title. The problem is the same for all select boxes, because I am using the fillMultipleSelect-function. Selecting several items from a list is not a problem and recording these items (as a string) in the database neither. Retrieving the string-value from the database is no problem either, nor is converting this string back to an array. The problem is, that I want to be able to change my selection. So, if I am accessing this form again, the values in the array should all be shown as selected in the select box.

But it only selects the first value of the array. "Prof"
The second time I'm calling the fillMultipleSelect-function, the IF-comparison simple refuses to work. I don't understand why. If I'm echoing the values of my array, it all seems okay.

Antonie
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

wow, you truly did understand it ;-)

Let's see what var_dump does.

WOW, thanks
array(2) { [0]=> string(4) "Prof" [1]=> string(3) " Mr" }


There is a space before Mr.

Damn, this truly sucks. :cry:

Antonie
Post Reply