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!
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
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.
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.
<?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 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.
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
@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.
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.
<?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.