This is likely a lot simpler than I'm thinking but I can't seem to figure it out.
I have the following code that's part of a image results list:
<option value="30" <?php echo ($_SESSION['results']== 30) ? 'selected' : ''; ?>>30</option>
<option value="40" <?php echo ($_SESSION['results']== 40) ? 'selected' : ''; ?>>40</option>
<option value="50" <?php echo ($_SESSION['results']== 50) ? 'selected' : ''; ?>>50</option>
<option value="60" <?php echo ($_SESSION['results']== 60) ? 'selected' : ''; ?>>60</option>
<option value="70" <?php echo ($_SESSION['results']== 70) ? 'selected' : ''; ?>>70</option>
<option value="80" <?php echo ($_SESSION['results']== 80) ? 'selected' : ''; ?>>80</option>
<option value="90" <?php echo ($_SESSION['results']== 90) ? 'selected' : ''; ?>>90</option>
The default of course is 40 results but I want the default to be 80. How do I make it so 80 is automatically selected by default
A cleaner way to do this would be:
<?php
session_start(); //Start session
$_SESSION['results'] = $_SESSION['results'] ? $_SESSION['results'] : 80; //Check and set the session variable else default it to 80
$numbers = [30, 40, 50, 60, 70, 80, 90]; //Initialize your array
?>
<select name="test">
<?php
foreach ($numbers as $num) { //Loop through the array and populate your select box.
?>
<option value="<?php echo $num; ?>" <?php echo ($_SESSION['results'] == $num) ? 'selected' : ''; ?>><?php echo $num; ?></option>
<?php
}
?>
</select>