Putting some PHP code into a drop down menu!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Putting some PHP code into a drop down menu!

Post by aaron101 »

Hi!

I'm sure someone on here can help me. I'm pretty new to PHP (been learning for a couple of weeks) and am trying to populate ONE drop down box with info from a MYSQL database.

The problem is that instead of all information going into one drop down box, each row of information is displayed in its own drop down box! I feel all I need is like a small change to my code, but haven't got a clue what to do. :?

I have also attached a screenshot.

Someone please help me!

<?php

require_once ('mysqli_connect.php'); // ACTUAL QUERY
$q = "SELECT CONCAT(route) as flight FROM flight_details";
$r = @mysqli_query ($dbc, $q);
if ($r) {

echo '<table align="left" cellspacing="3" cellpadding="3">
<tr><td><b>Flights:</b></td></tr>'; // TABLE HEADER

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // HERE THE ROUTES ARE PRINTED

echo '<tr><td align="left"><select name="flight"><option>' .
$row['flight'] .'</option></select></td></tr>';

}

echo '</table>';
mysqli_free_result ($r);
} else { // ROUTES CANNOT BE RETRIEVED

echo '<p class="error"> Flight details could not be retrieved. Please try again later.</p>';

echo '<p>' . mysqli_error($dbc) .
'<br /><br />Query: ' . $q .
'</p>';
}
mysqli_close($dbc);
?>
Attachments
Screenshot.png
Screenshot.png (101.08 KiB) Viewed 523 times
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Putting some PHP code into a drop down menu!

Post by josh »

Your select tag is inside the loop
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Re: Putting some PHP code into a drop down menu!

Post by aaron101 »

Ok cool, will try moving the tags in the morning! This may sound stupid, but where in the code outside the loop would you move the select tags?

thanks
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Putting some PHP code into a drop down menu!

Post by josh »

Before the loop starts, as close to the loop as possible so it is easy to read and understand the code.

1) echo opening <select>
2) loop through result set outputting <option> tag for each element of the drop down
3) exit loop and echo closing select tag
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Re: Putting some PHP code into a drop down menu!

Post by aaron101 »

It worked! :D

Every thing's in one place as it should be now, thanks very much!
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Re: Putting some PHP code into a drop down menu!

Post by aaron101 »

Since correcting the initial problem, I've run into another. I don't know how to enter the selection made in the drop down into the database as it always tries to enter "" into my route field. Can anyone help with this?


<?php

$page_title = 'Booking';

include ('includes/header.html');


if (isset($_POST['submitted'])) {
$errors = array();

if (empty($_POST['first_name'])) { // CHECKING FIRST NAME
$errors[] = 'You forgot to enter your first name.';
} else {
$f = trim($_POST['first_name']);
}

if (empty($_POST['last_name'])) { // CHECKING LAST NAME
$errors[] = 'You forgot to enter your last name.';
} else {
$l = trim($_POST['last_name']);
}

if (empty($_POST['email'])) { // CHECKING EMAIL
$errors[] = 'You forgot to enter your email address.';
} else {
$email = trim($_POST['email']);
}

if (!empty($_POST['pass1'])) { // CHECKING THE PASSWORDS MATCH
if ($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Your passwords do not match.';
} else {
$p = trim($_POST['pass1']);
}

} else {
$errors[] = 'You forgot to enter your password.';
}

if (empty($errors)) {

require_once // USER ENTERED INTO DATABASE
('mysqli_connect.php');
$q = "INSERT INTO booking_details (route, first_name, last_name, email, pass
) VALUES (
'$route', '$f', '$l', '$email', SHA1('$p'), NOW() )";

$r = @mysqli_query ($dbc, $q);

if ($r) {
echo '<h1>Thank you!</h1>
<p>Your flight has now been booked.</p><p><br /></p>';

} else { // FAILURE
echo '<h1>System Error!</h1>

<p class="error">Your flight has not been booked due to an error. Please try again</p>';

echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
}
mysqli_close($dbc); // CONNECTION TO DB CLOSED

include ('includes/footer.html');

exit();

} else {
echo '<h1>Error!</h1>

<p class="error"> The following error(s) occurred:<br />';

foreach ($errors as $msg) {

echo " - $msg<br />\n";

}

echo '</p><p>Please try again.</p><p><br /></p>';

}
}

?>

<h1>Book your flights!</h1>

<form action="booking.php" method="post">

<?php

require_once ('mysqli_connect.php'); // ACTUAL QUERY
$q = "SELECT CONCAT(route) as flight FROM flight_details";
$r = @mysqli_query ($dbc, $q);
if ($r) {

echo '<select name="flight"><table border="1" align="left" cellspacing="3"
cellpadding="3">
<tr><td><b>Flights:</b></td></tr>'; // TABLE HEADER

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // HERE THE ROUTES ARE PRINTED

echo '<tr><td align="left"><option>' .
$row['flight'] .'</option></td></tr>';
}

echo '</select></table>';
mysqli_free_result ($r);
} else { // ERROR IS ROUTES CANNOT BE RETRIEVED

echo '<p class="error"> Flight details could not be retrieved. Please try again later.</p>';

echo '<p>' . mysqli_error($dbc) .
'<br /><br />Query: ' . $q .
'</p>';
}
mysqli_close($dbc);
?>
<table>
<tr>
<td>

<b>First name: <input
type="text"
name="first_name"
size="15"
maxlength="20"
value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<b>Last name: <input
type="text"
name="last_name"
size="15"
maxlength="40"
value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

<b>Email Address: <input
type="text"
name="email"
size="20"
maxlength="80"
value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<b>Password: <input
type="password"
name="pass1"
size="10"
maxlength="20" /></p>

<b>Confirm password: <input
type="password"
name="pass2"
size="10"
maxlength="20" /></p>

<p><input type="submit"
name="submit"
value="register" /></p>

<input type="hidden"
name="submitted"
value="TRUE" />

</form>

<?php

include ('includes/footer.html');

?>


josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Putting some PHP code into a drop down menu!

Post by josh »

Did you make a form tag, submit button, and check the $_POST and $_GET array? echo out your query before you execute it to make sure the variables are being populated, if not trace your logic, just like you would back trace your steps if you lost your wallet on the way home from the store.
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Re: Putting some PHP code into a drop down menu!

Post by aaron101 »

Realised my mistake.. In the query, I was looking for information to be taken from 'flight' when it should have been 'route'. Now all is working :)

Thanks again!
aaron101
Forum Newbie
Posts: 6
Joined: Fri Nov 28, 2008 5:18 pm

Re: Putting some PHP code into a drop down menu!

Post by aaron101 »

Have another question... I've populated another drop down box with prices of stuff. Is there anyway I can get it to be displayed in '£'? Seems theres no way to do it in the actual database so was wondering if it's possible in php?
PHPyrox
Forum Newbie
Posts: 4
Joined: Sat Nov 29, 2008 12:45 pm

Re: Putting some PHP code into a drop down menu!

Post by PHPyrox »

aaron101 wrote:Have another question... I've populated another drop down box with prices of stuff. Is there anyway I can get it to be displayed in '£'? Seems theres no way to do it in the actual database so was wondering if it's possible in php?
Guess it depends on whether you are going to re-calculate the valuta or just put a "£" next to the price...

If you're just going to put "£ 1,234" it should be as simple as:
echo '£' . $price;

and later on strip the £ when you are going to input things into the database simple with:

//Puts a '£' before the number
$price = someprice;
$price = '£' . $price;

//Stripping the $price for it's first character (£)
//Might be necessary to input the number as an integer into the DB
$price_to_insert = substr($price,1)
$price_to_insert = (int)$price_to_insert;

This should be easy even within a while-loop for the drop-down box...

Hope it helped
Post Reply