Page 1 of 1

Problem with foreach

Posted: Wed Aug 25, 2004 2:16 pm
by ddragas
Hi all

I have problem with thois script

Code: Select all

<?php 
$sql = "select * from apartmani where iznajmljivac = '$broj' order by vrsta_smjestaja"; 
$opts = array(); 
$sifra = array();
$res = mysql_query($sql) or die(mysql_error()); 
while($row = mysql_fetch_array($res)){ 
    $opts[] = $row['vrsta_smjestaja'] . " - " . $row['Naziv_objekta']; 
	$sifra[] = $row['Sifra_apartmana'];
} 

?>
<select name="drugi_smjestaj" onChange="MM_jumpMenu('parent',this,0)" class="txt">
<?php 
foreach($sifra as $sif & $opts as $opt){
    echo '<option value="http://localhost/plavi/hr/preview_app.php?Sifra_apartmana='.$sif.'">'.$opt.'</option>'; 
} 
include("disconect.php");
?>
<?php

?>
Don't know how to connect these two, and I get parse error on line

foreach($sifra as $sif & $opts as $opt)

$shif should be ID of record, and $opt should be name of record

Please help

Posted: Wed Aug 25, 2004 2:19 pm
by feyd
you cannot foreach 2 different arrays at the exact same time.

Posted: Wed Aug 25, 2004 2:23 pm
by feyd
why not.....

Code: Select all

while($row = mysql_fetch_array($res)){
   $sifra[$row['Sifra_apartmana']] = $row['vrsta_smjestaja'] . " - " . $row['Naziv_objekta'];
}

//..................

foreach($sifra as $sif => $opt) ........

Posted: Wed Aug 25, 2004 2:29 pm
by ddragas
OK. Thanx for help

Now It shows in jump menu ID of record and in next row nameof record. Could it be done to show only name of record. Without ID?

Posted: Wed Aug 25, 2004 2:31 pm
by ddragas
Sorry

My mistake

Thanks for help

Works great

Posted: Wed Aug 25, 2004 2:50 pm
by ddragas
one more problem

When new page loads (after selection of jump menu) it is selected first value in jump menu. How to select value that was choosen?

Posted: Wed Aug 25, 2004 2:53 pm
by feyd

Posted: Wed Aug 25, 2004 3:07 pm
by ddragas
Thank you very much

Regards