object in array in array

Status
Niet open voor verdere reacties.

JeroenE

Terugkerende gebruiker
Lid geworden
20 mrt 2005
Berichten
1.950
Hallo,

Ik heb een stuk php-code dat met een while-loop data uit een tabel haalt van een mysql database.
Binnen die while loop wordt op het einde telkens een nieuw element toegevoegd aan een array binnen een eerder gedefinieerde array:
PHP:
<script language="javascript" type="text/javascript">
var aorgs = [];
<?php
$afirstsql = "SELECT * from first ORDER BY id";
$afirstquer = mysql_query($afirstsql,$sdb) or die(mysql_error());

while ($afirstline = mysql_fetch_object($afirstquer)) {
	$nfirst = count($afirst);
	$afirst[$nfirst]["id"] = $afirstline->id;
	$afirst[$nfirst]["cat_id"] = $afirstline->cat_id;
	$afirst[$nfirst]["name"] = $afirstline->name;
	for ($ni = 0; $ni <= $nzipcodes; $ni++) {
		if ($afirstline->zipcode_id == $azipcodes[$ni]["id"]) {
			$afirst[$nfirst]["location"] = $azipcodes[$ni]["location"];
		};
	};
	?>
	aorgs[<?php echo $afirst[$nfirst]["cat_id"]; ?>][<?php echo $afirst[$nfirst]["id"]; ?>] = {name: "<?php echo $afirst[$nfirst]["name"]; ?>", location: "<?php echo $afirst[$nfirst]["location"]; ?>"};
	<?php
};

$asecondsql = "SELECT * from second ORDER BY icri_id";
$asecondquer = mysql_query($asecondsql, $sdb) or die(mysql_error());

while ($asecondline = mysql_fetch_object($asecondquer)) {
	$nsecond = count($asecond);
	$asecond[$nsecond]["id"] = $asecondline->id;
	$asecond[$nsecond]["cat_id"] = $asecondline->cat_id;
	$asecond[$nsecond]["name"] = $asecondline->name;
	for ($na = 0; $na <= $nfirst; $na++) {
		if ($asecondline->icri_id == $afirst[$na]["id"]) {
			for ($ni = 0; $ni <= $nzipcodes; $ni++) {
				if ($afirst[$na]["zipcode_id"] == $azipcodes[$ni]["id"]) {
					$asecond[$nsecond]["location"] = $azipcodes[$ni]["location"];
				};
			};
		};
	};
	?>
	aorgs[<?php echo $asecond[$nsecond]["cat_id"]; ?>][<?php echo $asecond[$nsecond]["id"]; ?>] = {name: "<?php echo $asecond[$nsecond]["name"]; ?>", location: "<?php echo $asecond[$nsecond]["location"]; ?>"};
	<?php
};
?>
</script>

Dit geeft als array in javascript:

[JS]
<script language="javascript" type="text/javascript">
var aorgs = [];
aorgs[1][1] = {name: "naam 1", location: "locatie 1"};
aorgs[1][2] = {name: "naam 2", location: "locatie 2"};
aorgs[1][3] = {name: "naam 3", location: "locatie 3"};
aorgs[1][4] = {name: "naam 4", location: "locatie 4"};
aorgs[1][5] = {name: "naam 5", location: "locatie 5"};
aorgs[1][6] = {name: "naam 6", location: "locatie 6"};
aorgs[2][1] = {name: "naam 1", location: "locatie 1"};
aorgs[2][2] = {name: "naam 2", location: "locatie 2"};
aorgs[2][3] = {name: "naam 3", location: "locatie 3"};
aorgs[2][4] = {name: "naam 4", location: "locatie 4"};
</script>[/JS]

Als ik dan de gegevens nodig heb volgens categorie 2 organisatie 3 (aorgs[2][3].name en aorgs[2][3].location), dan krijg ik niets
Als ik in mijn adresbalk het volgende typ, krijg ik als alert een 0
javascript:alert(aorgs.length);

Definieer ik de array met [""], dan krijg ik als alert een 1

Hetzelfde resultaat krijg ik met new Array() en new Array("")

Wat loop hier mis?
 
Laatst bewerkt:
Ik heb het als volgt opgelost:
Per categorie heb ik er eerst voor gezorgd dat, als $nfirst nog 0 is, de subarray eerst definieerde als lege array
PHP:
<script language="javascript" type="text/javascript">
var aorgs = [];
<?php
$afirstsql = "SELECT * from first ORDER BY id";
$afirstquer = mysql_query($afirstsql,$sdb) or die(mysql_error());
 
while ($afirstline = mysql_fetch_object($afirstquer)) {
    $nfirst = count($afirst);
    $afirst[$nfirst]["id"] = $afirstline->id;
    $afirst[$nfirst]["cat_id"] = $afirstline->cat_id;
    $afirst[$nfirst]["name"] = $afirstline->name;
    for ($ni = 0; $ni <= $nzipcodes; $ni++) {
        if ($afirstline->zipcode_id == $azipcodes[$ni]["id"]) {
            $afirst[$nfirst]["location"] = $azipcodes[$ni]["location"];
        };
    };
    if ($nfirst == 0) {
        ?>
        aorgs[<?php echo $afirst[$nfirst]["cat_id"]; ?>] = [];
        <?php
    };
    ?>
    aorgs[<?php echo $afirst[$nfirst]["cat_id"]; ?>][<?php echo $afirst[$nfirst]["id"]; ?>] = {name: "<?php echo $afirst[$nfirst]["name"]; ?>", location: "<?php echo $afirst[$nfirst]["location"]; ?>"};
    <?php
};
 
$asecondsql = "SELECT * from second ORDER BY icri_id";
$asecondquer = mysql_query($asecondsql, $sdb) or die(mysql_error());
 
while ($asecondline = mysql_fetch_object($asecondquer)) {
    $nsecond = count($asecond);
    $asecond[$nsecond]["id"] = $asecondline->id;
    $asecond[$nsecond]["cat_id"] = $asecondline->cat_id;
    $asecond[$nsecond]["name"] = $asecondline->name;
    for ($na = 0; $na <= $nfirst; $na++) {
        if ($asecondline->icri_id == $afirst[$na]["id"]) {
            for ($ni = 0; $ni <= $nzipcodes; $ni++) {
                if ($afirst[$na]["zipcode_id"] == $azipcodes[$ni]["id"]) {
                    $asecond[$nsecond]["location"] = $azipcodes[$ni]["location"];
                };
            };
        };
    };
    if ($nsecond == 0) {
        ?>
        aorgs[<?php echo $asecond[$nsecond]["cat_id"]; ?>] = [];
        <?php
    };
    ?>
    aorgs[<?php echo $asecond[$nsecond]["cat_id"]; ?>][<?php echo $asecond[$nsecond]["id"]; ?>] = {name: "<?php echo $asecond[$nsecond]["name"]; ?>", location: "<?php echo $asecond[$nsecond]["location"]; ?>"};
    <?php
};
?>
</script>
Dan krijg je dit in het javascript:
[JS]<script language="javascript" type="text/javascript">
var aorgs = [];
aorgs[1] = [];
aorgs[1][1] = {name: "naam 1", location: "locatie 1"};
aorgs[1][2] = {name: "naam 2", location: "locatie 2"};
aorgs[1][3] = {name: "naam 3", location: "locatie 3"};
aorgs[1][4] = {name: "naam 4", location: "locatie 4"};
aorgs[1][5] = {name: "naam 5", location: "locatie 5"};
aorgs[1][6] = {name: "naam 6", location: "locatie 6"};
aorgs[2] = [];
aorgs[2][1] = {name: "naam 1", location: "locatie 1"};
aorgs[2][2] = {name: "naam 2", location: "locatie 2"};
aorgs[2][3] = {name: "naam 3", location: "locatie 3"};
aorgs[2][4] = {name: "naam 4", location: "locatie 4"};
</script>[/JS]
 
Laatst bewerkt:
Is het niet handiger om eens te kijken of je, wanneer je het resultaat vanuit MySQL direct naar een Javascript-object omzet via json_encode(), er verder mee kunt werken in Javascript.

Dan hoef je niet eerst door alle resultaten heen :)
 
Met die functie heb ik geen ervaring, laat staan dat ik weet wat die doet.
Alleszins bedankt voor de tip.
Ik leer die binnenkort net als zoveel andere zaken die ik nog niet ken
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan