problem listing data based on selection in listbox

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

angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

problem listing data based on selection in listbox

Post by angelic_devil »

i have 2 list box i want tht data in the second list box displa data based on the selection in the first list box

e.g if i select approved in the first list box then corresponding data of users that have status as approved be listed in second list box.


but the code is not doing tht its displaying all the data irrespective of the selection of the first box.
this is wat i am doing

Code: Select all

 
<body>
 
<?php
$username = 'username';
$usernamevalue = array();
$status = 'status';      
 
$query_name = "SELECT users.username FROM users WHERE users.status=$status";
    $result = mysql_query($query_name);
    confirm_query($result);
        
?>
<div style="position: absolute; width: 100px; height: 32px; z-index: 1; left: 225px; top: 5px" id="layer1"> 
<td><select name="status" maxlength="15">
<?php 
        
            echo '<option>'.Approved.'</option>';echo "<BR>";
             echo '<option>'.pending.'</option>';echo "<BR>";
              echo '<option>'.ban.'</option>';
        
        
        echo "<BR>";
            
    
?>  
 
 
  </select> </td>
  </div>
 
<div style="position: absolute; width: 100px; height: 32px; z-index: 1; left: 225px; top: 35px" id="layer2"> 
<td><select name="username" maxlength="23">
<?php 
        
            while ($record = mysql_fetch_assoc($result)) {
        while (list($username, $usernamevalue) = each ($record)) {
        echo '<option>'.htmlentities($usernamevalue).'</option>';
        
        }
        echo "<BR>";
                
    }   
                
    
?>  
  </select> </td>
  
            
</div>
 
</body>
 
 
please tell me which line i m making the mistake and wat i shud change

thanx
Last edited by Benjamin on Sat Apr 25, 2009 1:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

plz someone help me out with this
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: problem listing data based on selection in listbox

Post by Benjamin »

What is the value of $status after you submit the form? Where are your form tags?
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

y form tag for...even with it it doesnt change the end result...the first listbox shows all the optiones mentioned in line 18,19,20 and 2nd listbox populates with data from database irrespective of the selection of creteria of the first...i want it to display only those based on data selected in first.

$status are the values mentioned in the options in lines 18,19,20
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: problem listing data based on selection in listbox

Post by McInfo »

This script will help me see what your database table looks like. Change the values of the defined constants. Run the script. Then copy-and-paste the output into a reply post.

Code: Select all

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'database');
define('DB_TABLE', 'users');
 
header('Content-Type: text/plain');
mysql_connect(DB_HOST, DB_USER, DB_PASS)
    or die('Could not connect to MySQL server.');
mysql_select_db(DB_NAME)
    or die('Could not connect to MySQL database.');
$query = 'EXPLAIN `'.DB_TABLE.'`';
if ($result = mysql_query($query)) {
    while ($row = mysql_fetch_assoc($result)) {
        $rows[] = $row;
    }
    echo serialize($rows);
} else {
    echo 'Query failed.';
}
mysql_close();
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:19 am, edited 1 time in total.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

THE output was
a:15:{i:0;a:6:{s:5:"Field";s:8:"username";s:4:"Type";s:11:"varchar(23)";s:4:"Null";s:2:"NO";s:3:"Key";s:3:"PRI";s:7:"Default";N;s:5:"Extra";s:0:"";}i:1;a:6:{s:5:"Field";s:15:"hashed_password";s:4:"Type";s:11:"varchar(40)";s:4:"Null";s:2:"NO";s:3:"Key";s:0:"";s:7:"Default";N;s:5:"Extra";s:0:"";}i:2;a:6:{s:5:"Field";s:7:"emailid";s:4:"Type";s:4:"text";s:4:"Null";s:2:"NO";s:3:"Key";s:0:"";s:7:"Default";N;s:5:"Extra";s:0:"";}i:3;a:6:{s:5:"Field";s:6:"status";s:4:"Type";s:7:"char(1)";s:4:"Null";s:2:"NO";s:3:"Key";s:0:"";s:7:"Default";s:1:"P";s:5:"Extra";s:0:"";}i:4;a:6:{s:5:"Field";s:4:"rank";s:4:"Type";s:7:"char(1)";s:4:"Null";s:2:"NO";s:3:"Key";s:0:"";s:7:"Default";s:1:"U";s:5:"Extra";s:0:"";}i:5;a:6:
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: problem listing data based on selection in listbox

Post by McInfo »

It looks like two thirds of the serialized array are missing, but there was enough to work with.

I noticed you used the text data type for the emailid field. My intuition says there is something wrong with that.

This is meant to be an example rather than a complete solution. I am hoping you will read through the code comments carefully and try to understand what the script is doing.

Code: Select all

<?php
//------------------------------------------------------------
// Part 1: Choosing a Status
//============================================================
 
// Initializes a list of acceptable statuses
$status_list = array
(   'A' => 'Approved'
,   'P' => 'Pending'
,   'B' => 'Banned'
);
 
// Sets the default status
$status = 'P';
 
// If a status is requested and that status is legitimate...
if (isset($_GET['status']) && in_array($_GET['status'], array_keys($status_list)))
{
    // ...sets the status to the requested status
    $status = $_GET['status'];
}
 
// A form for choosing the status
?>
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
    <input type="submit" value="Search for status:" />
    <select name="status">
        <?php
        // Loops through the $status_list array
        foreach ($status_list as $value => $option)
        {
            // Sets an attribute to show the chosen status as selected
            $selected = ($status == $value) ? ' selected="selected"' : '';
           
            // Builds an option for each acceptable status
            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
        }
        ?>
    </select>
</form>
 
<?php
//------------------------------------------------------------
// Part 2: Connecting to the Database
//============================================================
 
// Database configuration constants
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'test_99098');
define('DB_TABLE', 'users');
 
// Sets the database connection
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASS)
    or die('Could not connect to MySQL server.');
 
// Selects the database
mysql_select_db(DB_NAME, $dbc)
    or die('Could not connect to MySQL database.');
 
//------------------------------------------------------------
// Part 3: Using the Chosen Status to Search the Database
//============================================================
 
// Initializes an empty array of usernames
$usernames = array();
 
// Builds a query to get the names of users with a certain status
$query = "SELECT `username` FROM `users` WHERE `status` = '{$status}'";
 
// Executes the query and stores the returned resource in $result
$result = mysql_query($query);
 
// If $result is a resource instead of FALSE...
if ($result)
{
    // Fetches a row until there are no more rows to fetch
    while ($row = mysql_fetch_assoc($result))
    {
        // Stores the username from the fetched row in the $usernames array
        $usernames[] = $row['username'];
    }
}
 
//------------------------------------------------------------
// Part 4: Closing the Database Connection
//============================================================
 
// Closes the database connection
mysql_close($dbc);
 
//------------------------------------------------------------
// Part 5: Building a List of Usernames
//============================================================
 
// If there are some usernames in the array...
if (count($usernames) > 0)
{
    ?>
    <p>Users with status: <?php echo $status_list[$status]; ?></p>
    <ul>
        <?php
        // Loops through the list of usernames
        foreach ($usernames as $name)
        {
            // Builds a list item for each username
            echo '<li>'.$name.'</li>';
        }
        ?>
    </ul>
    <?php
}
else
{
    ?>
    <p>There are no users with status: <?php echo $status_list[$status]; ?></p>
    <?php
}
?>
I'm just curious; what software do you use to write your PHP scripts?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:20 am, edited 1 time in total.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

editpad
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

but ur script is not putting the results in a drop down its echoing them back... i want the results in a drop box thts my problem
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: problem listing data based on selection in listbox

Post by McInfo »

angelic_devil wrote:but ur script is not putting the results in a drop down its echoing them back... i want the results in a drop box thts my problem
McInfo wrote:This is meant to be an example rather than a complete solution. I am hoping you will read through the code comments carefully and try to understand what the script is doing.
...then you will be able to catch your own fish.

The solution to putting the usernames in a drop-down list is in the example between lines 27 and 39.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:21 am, edited 1 time in total.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

can you plzz tell me what is happening in line 33
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: problem listing data based on selection in listbox

Post by McInfo »

Every pass though the loop, $selected is set to either an empty string or ' selected="selected"'. If $status has the same value as $value, $selected is set to ' selected="selected"'. The string is added to the option tag so that the chosen option will be shown as the selected option when the page is loaded.

If 'P' is the chosen (or default) status, the resulting HTML will be

Code: Select all

<select name="status">
    <option value="A">Approved</option>
    <option value="P" selected="selected">Pending</option>
    <option value="B">Banned</option>
</select>
Line 33 uses the "ternary operator" and is a shorthand way of writing

Code: Select all

if ($status == $value) {
    $selected = ' selected="selected"';
} else {
    $selected = '';
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:22 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: problem listing data based on selection in listbox

Post by Benjamin »

@angelic_devil, please do not use abbreviated words such as ur, plz, thts etc. Refer to the following forum rule:
Forum Rules wrote: 11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

thanx so so much i was able to get it into listbox and its makes sense now thanx
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: problem listing data based on selection in listbox

Post by angelic_devil »

ok i tried a little different thing as in case i have to get the data from database for the status and based on tht it should display the content of users now its displaying the content of status fine and that of user too but the users is not changing based on the selection of status.

please tell me where i am going now in this

Code: Select all

 
 
 
<?php
//------------------------------------------------------------
// Part 1: Choosing a Status
//============================================================
 
// Initializes a list of acceptable statuses
$status_list = array();
$query_status = " SELECT status_type.status from status_type ";
       $result_status = mysql_query($query_status);
        confirm_query($result_status);
  while ($record = mysql_fetch_assoc($result_status)) {
          $status_list[] = $record['status'] ;
      
        }
        
  $status = $_GET['status']; 
// A form for choosing the status
?>
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
    <input type="submit" value="Search for status:" />
    <select name="status">
        <?php
        // Loops through the $status_list array
         foreach ($status_list as $value => $option)
        {
            // Sets an attribute to show the chosen status as selected
            $selected = ($status == $value) ? ' selected="selected"' : '';
           
            // Builds an option for each acceptable status
            echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
        }
        ?>
    </select>
</form>
 
<?php
//------------------------------------------------------------
// Part 2: Using the Chosen Status to Search the Database
//============================================================
 
// Initializes an empty array of usernames
$usernames = array();
 
// Builds a query to get the names of users with a certain status
$query = "SELECT username FROM users WHERE status = '{$status}'";
 
// Executes the query and stores the returned resource in $result
$result = mysql_query($query);
 
// If $result is a resource instead of FALSE...
if ($result)
{
    // Fetches a row until there are no more rows to fetch
    while ($row = mysql_fetch_assoc($result))
    {
        // Stores the username from the fetched row in the $usernames array
        $usernames[] = $row['username'];
    }
}
 
 
//------------------------------------------------------------
// Part 5: Building a List of Usernames
//============================================================
 
// If there are some usernames in the array...
if (count($usernames) > 0)
{
    ?>
    <p>Users with status: <?php echo $status_list[$status]; ?></p>
    <ul>
    <td><select name="users" maxlength="23">
        <?php
        // Loops through the list of usernames
        foreach ($usernames as $name )
        {
            // Builds a list item for each username
            echo '<option>'.$name.'</option>';
        }
        ?>
        
        </select> </td>
       
    </ul>
    <?php
}
else
{
    ?>
    <p>There are no users with status: <?php echo $status_list[$status]; ?></p>
    <?php
}
?>
 
 
 
Last edited by angelic_devil on Tue Apr 28, 2009 12:26 am, edited 2 times in total.
Post Reply