I am trying to embed a drop down list to an update page, it works fine but I am having problems with option selected part. When an option is shown it also duplicates it in the list.
Is there any way I can say if record is used do not use it again with PHP?
<?php
$sql = "SELECT TeamName, TeamID FROM tblTeam";
$result = mysql_query($sql);
$player_id = $_GET['id'];
$current_team = mysql_query("SELECT
tblteam.TeamID,
tblteam.TeamName,
tblplayer.PlayerID,
tblplayer.PlayerTeam,
tblplayer.PlayerName
FROM
tblplayer
INNER JOIN tblteam ON tblplayer.PlayerTeam = tblteam.TeamID
WHERE PlayerID = $player_id LIMIT 1 ");
$my_row = mysql_fetch_array($current_team);
?>
<select name="TeamName">
<option selected value="<?php echo $my_row['TeamID']; ?>"> <?php echo $my_row['TeamName']; ?> </option>
<?php
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
echo "<option value=\"$team_id\">$team_name</option>";
}
echo "</select>";
?>
It seems like simple if condition will solve the case for you:
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
if($team_id != $my_row['TeamID']){
echo "<option value=\"$team_id\">$team_name</option>";
}
}
Additionally you should always sanitize $_GET / $_POST params, in your example:
$player_id = intval($_GET['id']);
Intval will return 0 if the given format is not numeric, so your sql query is safe from this moment.
Related
I would like to filter some data from MySQL database base on a drop-down list that populating from the MySQL database.
Now I have got a drop-down list like this:
which is populating from the MySQL database:
The following are my code for the drop-down list:
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
?>
Does anyone know how can I get the selected text of the drop-down menu?
I have tried to use $_POST[] in different ways like $_POST['id']
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
echo $_POST['id'];
?>
then Notice pop up from the website:
Notice: Undefined index: id
What should I do?
When I select checkbox or mulitple checkbox it only return 1 row of my database data, how to show all data from database?
<?php foreach($portControllerClass->getAllAgencies() as $port_list){ ?>
<input type="checkbox" name="port[]" value="<?=$port_list['agencies']?>"> <?=$port_list['agencies'] . '</br>';
} ?>
$date_from = date('Y-m-d', strtotime($_POST['date_from']));
$date_to = date('Y-m-d', strtotime($_POST['date_to']));
foreach($_POST['port'] as $port){
$sql = "SELECT #a:=#a+1 no, letter_no, letter_date, conformity_date,
agencies, DATEDIFF(`conformity_date`,`letter_date`) AS DiffDate
FROM info_lab, (SELECT #a:= 0) AS a
WHERE agencies LIKE '%". implode(",",$_POST['port']) ."%'
and conformity_date BETWEEN '".$date_from."'
and '".$date_to."'";
$query = $con->query($sql);
$row = $query->fetch_array();
echo $row['letter_no'];
}
You have only 1 input checkbox, so you can get only one value. To get multiple values, I think you should use foreach loop with checkbox values from database like below:
foreach ($port_list['agencies'] as $value){
<input type="checkbox" name="port[]" value="<?php echo $port_list['agencies']?>"> <php echo $port_list['agencies'] . '</br>'; ?>
}
ALso, please dont use <?= tag as it is deprecated, use <?php ?> instead
Instead of using $_POST['port'] in your query use $port.
So your query would become:
$sql = "SELECT #a:=#a+1 no, letter_no, letter_date, conformity_date,
agencies, DATEDIFF(`conformity_date`,`letter_date`) AS DiffDate
FROM info_lab, (SELECT #a:= 0) AS a
WHERE agencies LIKE '%".$port."%'
and conformity_date BETWEEN '".$date_from."'
and '".$date_to."'";
And if there is more than one row for each data then add this bit of code after $query = $con->query($sql);
if ($query->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['letter_no'];
}
} else {
echo "0 results";
}
I wonder if anyone can help.
I am trying to echo some SQL data into a <select> form with each <option> as a different row from the table.
For example if my table has two columns 'username' and 'category' and there are two rows for the same username, with the data:
"username: test, category: test1."
& second row as:
"username: test, category: test2."
How could I echo 'test1' and 'test2' as two options in a select form?
The table name is 'testtable' so my current $sql query is ("SELECT 'category' FROM 'testtable' WHERE 'username = \'$user\'")
$user is currently set to the $_SESSION['username']
Code examples would be really helpful and much appreciated. I just cant seem to get them to echo in examples I have found on forums.
try this
<select name="your_select_name" id="your_select_id">
<option value="">Select</option>
<?php
$username = $_SESSION['username'];
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo '<option value="'.$row['category'].'">'.$row['category'].'</option>';
}
?>
</select>
UPDATE 2:
For distinct category use this
$res = mysql_query("SELECT DISTINCT(`category`) as category FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
OR
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' GROUP BY category ") or die(mysql_error());
Try this:
<select>
<?php while($row = mysql_fetch_array($result)
{?>
<option><?php echo $row['category'];?></option>
<?php }?>
</select>
You got from your query all rows:
<?php
$query = "SELECT category FROM testtable WHERE username = '" . $_SESSION['username'] ."'";
$result = mysql_query($query);
$options = array();
while($row = mysql_fetch_array($result))
{
$options[] = "<option>" . $row['category'] . "</option>";
}
?>
<select>
<?php echo implode("",$options); ?>
</select>
Your select query has an error: unpaired single quote before username, and single quotes surrounding category - is that possible that was the reason you couldn't do it?
<?php
$result = mysql_query($query);
echo "<select>";
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['category']."</option>";
}
echo "</select>";
?>
Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>
My page stops loaded every time I turn on the code below... it looks correct and the tables and fields are correct.
<select name="common" style="width: 136px;">
<?php
$group1 = mysql_fetch_array(mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order"));
while($row = $group1){
echo "<option value=\"$group1\">$group1</option>\n";
}
?>
</select>
<?php
$group1 = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order");
while($row = mysql_fetch_array($group1)){
echo "<option value=\"$row[country]\">$row[country]</option>\n";
}
?>
Try this:
<select name="common" style="width: 136px;">
<?php
$recordset = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order") or die("Error found: " . mysql_error());
while($row = mysql_fetch_array($recordset)){
echo "<option value=\"".$row['country']."\">".$row['country']."</option>\n";
}
?>
</select>