This code in PHP always returns false.
I think it's a matter of how I compare values:
if (isset($_GET['materia']) and $_GET['materia']==$row['id_materia']){
echo'<option selected="selected" value="http://www.gonzalohll.com/pedidos.php?curso=1&?materia='.$row['id_materia'] .'">'. $row['materia'] .'</option>';}
else {
echo'<option value="http://www.gonzalohll.com/pedidos.php?curso=1&?materia='.$row['id_materia'] .'">'. $row['materia'] .'</option>';}
}
echo '</select>';
Any help on the subject is much apreciated.
You need to check the values for the variables that you are using, for see whats happening in your "if" statements.
For example, try this:
$id_materia = $row['id_materia'];
if (empty($id_materia)){
echo 'Incorrect value for id_materia';
} else {
echo 'id_materia: ' . $id_materia;
}
if (isset($_GET['materia'])){
echo ". Value for materia (GET): " . $_GET['materia'];
} else {
echo ". Materia (GET) not exists";
}
echo '<select>';
if (isset($_GET['materia']) && $_GET['materia'] == $row['id_materia']){
echo'<option selected="selected" value="http://www.gonzalohll.com/pedidos.php?curso=1&?materia='.$row['id_materia'] .'">'. $row['materia'] .'</option>';}
else {
echo'<option value="http://www.gonzalohll.com/pedidos.php?curso=1&?materia='.$row['id_materia'] .'">'. $row['materia'] .'</option>';}
}
echo '</select>';
Related
when i click the edit button it should go the edit page
when i want to edit the row with coursename="php" in the drop down the course name should have selected as php since the course name and the paper name,paper description are present in the different table i cant select the particular value in drop down
if(isset($_GET['id'])) {
$table="papers";
$condition="paper_id=".$_GET["id"]."";
$select=selectlist($table,$condition);
$row=$list->fetch_array();
$table="courses";
$datalist=selectdata($table);
$data=Selectdata($table);
while($row1=$result->fetch_assoc())
{
$coursename=$row1['course_name'];
$courseid=$row['course_id'];
$paper=$row['paper_name'];
$paperdesc=$row['paper_description'];
$_SESSION['pid']=$row['paper_id'];
echo '<option value="'. $row1['course_id'].'" selected="'. $row1['course_id'].'=='.$courseid.'">'.$row1['course_name'].'</option>';
}
}
?>
i have tried the above code i know the problem is with the "selected" please help me
You are almost close to the answer.
Some modifications and you are done.
You need to compare two values.
The value from database (selected value) and the value in loop.
Another suggestion is that we should not write any logic inside any
HTML tag.
HTMLs/Templates are just for printing output to screen.
We should first evaluate all conditions and then print HTML.
selected Reference
Change
echo '<option value="'. $row1['course_id'].'" selected="'. $row1['course_id'].'=='.$courseid.'">'.$row1['course_name'].'</option>';
To:
$selected = ($row1['course_id'] == $courseid) ? 'selected="selected"' : '';
echo '<option value="'. $row1['course_id'].'" ' . $selected . '>'.$row1['course_name'].'</option>';
Replace your while loop:
<?php
while($row1=$result->fetch_assoc())
{
$coursename=$row1['course_name'];
$courseid=$row['course_id'];
$paper=$row['paper_name'];
$paperdesc=$row['paper_description'];
$_SESSION['pid']=$row['paper_id'];
if($courseid == $row1['course_id']) {
echo '<option value="'. $row1['course_id'].'" selected>'.$row1['course_name'].'</option>';
} else {
echo '<option value="'. $row1['course_id'].'" >'.$row1['course_name'].'</option>';
}
}
?>
You can do it as following:
while($row1=$result->fetch_assoc())
{
$coursename=$row1['course_name'];
$courseid=$row['course_id'];
$paper=$row['paper_name'];
$paperdesc=$row['paper_description'];
$_SESSION['pid']=$row['paper_id'];
$selected="";
if($row['course_id']==$course_id)
{
$selected="selected";
}
echo '<option value="'. $row1['course_id'].'" $selected>'.$row1['course_name'].'</option>';
}
This makes your job done
while($row1=$result->fetch_assoc())
{
$coursename=$row1['course_name'];
$courseid=$row['course_id'];
$paper=$row['paper_name'];
$paperdesc=$row['paper_description'];
$_SESSION['pid']=$row['paper_id'];
echo "<option value = '{$row1['course_id']}'";
if ($courseid == $row1['course_id'])
echo "selected = 'selected'";
echo ">{$row1['course_name']}</option>";
}
Try this (use ternary operator inside option)
echo '<option value="'. $row1['course_id'].'" selected="'. $row1['course_id'].'=='.$courseid.'">'.$row1['course_name'].'</option>';
change into
<option value="<?= $row1['course_id'] ?>" <?= $row1['course_id']==$courseid? "selected":""?> > <?= $row1['course_name'] ?> </option>';
Hello guys I want to get data from the DB to set as default/selected in my dropdown.
I have a page named Update Project, in that page I have a dropdown component that is filled by records from my data, however I want to set the selected value from the query result.
This is what I've tried so far:
$st_ac = $conn->prepare( "SELECT p.project_code, m.type
FROM tblprojects as p
JOIN tblprojectsmaster as m
ON p.project_code = m.project_code
WHERE p.project_code = :code" );
$st_ac->execute(array(':code' => $code));
$result = $st_ac->fetch(PDO::FETCH_ASSOC);
$ptype = $result['type']; // the data to be selected in the dropdwon
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value"' . $type . '"';
if($ptype == $type)
echo 'selected="selected"';
echo '>' . $description . '</option>';
echo '<br />';
?>
<option value="<?php echo $type; ?>"><?php echo $description; ?></option>
<?php
}
?>
But I can't be able to achieve what I want. Any ideas? Your help will be truly appreciated. Thanks.
Try this
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value="'.$type.'"';
if($ptype == $type)
{
echo 'selected="selected"';
}
echo '>'.$description.'</option>';
}
?>
May be you can use like this
echo '<option value="' . $type . '"'; if($ptype == $type) { echo 'selected="selected" } >'; echo $description . '</option>'; echo '<br />';
Firstly apologies for asking this as it is quite basic but
I just can't seem to get it right. Have searched on here and
elsewhere for an answer (and tried various) but there is always
an error. Spent too long on this little bit and should really know
the answer but here goes:
Ok I have a main php file using an include statement to bring in a drop down menu
with the options being populated from a MySQL database. In the file being included I have this while loop which creates the
options and works fine:
while ($db_field = mysql_fetch_assoc($result)) {
$ManList2 = $db_field['categoryName'];
echo '<option value="' . $ManList2 . '">' . $ManList2 . '</option>';
}
What I want to add is something like the following in the option tag:
if($search == '$ManList2') { echo 'selected'; }
I just can't seem to get it right in the echo statement.
Any help greatly appreciated.
echo '<option value="'.$ManList2.'" '.($search == $ManList2 ? 'selected' : '').'>'.$ManList2.'</option>';
How about something like this:
while ($db_field = mysql_fetch_assoc($result)) {
$ManList2 = $db_field['categoryName'];
echo '<option value="' . $ManList2 . '"';
if($search == '$ManList2') {
echo ' selected';
}
echo '>' . $ManList2 . '</option>';
}
echo "<option value='".$ManList2; if($search == '$ManList2'){ echo 'selected'; } echo "'>".$ManList2."</option>";
You may try something like this (Assumed your select's name is search)
$search = isset($_POST['search']) ? $_POST['search'] : ''; // or $_GET maybe
$selected = '';
while ( $db_field = mysql_fetch_assoc($result) ) {
$ManList2 = $db_field['categoryName'];
$selected = $search == $ManList2 ? 'selected' : '';
echo '<option '.$selected.' value="'.$ManList2.'">'.$ManList2.'</option>';
}
You could use variables to put into your PHP, like:
while($db_field = mysql_fetch_assoc($result)) {
$ManList2 = $db_field['categoryName'];
$selected = $search === $ManList2 ? "selected='selected'" : '';
echo "<option value='$ManList2'$selected>$ManList2</option>";
}
I don't see what $search does, so this approach may not work. I would use my PHPglue Library. It handles this sort of thing for you.
Once it loads in my page, if nothing has been saved in the DB table, all options are shown. As soon as i make a selection and reload the page, the selected option dissapears from the list and isn`t reloaded in the dropdown. Instead, it displays the next value which takes the place of the selected one.
if i check the SQL statement and the $str, it does load all the options except the one which is selected which is in $getBris (it has a value).
What could be causing my select to not display my selected option and instead removing it from the list?
*It specifically doesnt work in IE8, wasnt working in Firefox but now it does
<script src="validation.js" type="text/javascript"></script>
<html>
<body onLoad="checkSecondValue();">
</body>
</html>
<?php
//retrieve all the bris for the drop down
include '../../inc/database.php';
// ORDER BY RAND()
$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";
$getBris = $_GET['bris'];
$getBris = $getBris - 1;
print_r("bris is : "+ $getBris);
if($getBris == null)
{
$str .= "<option value='' selected></option>";
}
else
{
$str .= "<option value='999'>Choisir un bris</option>";
}
$i = 0;
while($data = mysql_fetch_assoc($res))
{
if($data['F_BRISID'] == $getBris)
{
$str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
}
else
{
$str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
}
$i++;
}
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
$str .= "</select>";
echo $str;
if(is_numeric($bris))
{
echo "<script type=\"text/javascript\">alert('test');checkSecondValue();</script>";
}
?>
Use your browser's View Source feature to inspect the actual HTML you are generating (which is, in fact, the only see the browser ever sees). It looks like you're inserting random single quotes.
Update:
<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>"
... will render as:
<option value='blah' selected '>blah</option>
It's the only error I've cared to spot but an HTML validator should find them all. Also, I recommend you use this syntax:
<option value="blah" selected="selected">blah</option>
A construct like this
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
is highly wasteful of space and forces you to duplicate a big chunk of html whose only difference is the "selected" attribute. Why not do something like this:
$selected = ($getBris == 12) ? ' selected' : '';
$str .= "<option value=12{$selected}>Autre</option>";
Weird issue the country names are no longer being listed in the select dropdown box they're just spilling out all over the page. can anyone spot the coding error that is causing it?
echo "<select name=recordcountry style='width: 136px;'>";
//echo "<option value=$country selected=selected>-- Select --</option>";
echo "<option ". ($data['recordcountry'] == "" ? 'selected=selected>-- Select --</option>' : 'value="' .$data['recordcountry']. '" selected=selected');
$group1 = '<optgroup label=Common>';
$group2 = '<optgroup label=Alphabetically>';
$group = mysql_query("SELECT country, grouping, p_order FROM mast_country
WHERE grouping IN ('1','2') ORDER BY p_order");
while($row = mysql_fetch_array($group))
{
if ($row['grouping'] == '1')
{
$group1 .= '<option value="'.$row['country'].'">'.
$row['country'].'</option>';
}
else
{
$group2 .= '<option value="'.$row['country'].'">'.
$row['country'].'</option>';
}
$group1 .= '</otpgroup>';
$group2 .= '</otpgroup>';
echo $group1;
echo $group2;
echo "</select>";
}
It's </optgroup>, not </otpgroup>.
echo "<option ". ($data['recordcountry'] == "" ? 'selected=selected>-- Select --</option>' : 'value="' .$data['recordcountry']. '" selected=selected');
makes no sense, too. It is highly confusing and you are not adding </option>.
Here's a better version:
echo '<option value="'.$data['recordcountry'].'"'.($data['recordcountry'] ? '' : ' selected="selected"').'>'.($data['recordcountry'] ? $data['recordcountry'] : '--Select--').'</option>';
Or even better, split it into two lines and use an IF statement:
if($data['recordcountry'])
echo '<option value="'.$data['recordcountry'].'">'.$data['recordcountry'].'</option>';
else
echo '<option value="" selected="selected">--Select--</option>';
I think the problem is in this line.
echo "<option ". ($data['recordcountry'] == "" ? 'selected=selected>-- Select --</option>' : 'value="' .$data['recordcountry']. '" selected=selected');
No end tag is added when $data['recordcountry'] == "". Should be something like this i think.
echo "<option ". ($data['recordcountry'] == "" ? 'selected=selected>-- Select --</option>' : 'value="' .$data['recordcountry']. '" selected='selected'>" . $data['recordcountry'] . "</option>");
Edit: And what ThiefMaster said