I am trying to figure out how to mark a dropdown option as selected by checking it's value, but the value is coming from another query.
I get the $FK_TopicID from the query called $quickedit. The dropdown list is generated by a different query called $topresult. I have an IF/ELSE statement that is supposed to print SELECTED inside of the option like <option value="the Topic ID" SELECTED> when the $FK_TopicID is equal to $row['TopicID'].
I am just not sure how to check the $FK_TopicID within the while loop for $topresult. Any ideas?
<?php
$NewsID = $_GET["n"];
$quickedit = mysql_query("SELECT * FROM News LEFT JOIN Topics on Topics.TopicID = News.FK_TopicID WHERE NewsID = $NewsID ORDER BY TopicName ASC, NewsTitle");
$row = mysql_fetch_array($quickedit);
echo "<p>" . $FK_TopicID . "</p>";
/* additional php... */
$topresult = mysql_query("SELECT * FROM Topics WHERE FK_UserID=$_SESSION[user_id] ORDER BY TopicSort, TopicName");
while($row = mysql_fetch_array($topresult)) {
if ( $row['TopicID'] == $FK_TopicID){ /* $FK_TopicID not printing value here */
$selected = " SELECTED";
} else {
$selected = "";
}
echo '<option value=\"' . $row['TopicID'] . '" ' . $selected . '>' . $row['TopicName'] . '</option>';
}
?>
I have no clue about the structures of the database, but I'll give it a shot
Does this output something you want?
<?php
$NewsID = $_GET['n'];
$quickedit = mysql_query("SELECT * FROM News LEFT JOIN Topics on Topics.TopicID = News.FK_TopicID WHERE NewsID = $NewsID ORDER BY TopicName ASC, NewsTitle");
$topresult = mysql_query("SELECT * FROM Topics WHERE FK_UserID=$_SESSION[user_id] ORDER BY TopicSort, TopicName");
echo "<p>" . $FK_TopicID . "</p>";
while($row = mysql_fetch_array($quickedit)) {
while($row2 = mysql_fetch_array($topresult)) {
if ( $row2['TopicID'] == $row['FK_TopicID']){
$selected = " SELECTED";
} else {
$selected = "";
}
echo '<option value=\"' . $row2['TopicID'] . '" ' . $selected . '>' . $row['TopicName'] . '</option>';
}
}
?>
This code loops through both queries and if the TopicID from $topresult is equal to the FK_TopicID from $quickedit, it will be selected.
Related
I have a DB table with all categories (id, category) and table with all of events (id, event, categoryID).
And on the event editing form I have the select field with all of the categories (getting from the DB). But sinse I'm developing an editing form, I need to select current category by default.
This is my select field (PHP method that gets all the categories from DB and puts them in the following order):
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
Let's say, current event is under category 3, so I need the following HTML to be generated:
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3" selected>Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
How do I achieve it with PHP, if I have the catID?
Hopefully, this question is clear enough. Sorry for my bad explanation
UPD: This is my PHP code that generates category list:
public function getCatList($conf) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
}
Appending to your while iteration a condition will solve this for you:
while($row = mysqli_fetch_array($result)) {
$isSelected = $row['id'] == $catID;
echo '<option '.($isSelected ? 'selected="selected"' : '').' value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
You're making a comparison if the current value is the same as that stored in $catID - and store the boolean result in a variable. In the echo you're just doing a conditional forking and appending the selected attribute if the value was true, otherwise not appending any empty string.
You can do it like
while($row = mysqli_fetch_array($result)) {3
echo '<option value="' . $row['id'] . '"';
//if condition is met then make the option selected
if($row['categoryID'] == 3) {
echo " selected='selected' ";
}
echo '>' . $row['category'] . '</option>';
}
you should add the selected catID as a parameter to your getCatList-function. Then just change the creation of your HTML to include the selected-attribute:
public function getCatList($conf, $catID = 0) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"' . ($row['id']==$catID?' selected':'') . '>' . $row['category'] . '</option>';
}
}
now you can just pass the catID to your function:
$myConf = "something";
$myCatID = 3;
getCatList($myConf, $myCatID);
//query for the categories and options
$current_value = 3;
$total_num_of_options = mysql_num_rows($your_query_result);
for($count = 1; $count <= $total_num_of_options){
echo "<option value='".$count."' ";
if($count == $current_value)
echo "selected";
echo ">cat".$count."</option>";
}
I have two dropdowns, one displays States from HTML SELECT, and the next one populates from MYSQL query of markets matched to the state selected. After selection of the Market, the SID should appear. The problem is the code remembering the $pickstate var when you select the $market var so Mysql query can display the SID result.
<?php
// Get State from above form.
$pickstate = $_POST['state'];
$result = mysql_query("SELECT Market FROM ppc WHERE State='" . $pickstate . "'");
// After State is selected from MySQL, populate Market Dropdown.
echo '<form style="display:inline-block;" action="" method="POST"><select id="Market" name="Market" onchange="this.form.submit();">';
echo '<option value="">Select Your Market</option>';
while ($row = mysql_fetch_array($result)) {
echo ( '<option value= "' .$row['Market']. '">'.$row['Market'].'</option>' );
}
echo "</select></form><br />";
// Get SID Result
$market = $_POST['Market'];
$sid = mysql_query("SELECT SID FROM ppc WHERE State='" . $pickstate . "' AND Market='" . $market . "'");
// This is for debugging only.
// This shows, until market is selected.. then vanishes.
echo $pickstate . "<br />";
// This shows after market has been chosen.
echo $market . "<br />";
// "SHOULD" Display SID.
if ($state != null && $market != null) {
echo '<p style="display:inline-block;margin:0;padding:0;"> Use: ';
}
while ($row = mysql_fetch_array($sid)) {
echo $row['SID'] . ' ';
}
?>
it will not work like that .
the query is server side . you may consider to send your selected value to another page and then get values from your query
OR you can use ajax .
I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.
I have trying to print the selected value in the drop down list, but in vain. I am new to php and html, so this might sound like a stupid question but please help me out! this is my code:
echo '<tr><td>Client:</td><td><select name="client_name">';
$sql = mysql_query("SELECT * FROM client");
$s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");
// $s2= mysql_fetch_array($s);
while ($row = mysql_fetch_array($sql))
{
while ($s2==mysql_fetch_array($s))
{
if ($row['client_id'] == $s2['client_id'])
$selected = "selected=\"selected\"";
else
$selected = " ";
}
echo '<option value="' . $row['client_id'] . '" ' . ($selected == $row['client_id'] ? ' selected' : '') . '>' . $s2['client_name'] . '</option>';
}
This code doesnt work. Please help me out! Is there a different way to do it?
Try this
echo '<tr><td>Client:</td><td><select name="client_name">';
$sql = mysql_query("SELECT * FROM client");
$s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");
// $s2= mysql_fetch_array($s);
while ($row = mysql_fetch_array($sql))
{
while ($s2==mysql_fetch_array($s))
{
if ($row['client_id'] == $s2['client_id'])
$selected = "selected=\"selected\"";
else
$selected = " ";
}
echo '<option value="' . $row['client_id'] . '" ' . $selected . '>' . $s2['client_name'] . '</option>';
}`
This query is a problem
$s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");
you are trying to get two client ids project.client_id and client.client_id, in such cases you should give aliases like project.client_id as project_client_id and client.client_id as client_client_id, then use that value to compare in while loop.
Note use whichever client id you want either project_client_id or client_client_id.
I want to search a table by inputing a random number for the ID, and for it to be successful, it has to match the specified tag. So far I have:
$query = "SELECT * FROM web_db WHERE P_Id = " . $random;
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result)){
$name = $row[$id];
echo 'ID:' . $name . '<br>';
$name = $row[$url];
echo 'URL: ' . $name . '<br>';
$name = $row[$tag];
echo 'Tag:' . $name . '<p>';
}
}
This brings up one entry, any tag. How can I have it repeat until Tag matches a specified value?
You don't. SELECT statement returns everything that matches the followed conditions. So, if you want to query for a specific tag entry disregarding the P_Id, do this :
$query = "SELECT * FROM web_db WHERE tag = '".$tag."' ORDER BY RAND() LIMIT 1";
RAND() in this case will order the list randomly, while the query returns the first result that matches the tag used.
$result = mysql_query($query);
if(count($result) > 0) {
while($row = mysql_fetch_array($result)) {
echo 'ID:' . $row['id'] . '<br>';
echo 'URL: ' . $row['url'] . '<br>';
echo 'Tag:' . $row['tag'] . '<p>';
}
} else {
echo 'no entries found';
}
if("sometag" === $name) break;
that exits the while loop. after you exit, you should check again to see if the tag was found or not