Problem with foreach

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

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Problem with foreach

Post 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
Last edited by ddragas on Wed Aug 25, 2004 2:23 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you cannot foreach 2 different arrays at the exact same time.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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) ........
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Sorry

My mistake

Thanks for help

Works great
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you very much

Regards
Post Reply