Jump to content

[SOLVED] PHP List Box Array - Help!


Jezza22

Recommended Posts

Ok.. Heres what I am trying to do. I have a table called "account_status" with two columns: -

 

Status, Status_Description

 

I would like to list the results from the table into a HTML select (drop down) list, which I can achieve with the following: -

 

$query = mysql_query("SELECT * FROM accounts_status;");

echo "<select name='Status'>";

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

echo "<option>" . $row[status] . "</option>";}

echo "</select>";

 

This results in listing all of the Status codes available in the table into the HTML select drop down list, which is great!

 

However, I would like to be able to choose which status code it defaults to, yet retaining the remainder of the results in the list. Furthermore display the "Status_Description" next to it!

 

For example, lets say I have three rows of data in my table as follows: -

 

STO, Account on Stop

COD, Cash On Delivery

DNC, Do Not Call

 

Here is my attempt, :)

 

 

$StatusCode="STO";

 

$query = mysql_query("SELECT * FROM accounts_status;");

echo "<select name='Status'>";

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

$Desc=$row[status_Description];

echo "<option>" . $row[status]=$StatusCode . "</option>";}

echo "</select><tr><td>" . $Desc . "</td><td>";

 

This defaults "STO" in the list box, but when I click the down arrow I see three of the same "STO" and the Status_Description is DNC, which is the last Status_Description in the table. I can sort of see why it's doing it, please help !!!!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/147635-solved-php-list-box-array-help/
Share on other sites

Maybe something more like this is what your after?

 

<?php

if ($result = mysql_query("SELECT Status, Status_Description FROM accounts_status;")) {
  if (mysql_num_rows($result)) {
    echo "<select name='Status'>"; 
    while ($row=mysql_fetch_array($result)) {
      if ($row['Status'] == 'STO') {
        echo "<option value='{$row['Status']}' selected='selected'>{$row['Status_Description']}</option>";}
      } else {
        echo "<option value='{$row['Status']}'>{$row['Status_Description']}</option>";}
      }
    }
    echo "</select>";
  }
}

?>

YES!! That worked a treat, I had to debug it and change some minor points but it now works!!

 

It's not displaying the Status_Description next to the list box either (I wanted Status in the list box and Status_description next to the list box), but thats HTML and I think I can work that out, but defaulting the result is exactly what I wanted and can live with that!

 

Thanks once again! Jez

 

Here's the final code: -

 

 

if ($result = mysql_query("SELECT Status, Status_Description FROM accounts_status;")) {

  if (mysql_num_rows($result)) {

    echo "<select name='Status'>";

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

      if ($row['Status'] == 'STO') {

      echo "<option value='{$row['Status']}' selected='selected'>{$row['Status_Description']}</option>";}

      else {

      echo "<option value='{$row['Status']}'>{$row['Status_Description']}</option>";}

  }

      }

    }

    echo "</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.