Jump to content

Combining 2 fields in a list box


newbreed65

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> 

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.