Using a href in select box to load data

Hello. What I am trying to do is to have a select box listing page from a database. When an option is clicked the page is loaded. I have a select box, and but I get a syntax error in my editor (Aptana studio 3).

The HTML code:

<nav class="navigation" role="navigation">
			
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<?php 
				
							$q = "SELECT * FROM pages";
							$r = mysqli_query($dbc, $q);
							echo "<select>";
							while($nav = mysqli_fetch_assoc($r)) {
								
								echo "<option value='<a href='?page='.$nav['id'].'>'.$nav['title'].'</a>'>;'>";
											
											
								echo "</option>";
							}
							echo '</select>';
						?>
					</div>
				</div>
			</div>
			
    	</nav><!-- navbar end: -->

It does not state where in the code the error is so I cannot correct it.

Any help with be appreciated.

As far as i know you can not put links into options according to the html standard but you can use a little javascript to let it work.

<select onchange="javascript:handleSelect(this)">
    <option value >Choose one:</option>
    <option value="home.php">Home</option>
    <option value="contact.php">Contact</option>
</select>

<script type="text/javascript">
    function handleSelect(elm)
    {
    	if(elm.value) {
        	window.location = elm.value;
        }
    }
</script>

On the other hand it looks like you are using bootstrap. Why don’t you just use the bootstrap dropdown-menu instead of a select?

I am also missing any error handling on the mysqli_query() function that could give a false as return value

Last but not least a good practice: Handle your functional php first before starting the output. Which in this case means that you should execute your query before you start the output…

I have seen html a href used in a select box but the code was to load data - as you would use in a CRUD system to “SELECT FROM ,”. I never thought about a drop down list so will try that. As I am new to PHP I have no idea how or which error message to use, but thanks for the advice.

Just like this in the most simple way:

$r = mysqli_query($dbc, $q);
if($r === false) {
    printf("Error: %s\n", mysqli_error($dbc));
}

I was wondering if you could help further. I took your advice and tried to use a drop down menu, but I get an error message.

The new html code:

        <nav class="navigation" role="navigation">
			
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<?php 
				
							$q = "SELECT * FROM pages";
							$r = mysqli_query($dbc, $q);
							
							if($r === false) {
							    printf("Error: %s\n", mysqli_error($dbc));
							}
							
							echo "<ul class='nav navbar-nav navbar-right'>";
							while($nav = mysqli_fetch_assoc($r)) {
								
								echo "<li>";
								
								echo "<a href='?page='.$nav[id]>$nav[title]";
								
								echo "</a>";
								
								echo "</li>";

							}
							echo "</ul>";
						?>

					</div>
				</div>
			</div>
			
    	</nav><!-- navbar end: -->

Error message: Warning : mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\dynamic\egypt\functions\data.php on line 9

What does this mean and how do I solve it.

Thanks for any help you give.

It means that the query failed just before mysqli_fetch_assoc() .

If a query fails then mysqli_query() will return FALSE. In the next line if you want to fetch your data you give that FALSE as a parameter to the function mysqli_fetch_assoc(). But mysqli_fetch_assoc() expects a mysqli_result.

Long story short: Where is line 9 in your code? Probably you should do there some error handling as well?

Sponsor our Newsletter | Privacy Policy | Terms of Service