Select list box display double same value - php

i have a list box that contain name of persons. In the column "Requestor" there are many names that repeat with the same name. So when I make the list box, the name shows all the name and some name shows more than one with the same name. How can i make the name just come out one only. Below are my codes.
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
?>
<option id="<?php echo $viewallCapex_transaction ['Project_id'];?>"><?php
echo $viewallCapex_transaction['Requestor']; ?></option>
<?php } ?>
</select>
And one more question how can I show list of month and year that connect with mySql? can someone show me some codes. Thanks

Well, if there are multiple rows with the same "Requestor" but different "Project_id", that's just how it's gonna turn out. You can select distinct by Requestor in your SQL, but that will leave you unaware of what record you're pointing at.

Put your values in an array and some something like array_unique to create an unique array. Try something like this:
<?php
$requestorArray = array();
?>
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
$requestorArray[$viewallCapex_transaction ['Project_id']] = $viewallCapex_transaction['Requestor'];
}
$requestorArray = array_unique($requestorArray);
foreach($requestorArray as $projectId => $requestor)
?>
<option id="<?php echo $projectId;?>"><?php
echo $requestor; ?></option>
<?php } ?>
</select>

Related

Use mysql query result in more than one place in code

I'm building a system that tracks contact lenses. I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. my code looks like this :
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
Then I echo that list out in a while loop using PHP to populate the options in the dropdown.
My question is this: I have these dropdowns for each eye on the same form. So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". But ONLY the right eye is populating with the brand info because it appears first in the code. What i'm having to do is copy/paste the exact same query and do
$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
then later if I need the dropdown again, I need to do $contact_list3..and so on. Why can i not generate a drop down using the same variable? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time?
just for refernce, my php while code is this:
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
while($row = mysqli_fetch_array($contact_list))
{
?>
<option value = "<?php echo($row['brand'])?>" name = "brandOS">
<?php echo($row['brand']) ?>
</option>
<?php
}
?>
</select>
I have this loop copy/pasted for right eye and left eye. But it only works on which ever drop down appears first in the code.
A possible solution will be more efficient in term of performance could be :
<?php
$left_eye = '<option value="0">Please Select</option>';
$rigth_eye = '<option value="0">Please Select</option>';
while($row = mysqli_fetch_array($contact_list))
{
//logic for left eye
$left_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
//logic for rigth eye
$rigth_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
}
?>
<select class="form-control" name = "brandOS">
<?php echo $left_eye ; ?>
</select>
<select class="form-control" name = "brandOS">
<?php echo $rigth_eye ; ?>
</select>
With this solution you get your result in the same while loop. If the left and right select are the same you can use the same variable.
Store the brands in an array, then you can just loop through the array.
<?php
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
$brands = array();
while($row = mysqli_fetch_array($contact_list))
{
array_push($brands, $row['brand']);
}
?>
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
foreach($brands as $brand){
?>
<option value = "<?php echo($brand[0])?>" name = "brandOS">
<?php echo($brand[0]) ?>
</option>
<?php
}
?>
</select>
You can use a PHP array, like the SESSION one, to store values and use them across your site. Be sure you call "session_start()" method on each page you use that array, though.
//Initialize sessions
session_start();
...
//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;
To use it, just be sure to call the method I told you above, and call the variable with the same syntax:
<?php
while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>
Hope this helps.

SET selected in the select tag

<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'">'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
This is Edit.php
This is the result of that code
but i want is. after getting the data from the database. it will automatically selected the value of the option base on the database data.
Suppose the query result returned from tblchurchs contains a field Selected which is either set to 1 or 0. If it is set to 1 then you have to select that option in your drop-down list, and if it is set to 0, it is not selected.
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'"';
if($get_detail['Selected'] == 1)
{
echo ' selected="selected"';
}
echo '>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
to getting selected for selected option.
first you have to save the selected data getting from database to a variable and then using an if loop you can do this.
<?php
$AChurchID=1; // this is your already selected value that is in db .
?>
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
$sel="";
if($AChurchID==$get_detail['AChurchID']){
$sel="selected";
}
echo'<option value="'.$get_detail['AChurchID'].'" '.$sel.'>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
This will work in your case. This is works fine in ma case. Please have a try.

Php pdo display enum values in dropdown

i am trying below code to display list of enum values in select dropdown box.
but its displaying only dropdown box, but values are not displaying....
tablename = tbl_users, column name = userStatus
<select>
<?
$stmt = $user_home->runQuery('SHOW COLUMNS FROM '.tbl_users.' WHERE field="'.userStatus.'"');
while($data = $stmt->fetch()) {
foreach(explode("','",substr($row[1],6,-2)) as $option) {
print("<option>$option</option>");
}
}
?>
<select>
Note : I really tried lot before posting question here & i am new to php coding, still learning....
To display list of enum values in select dropdown:
<select name="select">
<?php
$sql = 'SHOW COLUMNS FROM table_name WHERE field="field_name"';
$row = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
foreach(explode("','",substr($row['Type'],6,-2)) as $option) {
print("<option value='$option'>$option</option>");
}
?>
</select>
For display enum value as dropdown you can do something like this.
<?php $status = array('Y'=>'Approve','N'=>'unapprove'); ?>
<select>
<?php foreach($status as $key=>$state) { ?>
<option value="<?php echo $key;?>"><?php echo $state;?></option>
<?php } ?>
</select>

How to add a dropdown box for state name in vtiger crm

I have a form and I have to add state name field with a drop down so the user can select name of the state they want from that select box.
How can I do this?
Assuming you have a databse like mysql.
create a table state (state_id, state_name, state_abbr)
fetch the states from the state table (write a function to fetch states)
iterate in your option of the select box using php script, for example:
<select name="state">
<?php
// At this point you should have a recordset $rsstate which fetches all the records from the state table
while($rowState = mysql_fetch_array($rsState)) { ?>
<option value=<?php echo $rowState["state_abbr"] ?>><?php echo $rowState["state_name"]; ?></option>
<?php } ?>
</select>
You can use vtlib library for that.
This how we create a state name dropdown Box in Accounts module using vtlib
<?php
$Vtiger_Utils_Log = true;
include_once('vtlib/Vtiger/Menu.php');
include_once('vtlib/Vtiger/Module.php');
$module = Vtiger_Module::getInstance('Accounts');
$infoBlock = Vtiger_Block::getInstance('LBL_ACCOUNT_INFORMATION', $module);
$stateField = Vtiger_Field::getInstance('state', $module);
if (!$stateField) {
$stateField = new Vtiger_Field();
$stateField->name = 'state';
$stateField->label = 'State';
$stateField->columntype = 'VARCHAR(100)';
$stateField->uitype = 16;
$stateField->typeofdata = 'V~O';
$infoBlock->addField($stateField);
$stateField->setPicklistValues(array('Kerala', 'Karnataka', 'Maharashtra', 'Manipur'));
}
Add the rest of state list in that array.
Hope this helps.
Just go to admin panel and add a picklist. Its very simple.
Assuming you have a databse like mysql.
create a table state (state_id, state_name, state_abbr)
fetch the states from the state table (write a function to fetch states)
iterate in your option of the select box using php script
example:
<select name="state">
<?php
*// At this point you should have a recordset $rsstate which fetches all the records from the state table*
while($rowState = mysql_fetch_array($rsState)){?>
<option value=<?php echo $rowState["state_abbr"]?>><?php echo $rowState["state_name"]; ?></option>
<?php }?>
</select>

Multiple initially selected values in a dynamic select list with PHP

Thanks in advance for your help. This list is to update an existing record, so it's populated from a database with $rs_fullcat then checked with another recordset $rs_cat for the initially selected values. I can only get it to initially select one value, the first one in $rs_cat, but I need it to select all of the existing options from the database. I feel like I'm close but I can't find the answer anywhere. Here's the code:
<select name="category" multiple="multiple" id="category">
<?php
do {
?>
<option value="<?php echo $row_rs_fullcat['categoryID']?>"<?php if (!(strcmp($row_rs_fullcat['categoryID'], $row_rs_cat['categoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_fullcat['category_name']?></option>
<?php
} while ($row_rs_fullcat = mysql_fetch_assoc($rs_fullcat));
$rows = mysql_num_rows($rs_fullcat);
if($rows > 0) {
mysql_data_seek($rs_fullcat, 0);
$row_rs_fullcat = mysql_fetch_assoc($rs_fullcat);
}
?>
</select>
What you want to do, is first select (and fetch) all the selected ones from the database, and put them in a variable ($rs_cat in your case). Then, in your while loop it's a simple matter of doing an in_array() check to see if the value is selected.

Categories