Loading data from database in drop down list-PLS HELP

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
smart_is_cool
Forum Newbie
Posts: 1
Joined: Fri Dec 02, 2011 2:56 pm

Loading data from database in drop down list-PLS HELP

Post by smart_is_cool »

Hello everyone. Can someone help me with this code, it doesn't work?
HTML CODE

Code: Select all

<table>
<tr>
<th>Ime korisnika:</th>
	<td>
		<select name="maticni_broj">
		<OPTION VALUE=0>Choose 
                <?=$options_korisnik?>
		</select>
	
    <th>Naslov knjige:</th>
	<td>
		<select name="broj_knjige">
		<OPTION VALUE=0>Choose
               <?=$options_knjige?>	
               </select>

<td colspan="2" align="center">
	<input type="submit" value=" Iznajmi ">
	</td>
</tr>
</table>[/color]
PHP CODE

Code: Select all

<?php
$sql1="SELECT broj_knjige,naslov FROM knjige"; 
$result1=mysql_query($sql1); 

$options_knjige=""; 

while ($row1=mysql_fetch_array($result1)) { 
    $broj_knjige=$row1["broj_knjige"]; 
    $naslov=$row1["naslov"]; 
echo "<OPTION VALUE=\"$broj_knjige\">$naslov</option>\n"; 
} 

$sql="SELECT maticni_broj,ime, prezime FROM clanovi"; 
$result=mysql_query($sql); 

$options_korisnik="";

while ($row=mysql_fetch_array($result)) { 

    $maticni_broj=$row["maticni_broj"]; 
    $ime=$row["ime"]; 
	$prezime=$row["prezime"]; 
    $options_korisnik.="<OPTION VALUE=\"$maticni_broj\">" .$ime .$prezime; 
}

?>
:?:
Last edited by califdon on Fri Dec 02, 2011 5:11 pm, edited 2 times in total.
Reason: Moderator added syntax=html and syntax=php tags to make code readable. Note to poster, please always do this.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Loading data from database in drop down list-PLS HELP

Post by Celauran »

Can you elaborate on what isn't working?

I see a few potential problems. First, <option> requires a closing tag. Second, $options_knjige is never populated.

Code: Select all

while ($row1=mysql_fetch_array($result1)) {
    $broj_knjige=$row1["broj_knjige"];
    $naslov=$row1["naslov"];
echo "<OPTION VALUE=\"$broj_knjige\">$naslov</option>\n";
}
Just guessing here, but that echo should probably read:

Code: Select all

$options_knjige .= "<option value=\"{$broj_knjige}\">{$naslov}</option>";
User avatar
Longo8
Forum Newbie
Posts: 2
Joined: Fri Oct 28, 2011 7:04 am

Re: Loading data from database in drop down list-PLS HELP

Post by Longo8 »

In "html code" u are using varables such us:

Code: Select all

<?=$options_korisnik?>
<?=$options_knjige?>     
I hope u saved that page as .php because that can't work in an html page.
Post Reply