is there a command in php that can be used just like WHERE in a query?
i need to do a WHERE in a loop but can’t find the right statement.
Are you asking about IF operator?
If not, can you explain what problem you’re trying to solve?
I’m fairly new with php. I am trying to pass a value to a Select option tag in a form through a hyperlink using the GET method. My option values in this form are populated from a query in mysql. I can populate option values in the form but I can’t seem to pass a value to it and make it so it becomes Selected.
It can be done like this:
<?php $selected_id = $_GET['id']; ?>
<select>
<?php foreach($data as $item) { ?>
<option value="<?php echo $item['id']; ?>" <?php if ($selected_id == $item['id']) { ?>selected="selected"<?php } ?>><?php echo $item['title']; ?></option>
<?php } ?>
</select>
Show how you generate your list and I’ll help adapt that approach for your case
this would be the top part of the select script to populate option.
if(isset($_GET['jobtitleid'])){
$selectedid = $_GET['jobtitleid'];
$jobsql = "SELECT jobtitleid, jobtitle FROM jobtitle";
$jobresult= mysqli_query($dbc, $jobrsql);
while($jobr = mysqli_fetch_assoc($jobresult))
EUREKA!
I modified your code slightly and Voila! it worked great. Thank you very much.
<label for="jobtitleid" class="label">Job title</label>
<select type="text" required name="jobtitleid" class="input" id="jobtitleid"/>
<option value="select"></option>
<?php
if(isset($_GET['jobtitleid'])){
$jobsql = "SELECT jobtitleid, jobtitle FROM jobtitle";
$jobresult= mysqli_query($dbc, $jobsql);
while ($row = mysqli_fetch_array($jobresult)) {
?><option value="<?php echo $row["jobtitleid"]; ?>" <?php if($row['jobtitleid']==$_GET['jobtitleid']){?> selected<?php };?> ><?php echo $row["jobtitle"] ?></option>
<?php }
}
?>
</select>
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.