Reputation: 400
I've got a setup for showing years in my webform that updates each year that's working good.
Since I need to do math on the year value selected by the user, I've made it an INT in the MySQL database. Making the value INT returns a zero to the select field where ever the user hasn't put any information in. The user will leave some of these fields empty.
Here's the code I'm currently using to populate the year select box:
<div class="kstyle">
<label for="c02">C #02 </label>
<select id="c02" name="c02">
<option value=""></option>
<option value="<?=$row['c02'];?>" selected><?=$row['c02'];?></option>
<script>
var min = new Date().getFullYear() - 45;
cur = new Date().getFullYear();
select = document.getElementById('c02');
for (var i = min; i<=cur; i++){
var opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
</script>
</select>
</div>
This returns a zero in any INT field that doesn't have a value selected.
The issue I'm running into is an aesthetic one. I'd like the page to show a completely blank result whenever the database returns zero/NULL.
I'm working on tweaking the above code with an if - else statement but keep running into syntax errors associated with equal signs, left and/or right carrot signs. This tells me that these values are being evaluated as arithmetic, which means I probably need to tweak where I put parenthesis and/or quotes. This however is outside the bounds of what I've read in the PHP manual, so I need some help.
Here's where I'm at right now with the code:
<div class="kstyle">
<label for="c03">C #03 </label>
<select id="c03" name="c03">
<option value="<?php if($row['c03']=NULL)
{ echo '';
} else {
<$row['c03']; selected><$row['c03'];}>?>"></option>
<script>
var min = new Date().getFullYear() - 45;
cur = new Date().getFullYear();
select = document.getElementById('c03');
for (var i = min; i<=cur; i++){
var opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
</script>
</select>
</div>
If anybody could show me where I'm screwing up or a different approach to this, I'd be eternally grateful.
Upvotes: 0
Views: 2039
Reputation: 4204
Unless I'm not quite understanding what you're trying to do, it just looks like your output syntax is wrong.
Try this?
<option value="<?php
if($row['c03'] == null){
echo '">';
} else {
echo $row['c03'] . '" selected >';
}
?>
</option>
Upvotes: 2
Reputation: 56
You could check for 0 and null with:
<?= !empty($row['c02']) ? $row['c02'] : null ?>
But, why not generate all the options with PHP:
$minY = date('Y',strtotime('-45 years')); // ## 45 Years Ago
$curY = date('Y'); // ## This Year
$selY = !empty($row['c03']) ? $row['c03'] : null; // ## Resultset Year
echo "<select id=\"c03\" name=\"c03\">\n";
echo "\t<option value=\"\">Select a Year</option>\n"; // ## Blank/Prompt Option
for($i = $minY; $i <= $curY; $i++) {
$selected = ($i == $selY) ? 'selected' : null;
echo "\t<option $selected value=\"$i\">$i</option>\n";
}
echo "</select>\n";
The "\n"
(newline) and "\t"
(tab) are not necessary but result in more legible code. This should give you something like:
<select id="c03" name="c03">
<option value="">Select a Year</option>
<option value="1972">1972</option>
<option value="1973">1973</option>
...
<option value="2017">2017</option>
</select>
Upvotes: 1
Reputation: 3879
First ting first, your if condition is wrong. Compare it with ==
if($row['c03']==NULL)
Second,You say you have 0 value in the database and if condition is checking NULL, which I think is wrong. It should be
if($row['c03']==0)
And there is syntax error for option tag. It should be like this
<option value="<?php if($row['c03']==0)
{ echo '';
} else {
$row['c03'];?> selected><?php echo $row['c03'];}?>"></option>
Upvotes: 1