i have a dependent drop-down list which composition is being populated depending on KNITTYPE. But when i try to order the composition i can not get any result, it doesnt work . what am i doing wrong ?
my second question is : how can i eliminate the repeat of the same composition results
i know they belong to different rows but i want to merge them so for eaxmple when i select 100%COTTON i want to bring all 100%COTTON results , rightnow its bringing 100% cotton for each KNITTYPE SELECTED ?
<?php
require_once 'dbconnect.php';
if(!empty($_REQUEST["KNITTYPE_id"])) {
$query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."' AND ORDER COMPOSITION BY ASC" ;
$result = mysqli_query($conn, $query);
?>
<option value="">Select COMPOSITION</option>
<?php
while($row2=mysqli_fetch_assoc($result)){
//var_dump($row2);
if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1) { ?>
<option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo $row2['COMPOSITION']; ?> </option>
<?php
$bul2[$row2['COMPOSITION']] = true;
}
}
}
?>
Your MYSQL query statement is not in the right way
if(!empty($_REQUEST["KNITTYPE_id"])) {
$query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."' ORDER BY COMPOSITION ASC LIMIT 1" ;
$result = mysqli_query($conn, $query);
?>
<option value="">Select COMPOSITION</option>
<?php
while($row2=mysqli_fetch_assoc($result)){
//var_dump($row2);
if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1) { ?>
<option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo $row2['COMPOSITION']; ?> </option>
<?php
$bul2[$row2['COMPOSITION']] = true;
}
}
}
?>
this worked for me i guess
<?php
require_once 'dbconnect.php';
if(!empty($_REQUEST["KNITTYPE_id"])) {
$query ="SELECT COMPOSITION FROM egearge3 WHERE KNITTYPE =" . "'" . mysqli_escape_string($conn, $_POST["KNITTYPE_id"] ) ."' GROUP BY COMPOSITION " ;
$result = mysqli_query($conn, $query);
?>
<option value="">Select COMPOSITION</option>
<?php
while($row2=mysqli_fetch_assoc($result)){
//var_dump($row2);
if($bul2[$row2['COMPOSITION']] != true && $row2['COMPOSITION'] != 'COMPOSITION' || 1) { ?>
<option value="<?php echo $row2['COMPOSITION']; ?>"><?php echo $row2['COMPOSITION']; ?> </option>
<?php
$bul2[$row2['COMPOSITION']] = true;
}
}
}
?>
AND ORDER COMPOSITION BY ASC
That is not valid.
You don't need a AND before ORDER and the correct syntax is ORDER BY X, and not ORDER X BY
ORDER BY COMPOSITION ASC
See DISTINCT to remove duplicate entries from the result
Related
I have this non-dynamic script works like this:
<p>Environment: <select name="formEnvironment" value="" >
<option value="TEST" <?= $_POST['formEnvironment'] == TEST ? 'selected' : '' ?>>Test</option>
<option value="PROD" <?= $_POST['formEnvironment'] == PROD ? 'selected' : '' ?>>Production</option>
</select></p>
As I have tried to make it more dynamic as possible within variables. I have problems with code within while nest and I do think it has to do with strings and variables mixing together which makes it more complicated. Any assistance with what is right way to code in that regard. I would appreciate assistance as I am only few months into PHP.
function DropDownItems($itemName, $query, $connection)
{
$result = mysqli_query($connection, $query);
while ($row = $result->fetch_assoc()){
$MAS = "$_POST['form"{$itemName}"']";
echo "<option value=\"{$row['DDVALUE']}\" ";
echo "<?= "{$MAS}" == "{$row['DDVALUE']}" ? 'selected' : '' ?>>"{$row['DDTEXT']}"</option>";
}
}
$itemName="Environment";
$query = "SELECT DDVALUE, DDTEXT FROM DDLISTS WHERE FIELDNAME='".$itemName."' ORDER BY DDTEXT ASC";
echo "<p>$itemName.": <select name='form".$itemName."'>";
echo "<option value=''>Select below:</option>";
DropDownItems($itemName, $query, $connection);
echo "</select>";
Its never a good idea to echo in functions, instead just return an array and then loop over it.
You can also break out of PHP so you don't need to do lots of echos.
So many syntax errors, so I've just rewrote it:
<?php
function DropDownItems($itemName, $query, $connection) {
$return = [];
$result = mysqli_query($connection, $query);
while ($row = $result->fetch_assoc()) {
$return[] = [
'value' => $row['DDVALUE'],
'label' => $row['DDTEXT']
];
}
return $return;
}
$itemName="Environment";
$query = "SELECT DDVALUE, DDTEXT FROM DDLISTS WHERE FIELDNAME='".$itemName."' ORDER BY DDTEXT ASC";
?>
<p><?= $itemName ?>
<select name="form<?= $itemName ?>">
<option value="">Select below:</option>
<?php foreach (DropDownItems($itemName, $query, $connection) as $item): ?>
<option value="<?= $item['value'] ?>"<?= (isset($_POST['form'.$itemName]) && $_POST['form'.$itemName] === $item['value'] ? ' selected' : '') ?>><?= $item['label'] ?></option>
<?php endforeach ?>
</select>
</p>
Be aware of SQL injections, if $itemName="Environment"; is user supplied you should change the code to use prepared queries.
Fixed the syntax errors. And made the code a little more readable. You can try the below code. Haven't tested it, hope it works fine.
<?php
function DropDownItems($itemName, $query, $connection)
{
$result = mysqli_query($connection, $query);
$html = '';
while ($row = $result->fetch_assoc()){
$MAS = $_POST['form'.$itemName];
$html .= "<option value=\"{$row['DDVALUE']}\" ";
$html .= ( $MAS == $row['DDVALUE'] ? 'selected' : '' ).">{$row['DDTEXT']}</option>";
}
return $html;
}
$itemName="Environment";
$query = "SELECT DDVALUE, DDTEXT FROM DDLISTS WHERE FIELDNAME='".$itemName."' ORDER BY DDTEXT ASC";
echo "<p>$itemName".": <select name='form".$itemName."'>";
echo "<option value=''>Select below:</option>";
echo DropDownItems($itemName, $query, $connection);
echo "</select>";
?>
Alright I have a hell of a problem right now .
I have a bootstrap multiselect like below which allows me to add or remove groups for an user.
Well it works but there is a bug which remove me 1 additional group each time I click on "Submit"
Example : If the user was in 3 groups( blabla, bleble and blibli) and I press Submit without even touching the groups, it will remove the user from 1 group and he will have only 2 left (blabla and bleble).
Here is the PHP code i used for treatement :
$groupes = $_GET['groupes'];
$idc = $_GET['idc'];
if ($groupes != "null") {
$g = explode(',', $groupes);
for ($i = 0; $i < count($g); $i++) {
$sqlDel = "DELETE FROM appartenir WHERE idc = $idc";
$reqDel = mysqli_query($mysqli, $sqlDel) or die('Erreur SQL !<br/>' . $sqlDel . '<br/>' . mysqli_error($mysqli));
}
for ($i = 0; $i < count($g); $i++) {
$sql1 = "INSERT INTO appartenir(idc,idg) VALUES('$idc', '$g[$i]')";
$req1 = mysqli_query($mysqli, $sql1) or die('Erreur SQL !<br/>' . $sql1 . '<br/>' . mysqli_error($mysqli));
}
}
And the HTMLcode of the select if it helps :
<select id="filtreMultiselectModif" name="groupes[]" multiple="multiple">
<?php
while ($data3 = mysqli_fetch_array($req3)) { // Remplissage du SELECT
$idg = $data3['idg'];
$intitule = $data3['intitule'];
?>
<option value="<?php echo $idg ?>" selected><?php echo $intitule ?></option>
<?php } ?>
<?php
while ($data2 = mysqli_fetch_array($req2)) { // Remplissage du SELECT
$idg = $data2['idg'];
$intitule = $data2['intitule'];
?>
<option value="<?php echo $idg ?>"><?php echo $intitule ?></option>
<?php } ?>
</select>
I don't think putting the jquery code will be useful
Any help will be appreciated !
I am struggling to get this code to work, what I want from it is to show the item (that is already in the database) to be selected in the selection form.
<label>Server Ports:</label>
<select multiple class="form-control" name="select[]">
<?php
// Get Server Information
$query = "SELECT port_no FROM _servers WHERE (server_id = '$servid') ";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$no = $row['port_no'];
}
$query = "SELECT id, name, port_no, unique_id FROM ports ORDER BY name ASC";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result))
{
$port_no = $row['port_no'];
$port_name = $row['name'];
$p_unique = $row['unique_id'];
}
?>
<option value="<?php echo $p_unique;?>"
<?php
if ($p_unique == $no) {
$check = 'selected';
} else {
$check = '';
}
echo $check;
?>
>
<?php
echo $row['name'];
?>
(
<?php
echo $row['port_no'];
?>
)</option>
<?php
}
?>
</select>
Are you sure your test must be if ($p_unique == $no) {} and not if ($port_no == $no) {} ?
If yes, try to checks your variables values :
Do a var_dump() of $no :
var_dump($no);
In your while() loop, you can also check values of $p_unique and $no like that :
var_dump($p_unique.'/'.$no);
Also, here is a simplest way to test for selected :
<option value="<?php echo $p_unique;?>" <?php if ($p_unique == $no) echo 'selected="selected"'; ?>><?php echo $row['name'] ;?> (<?php echo $row['port_no'] ;?>)</option>
I am using PHP to call the database to print 3 different dropdown menus. That works. My problem is calling the function and passing the dropdown selections into the function and displaying the records after the submit button is pressed. The function is a build query taking into account if only 1 of the dropwdowns are selected or all 3.
The function is currently in the same page as the the form.
Here is the form:
<form action="edit.php" method="POST">
<select>
<?php $getGroup = mysql_query("SELECT DISTINCT resgroup FROM restable ORDER BY resgroup");
while($viewAllGroups = mysql_fetch_array($getGroup)){
?>
<option id="<?php echo $viewAllGroups['resgroup']; ?>"><?php echo $viewAllGroups['resgroup']; ?></option><?php } ?>
</select>
<select>
<?php $getType = mysql_query("SELECT DISTINCT restype FROM restable ORDER BY restype");
while($viewAllTypes = mysql_fetch_array($getType)){
?>
<option id="<?php echo $viewAllTypes['restype']; ?>"><?php echo $viewAllTypes['restype']; ?></option><?php } ?>
</select>
<select>
<?php $getService = mysql_query("SELECT DISTINCT service FROM restable ORDER BY service");
while($viewAllServices = mysql_fetch_array($getService)){
?>
<option id="<?php echo $viewAllServices['service']; ?>"><?php echo $viewAllServices['service']; ?></option><?php } ?>
</select>
<input type="submit" class="btn btn-primary" value="Filter" />
</form>
Here is the function:
<?php
function displayrecords(){
$groups = $_POST['resgroup'];
$type = $_POST['restype'];
$service = $_POST['service'];
if($groups != ""){
$where[] = " `resgroup` = '".mysql_real_escape_string($group)."'";
}
if($type != ""){
$where[] = " `restype` = '".mysql_real_escape_string($type)."'";
}
if($service != ""){
$where[] = " `service` = '".mysql_real_escape_string($service)."'";
}
$sql_json = "SELECT * FROM mytable WHERE $where_clause ORDER BY id DESC";
}
?>
Then I try to display the function.
<?php displayrecords(); ?>
I am not getting an error, however, once the submit button clicked, the dropdown menu's clear out, and it doesn't return anything. I know I'm missing a lot. I would appreciate any help.
Thank you in advance.
First of all please provide name to each select element. Again in the the edit.php file access the values of post array by that name.
Now I am giving an example for it.
HTML part:
<select name='select1' >
<option value='1'>Value</option>
<option value='1'>Value</option>
</select>
Now in edit.php you can access the value of selected element of selectbox select1
as $_POST['select1'];
You are adding an Array into the string, which will only result in "SELECT * FROM mytable WHERE Array() ORDER BY id DESC"; or something similar.
Try to add this befor your $sql_json = "...line:
$where = implode(" AND ", $where);
This should add restype=value AND service=value etc to your string.
Additionally, you are referencing to $group instead of $groups in your if($groups != "") clause.
Also, you have to give your select tags a name to be able to reference them in $_POST:
<select name="restype">
You need to alter your PHP because the sql statement is looking for a variable $where_clause and I don't see it defined in your code.
You can rewrite the building of the where clause
<?php
function displayrecords(){
$groups = $_POST['resgroup'];
$type = $_POST['restype'];
$service = $_POST['service'];
$where = "";
if($groups != ""){
$where = " `resgroup` = '".mysql_real_escape_string($group)."'";
}
if($type != ""){
if( $where != "" ) $where .= " AND ";
$where .= " `restype` = '".mysql_real_escape_string($type)."'";
}
if($service != ""){
if( $where != "" ) $where .= " AND ";
$where .= " `service` = '".mysql_real_escape_string($service)."'";
}
$sql_json = "SELECT * FROM mytable WHERE $where ORDER BY id DESC";
}
?>
If in the browser I have parameters like:
http://localhost/specials.php?year=2009&make=honda&model=civic
and the dropdown looks something like this:
<select name="year">
<?php
$query = mysql_query("select distinct year from tbl_content where year = '$year'");
while($row = mysql_fetch_assoc($query)) {
echo "<option value=\"{$row['year']}\">{$row['year']}</option>";
}
?>
</select>
Now what I'm trying to do is show select when the dropdown options value is equal to the parameter year in the browser URL.
I tried this:
<select name="year">
<?php
$query = mysql_query("select * from tbl_year
while($row = mysql_fetch_assoc($query)) {
#=============================
if(isset($_GET['year'])) {
$year = (int)$_GET['year'];
if($year == $row['year'] { $selected = "selected"; }
else { $selected = "";
}
echo "<option value=\"{$row['year']}\" {$selected}>{$row['year']}</option>";
}
?>
</select>
Maybe try "selected='selected'" to make it valid xml.
<select name="year">
<?php
$selectedYear = NULL;
if(isset($_GET['year']))
$selectedYear = (int)$_GET['year'];
$query = mysql_query('SELECT year FROM tbl_year GROUP BY year ORDER BY year ASC');
while($row = mysql_fetch_assoc($query)) {
echo '<option value="' . htmlspecialchars($row['year']) . '"';
if($selectedYear === (int)$row['year']) {
echo ' selected="selected"';
}
echo '>' . htmlspecialchars($row['year']) . '</option>";
}
?>
</select>
Fell free to separate functionality!
<?php
function selectList($name,$values,$labels=null,$selected=null){
if($labels==null) $labels=&$values;
$data="<select name='$name'>";
foreach($values as $k=>$v){
$selected=($v==$selected)?'selected="selected"':false;
$data.="<option value='$v' $selected>".htmlspecialchars($labels[$k])."</option>";
}
$data.="</select>";
return $data;
}
$select=isset($_REQUEST['year'])?(int) $_REQUEST['year']:null;
$query=mysql_query("SELECT DISTINCT `year` FROM `tbl_year` ORDER BY `year`");
while(($row=mysql_fetch_assoc($query))!==false){
$values[]=$row['year'];
}
echo selectList("year",$values,null,$select);
?>