Page 1 of 2
It seems so easy....
Posted: Wed Aug 18, 2004 2:11 pm
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
Posted: Wed Aug 18, 2004 2:27 pm
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
Off course
Posted: Thu Aug 19, 2004 9:09 am
by Lord Sauron
Yeah, thanks Draco.
Those are the kind of advices I need
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
Posted: Thu Aug 19, 2004 9:15 am
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 )
Posted: Thu Aug 19, 2004 10:16 am
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
?>
Posted: Thu Aug 19, 2004 10:28 am
by feyd
where's $titel defined?
Posted: Thu Aug 19, 2004 10:41 am
by Lord Sauron
In the ...
// ....
// Creation of an array called $titel
// The array is like [Prof, Mr, Ir, Bac]
// ...
... part
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
Posted: Thu Aug 19, 2004 11:12 am
by feyd
could you post part of how $titel is defined? I fail to see a problem in the code itself thus far..
Posted: Thu Aug 19, 2004 11:25 am
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;
}
?>
Posted: Thu Aug 19, 2004 11:34 am
by Lord Sauron
Perhaps this will help:
print_r($titel) will result in
Array ( [0] => Prof [1] => Mr [2] => Bac)
Posted: Thu Aug 19, 2004 11:50 am
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..
Posted: Thu Aug 19, 2004 11:59 am
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.
Posted: Thu Aug 19, 2004 12:06 pm
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
<OPTION VALUE="Prof" SELECTED>Prof</OPTION>
<OPTION VALUE="Mr" SELECTED>Mr</OPTION>
<OPTION VALUE="Dr">Dr</OPTION>
<OPTION VALUE="Drs">Drs</OPTION>
<OPTION VALUE="Ir" SELECTED>Ir</OPTION>
<OPTION VALUE="Ing">Ing</OPTION>
<OPTION VALUE="Ms">Ms</OPTION>
<OPTION VALUE="Bac" SELECTED>Bac</OPTION>
maybe you have some spaces in your array? try var_dump($titel) to see...
Posted: Thu Aug 19, 2004 12:11 pm
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
Posted: Thu Aug 19, 2004 12:22 pm
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.
Antonie