Afternoon all,
A very quick question... a friend has set up a form for me using mysql_query. Since he wrote this, I have added an extra column into the database, which I want to pull through into the form.
However I can't seem to get this extra column to appear (labelled Currency). The reason I need it is the query below will pulls back a value and the £ symbol. Because I want to display not only £, but also € prices, I need this extra column to pull through (obviously I will have to remove the £ from the echo below too).
I've tried adding the extra column (Currency) to the code, e.g. "SELECT Room, Currency, Price FROM Spa_Upgrades
but this hasn't worked.
The code is:
<?php
if (isset($id))
{
$query2 = mysql_query("SELECT Room, Price FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
$rows = mysql_num_rows($query2);
if($rows==0) {echo "disabled='disabled'/>";}
else
{
echo "/>";
echo "<option value='Default'>Please Select</option>";
for($i=0; $i<$rows; $i++)
{
$result2 = mysql_fetch_array($query2);
echo "<option value='".$result2[0]." £".$result2[1]."pp'>$result2[0] £$result2[1]pp</option>";
}
}
}
Hugely grateful if someone can solve this!
Thanks,
Motley
Alter the query as follows:
SELECT Room, Price, Currency FROM Spa_Upgrades ...
Alter the line beginning echo inside the for loop: replace £ with $result2[2] wherever it appears. (Or if the Currency column doesn't contain the HTML entity for the currency symbol, then replace £ with appropriate code to obtain the symbol from the Currency column entry.)
You also need to add the column to the output... I would also switch to an associative array otherwise if you add a column and its not at the end you have to change all the indexes.
if (isset($id))
{
$query2 = mysql_query("SELECT Room, Price, Currency FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
$rows = mysql_num_rows($query2);
if($rows==0) {echo "disabled='disabled'/>";}
else
{
echo "/>";
echo "<option value='Default'>Please Select</option>";
for($i=0; $i<$rows; $i++)
{
$result2 = mysql_fetch_assoc($query2);
$value = $result2['Room'] . ' ' . $result2['Currency'].$result2['Price'].'pp';
echo sprintf('<option value="%s">%s</option>', $value, $value);
}
}
}
Use mysql_fetch_assoc() instead of mysql_fetch_array()
A good practice as well is to separate data retrieving from display logic
Try this:
<?php
$results = array();
if (isset($id))
{
$resource = mysql_query("SELECT room, currency, price FROM Spa_Upgrades
WHERE Spa_Upgrades.Spa_Id = '".intval($id)."' AND Spa_Upgrades.Id = '".intval($pack)."'
order by Spa_Upgrades.Order");
while($row = mysql_fetch_assoc($resource))
$results[] = $row;
}
?>
<select name="..." <?php echo (count($results) > 0 ? '' : 'disabled') ?>>
<option value="Default">Please Select</option>
<?php
foreach($results as $result)
{
$value = $result['room'].' '.$result['currency'].$result['price'].'pp';
echo '<option value="'.htmlspecialchars($value).'">'.htmlspecialchars($value).'</option>'."\n";
}
?>
</select>
Related
I have been searching for days and trying everything, but i can't seem to get this working.
Question :
I have a user profile where a user can select some hobbies from a dropdown menu. This user can also edit his profile. On this edit page I would like to display a dropdown where the previously selected hobbies are selected and the rest of the available hobbies option are displayed for selection.
This is the basic code I have so far ( minus all the code that didnt work ). I hope someone can help he out.
$existing_hobby_values = array("Football", "Tennis", "Volleyball");
$sql = "select hobby from hobbies ORDER BY id ASC";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<select multiple>";
while($row = mysqli_fetch_assoc($result)) {
$interesse = $row['hobby'];
//if{$interesse = in_array($existing_hobby_values) echo "selected" inside option }
echo "<option value='$interesse'>$interesse</option>";
}
echo "</select>";
}
By the way...I know I should start using PDO instead of Mysqli, but because this project has a deadline I have to finish this before starting with learning PDO.
You can try something like
while ($row = mysqli_fetch_assoc($result)) {
$interesse = $row['hobby'];
echo '<option ';
if (in_array($interesse, $existing_hobby_values)) {
echo 'selected ';
}
echo "value='$interesse'>$interesse</option>";
}
And you should definitely do something about your tabulation.
change
echo "<option value='$interesse'>$interesse</option>";
to
echo "<option value='$interesse' ".( in_array($intresse,$existing_hobby_values) ? "SELECTED ": "" ) .">$interesse</option>";
though to be fair, #rept1d's answer should work just fine as well.
I am presenting a list of options to the user based on a database query. The results are being fetched correctly however they are currently unordered and I would like to display them in alphabetical order in the option list. I have tried several solutions so far with "ORDER BY" on the query level however with no success. Is there a simple way to achieve this? (The list contains 1000+ results) The code is as follows:
<?php
$user_id = $_SESSION['user_id'];
$sql = "SELECT * FROM add_bulding WHERE description<>''";
$query = $this->db->query($sql);
$building_title = $query->result_array();
?>
<div>
<select>
<option value="">Select Building</option>
<?php foreach ($building_title as $value) { ?>
<option value = "<?php echo $value['id']; ?>">
<?php echo $value['title']; ?>
</option>
<?php
}
?>
</select>
</div>
Try changing your query to:
$sql = "SELECT * FROM add_bulding WHERE description<>'' ORDER BY title DESC";
Sounds like you want a query like:
SELECT id, title FROM add_bulding WHERE description<> ORDER BY title desc
Alternately, you can sort it in php:
<?php
$tmp = array();
foreach ($building_title as $value){
$tmp[$value['id']] = $value['title'];
}
ksort($tmp);
foreach ($tmp as $k => $v) {
print "<option value=\"" + $k + "\">" + $v + "</option>\n";
}
?>
However, in either case, be sure to sanitize your values so you don't end up with XSS problems.
Try this query :
$sql = "SELECT * FROM add_bulding WHERE description<>'' ORDER BY title";
If you want to get title data with alphabetical order then you can write 'ASC' or do not need to write any after ORDER BY title.
But you want to get title data with descending order then you need to write 'DESC' after ORDER BY title
I hope you will get solution.
I have a dynamic drop down menu on a PHP form which is working fine in that it retrieves/inputs the right id and will not process the form if no option is collected.
However, I am not sure how to make it sticky. I can do it on a static drop down with no problems but obviously I am missing something, can anyone help?
Below is the drop down menu:
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0] \" <?php if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == '$row[0]') {echo 'selected=\"selected\"';} ?> >$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';
Below is the validation code
if (isset($_POST['dealership_id'])) {
$dealer_id = (int) $_POST['dealership_id'];
} else {
$dealer_id = 0;
}
if ($dealer_id > 0) {
$query = "SELECT dealership_id FROM dealership WHERE dealership_id=$dealer_id";
$result = mysql_query ($query); }
else {
echo '<p><font color="red">Please select your Dealership</font></p>';
}
BTW, row 0 is the primary key, row 3 is the name.
I don't think there should be single quotes around $row[0] in the following:
$_POST['dealership_id'] == '$row[0]'
By using single quotes you are literally comparing the string $row[0] instead of the variable value
Here's your code with some changes that are at least valid syntax; I didn't test to see if it works, but it should. It would be helpful for you to research string concatenation in php, some useful info here: http://www.php.net/manual/en/language.operators.string.php
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\"";
if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == $row[0]){
echo ' selected=\"selected\"';
}
echo ">$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';
I have a HTML etc.. tags now what I want to achieve is upon a selection of ie. i want to load the related info from database to in a new tag with as many tags.
I am using PHP to do achieve this now at this point if for example i choose option1 then the query behind it retrieves relevant information and stores it in a array, and if I select option2 exactly the same is done.
The next step I made is to create a loop to display the results from array() but I am struggling to come up with the right solution to echo retrieved data into etc. As its not my strongest side.
Hope you understand what I am trying to achieve the below code will clear thing out.
HTML:
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP:
$form = Array();
if(isset($_POST['workshop'])){
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
while($result2 = mysqli_fetch_assoc($query2)){
//The problem I am having is here :/
echo "<select id='Forex' name='Forex' style='display: none'>";
echo "<option value='oko'>.$result[1].</option>";
echo "</select>";
print_r($result2);echo '</br>';
}
}else{
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%'";
$query = mysqli_query($link, $sql);
while($result = mysqli_fetch_assoc($query)){
print_r($result);echo '</br>';
}
}
}
Try this code:
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value='oko'>{$result['course']}</option>";
}
echo "</select>";
echo '</br>';
From the top in your php:
// not seeing uses of the $form I removed it from my answer
if(isset($_POST['workshop'])){
$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?
//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR course LIKE '&$lowerWorkhop%'";
$query = mysqli_query($link, $sql); // here no need to have two results
// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
$select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys.
// to retrieve them, you have to use quotes around the key, hence the change
}
$select.= '</select>';
}
echo $vSelect;
this will output a select containing one option for each row returned by either of the queries. by the way this particular exemple won't echo anything on screen (since your select display's set to none). but see the source code to retrieve the exemple.
I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>