Jump to content

Help Looping an Option Form


topflight

Recommended Posts

I am trying to create a simple option form and have it loop at the name in the database, so that all the names will show in the option menu. This is for a form I am working on here is my code

 

<?php
session_start();
if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){
      echo "You must be logged in";
}
else {


include 'db.php';
$login = $_SESSION['login'];
$lname = $_SESSION['SESS_LNAME'];
$fname = $_SESSION['SESS_FNAME'];

$connect = mysql_connect($db_host,$db_username,$db_password)
  or die("MySQL Said:".mysql_error());
  
$database = mysql_select_db($db_database,$connect)
  or die("MySQl Said:".mysql_error());

$checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'")
  or die("MySQL Said:".mysql_error());

$edata=mysql_fetch_assoc($checkl);

$link = mysql_query("SELECT * FROM `pilots` WHERE status >'0' ORDER BY login ASC")
  or die("MySQL Said:".mysql_error());
  

  

  

if($bm=='1'){

?>

<form method="post" action="?page=addstaff_phrase" name="adds"/>
<select name="pilots"> 
<?php while($data = mysql_fetch_assoc($link))?>{<option><?php echo"$fname";?> <? echo"$lname";?> - <?php echo"$pid"; ?> </option>
</select>
</form>
<?php
}


}
  $pid = $data['login'];
  $fname = $data['fname'];
  $lname = $data['lname']; 



?>

I know this sounds simple but I am confuse on how to do it please help thanks.

Link to comment
Share on other sites

<?php
$query = "SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'";
$result = mysql_query($query) or die("MySQL Said:".mysql_error());

$pilots = array();
while ($pilot = mysql_fetch_assoc($result)) {
    $pilots[$pilot['id']] = $pilot['lname'] . ',  ' . $pilot['fname'];
}
?>

<select name="pilot">
<?php foreach ($pilots as $id => $name): ?>
    <option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php endforeach ?>
</select>

Link to comment
Share on other sites

I have changed my code to this:

<?php
session_start();
if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){
      echo "You must be logged in";
}
else {



include 'db.php';
$login = $_SESSION['login'];
$lname = $_SESSION['SESS_LNAME'];
$fname = $_SESSION['SESS_FNAME'];

$connect = mysql_connect($db_host,$db_username,$db_password)
  or die("MySQL Said:".mysql_error());
  
$database = mysql_select_db($db_database,$connect)
  or die("MySQl Said:".mysql_error());

$checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'")
  or die("MySQL Said:".mysql_error());

$edata=mysql_fetch_assoc($checkl);

$result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC")
  or die("MySQL Said:".mysql_error());
  

$bm = $edata["bm"];

  

if($bm=='1'){

$pilots = array();
while ($pilot = mysql_fetch_assoc($result)) {
    $pilots[$pilot['id']] = $pilot['lname'] . ',  ' . $pilot['fname'];
}
?>

<select name="pilot">
<?php foreach ($pilots as $id => $name): ?>
    <option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php endforeach ?>
</select>
<?
} else {echo'<font color="#FF0000"> Insufficient Rights </font>';}?>
<?
} 
?>

And it is still on displaying one name in the drop down menu.

Link to comment
Share on other sites

Are you sure you are getting more than one row from the query?

 

Add this and make sure

<?php
$result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC")
  or die("MySQL Said:".mysql_error());

die("num results: " . mysql_num_rows($result));

Link to comment
Share on other sites

Now my code look like this:

<?php
session_start();
if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){
      echo "You must be logged in";
}
else {



include 'db.php';
$login = $_SESSION['login'];
$lname = $_SESSION['SESS_LNAME'];
$fname = $_SESSION['SESS_FNAME'];

$connect = mysql_connect($db_host,$db_username,$db_password)
  or die("MySQL Said:".mysql_error());
  
$database = mysql_select_db($db_database,$connect)
  or die("MySQl Said:".mysql_error());

$checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'")
  or die("MySQL Said:".mysql_error());

$edata=mysql_fetch_assoc($checkl);


$result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC")
  or die("MySQL Said:".mysql_error());

die("num results: " . mysql_num_rows($result));

$bm = $edata["bm"];

  

if($bm=='1'){

$pilots = array();
while ($pilot = mysql_fetch_assoc($result)) {
    $pilots[$pilot['id']] = $pilot['lname'] . ',  ' . $pilot['fname'];
}
?>

<select name="pilot">
<?php foreach ($pilots as $id => $name): ?>
    <option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php endforeach ?>
</select>
<?
} else {echo'<font color="#FF0000"> Insufficient Pilot Privileges </font>';}?>
<?
} 
?>

And on the page it says num rows three. I am trying to display all the member in the MySQL Database in an option menu.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.