Jump to content

Recommended Posts

I currently struggling to get 2 fields (firstname, surname) from a database to populate in a list box I can do it easy with just one field like with my code example ive got it working with people_firstname  but I just cant get it working with both of them to make a full name , any suggestions??

<?php

 foreach ($people as $people_id => $people_firstname ) {

   if ($people_id == $movie_director) {

     $selected = " selected";

   } else {

     $selected = "";

   }

?>

       <option value="<?php echo $people_id; ?>"<?php

       echo $selected; ?>>

<?php echo $people_firstname; ?></option>

<?php

 }

?>

     </select>

 

Link to comment
https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/
Share on other sites

Try this:

 

<?php

  foreach ($people as $people_id => "$people_firstname $people_lastname" ) {

    if ($people_id == $movie_director) {

      $selected = " selected";

    } else {

      $selected = "";

    }

?>

        <option value="<?php echo $people_id; ?>"<?php

        echo $selected; ?>>

      <?php echo $people_firstname; ?></option>

<?php

  }

?>

      </select>

What does the $people array look like? How is it being populated?

 

I'm guessing its like this:

Array (

0 => Bill,

1 => Bob,

2 => Jack

)

 

When populating it you could make it like this:

Array (

0 => array(Bill, Anderson),

1 => array(Bob, Builder),

2 => array(Jack, Beanstalk)

)

 

Then your code:

 

<?php
  foreach ($people as $people_id => $people_array) {
    if ($people_id == $movie_director) {
      $selected = " selected";
    } else {
      $selected = "";
    }
?>
        <option value="<?php echo $people_id; ?>"<?php
        echo $selected; ?>>
      <?php echo $people_array[0]." ".$people_array[1]; ?></option>
<?php
  }
?>
      </select> 

thanx ProjectFear that sorta works its showing first letter and second letter of people_firstname i just need to declaire the fields better

 

im getting

 

J i

B o

S a

 

when i want

 

Jim Carrey

Bob Random

Sam Smith

Yeah, you would. You need to post the code that builds your $people array. When you populate it you need to make the value for the keys an array containing the first and last name. Hope you can understand what I mean.

 

If you don't, post the part of the code that makes the $people array.

yes sorry in away i did get what you mean but because im just a beginner with PHP my head struggles to get round somethings

 

  $peoplesql = "SELECT * FROM people";

  $result = mysql_query($peoplesql) 
    or die("Invalid query: " . mysql_error()); 
  while ($row = mysql_fetch_array($result)) {
    $people[$row['people_id']] = $row['people_firstname'];
  }

      <select name="movie_director">
        <option value="" selected>Select a director...</option>
<?php
  foreach ($people as $people_id => $people_array) {
    if ($people_id == $movie_director) {
      $selected = " selected";
    } else {
      $selected = "";
    }
?>
        <option value="<?php echo $people_id; ?>"<?php
        echo $selected; ?>>
      <?php echo $people_array[$people_firstname]." ".$people_array[1]; ?></option>
<?php
  }
?>
      </select>


Okay try this:

 

  $peoplesql = "SELECT * FROM people";

  $result = mysql_query($peoplesql) 
    or die("Invalid query: " . mysql_error()); 
  while ($row = mysql_fetch_array($result)) {
    $people[$row['people_id']] = array("firstname" => $row['people_firstname'], "lastname" => $row['people_lastname']);
  }

      <select name="movie_director">
        <option value="" selected>Select a director...</option>
<?php
  foreach ($people as $people_id => $people_array) {
    if ($people_id == $movie_director) {
      $selected = " selected";
    } else {
      $selected = "";
    }
?>
        <option value="<?php echo $people_id; ?>"<?php
        echo $selected; ?>>
      <?php echo $people_array['firstname']." ".$people_array['lastname']; ?></option>
<?php
  }
?>

 

EDIT, actually you don't need two loops.

Try this, if it doesn't work I mucked up somewhere. In that case, use the above.

 

  $peoplesql = "SELECT * FROM people";

  $result = mysql_query($peoplesql) 
    or die("Invalid query: " . mysql_error()); 
?>
      <select name="movie_director">
        <option value="" selected>Select a director...</option>
<?php
while ($row = mysql_fetch_array($result)) {
	if($row['people_id'] == $movie_director){
		$selected = " selected='selected'";
	}else{
		$selected = "";
	}

?>
<option value="<?php echo $row['people_id']; ?>"<?php echo $selected; ?>>
	<?php echo $row['people_firstname']." ".$row['people_lastname']; ?>
</option>
  <?php
  }
  ?>
  </select>

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.