Jump to content

Recommended Posts

hey team
i'm trying to extract data from a table in xampp server, mysql

database name= inshapewebsite

table= food

and use this data to create a dropdown list that has 2 columns: Food, Calories

is that possible??
 

i have this code so far

 

 

 
<?
// Create connection
$con=mysqli_connect("localhost","root","","inshapewebsite");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}  
else 
{ 
 
<select name="foodType" onchange="autoSubmit();">
        <option VALUE="null"></option>
 $result = "SELECT  Food,Calories FROM food";
        $food = mysql_query($sql,$conn);
 
        while($row = mysql_fetch_array($food))
        {
            echo ("<option VALUE=\"$row[Food]\" " . ($food == $row["Food"] ? " selected" : "") . ">$row[Calories]</option>");
        }
 
}
</select>
?>
 
Link to comment
https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/
Share on other sites

yes!!
$result = "SELECT Food,Calories FROM food"; $food = mysql_query($sql,$conn); while($row = mysql_fetch_array($food)) { echo (""); } /*echo "Select food: \n" echo "";  while($row=mysqli_fetch_assoc($result)){ $Food=$row['Food']; $Calories=$row['Calories']; print "$Food\n"; }  print " \n"; print "

 

\n";*/ } ?>

You have HTML inside of your PHP tags, you need to echo or print that data, or close the tags and open them when needed.

 

Something like this:

<?PHP
  // Create connection
  $con=mysqli_connect("localhost","root","","inshapewebsite");
  // Check connection
  if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit;
  }
?>
 
  <select name="foodType" onchange="autoSubmit();">
    <option VALUE=""></option>
<?PHP
  $result = "SELECT  Food,Calories FROM food";
  $food = mysql_query($sql,$conn);
 
  while($row = mysql_fetch_array($food)) {
    echo ("<option VALUE=\"{$row['Food']}\" " . ($food == $row['Food'] ? " selected" : "") . ">{$row['Calories']}</option>");
  }
?>
 
  </select>
Edited by PaulRyan

still an empty list..

 

 <select name="foodType" onchange="autoSubmit();">
    <option VALUE=""></option>
<?PHP
  $query = "SELECT  Food,Calories FROM food";
  $result = mysqli_query($query,$conn);
while($row = mysqli_fetch_assoc($result)) {
    echo ("<option VALUE=\"{$row['Food']}\" " . ($food == $row['Food'] ? " selected" : "") . ">{$row['Calories']}</option>");
  }
?>

Problem solved :)

i am getting now the list :))

one last question, is it possible that in the drop down list, i get to show both column values?? i.e: Food, Calories ??

option: Cucumber, 30 ?

that's the correct code :)

 

<?PHP
  $query = "SELECT  Food,Calories FROM food";
  $result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)) {
    echo ("<option VALUE=\"{$row['Calories']}\" " . ($food == $row['Food'] ? " selected" : "") . ">{$row['Food']}</option>");
  }
?>
Edited by farahZ

If you put them in the same column it makes it hard if you decide to do something like total the calories later. Try this instead:

echo ("<option VALUE=\"{$row['Calories']}\" " . ($food == $row['Food'] ? " selected" : "") . ">{$row['Food'] - $row['calories']}</option>");

The following code:

<?php
$row['Calories']=90;
$food = $row['Food']="hot Dog";
echo ("<option VALUE=\"".$row['Calories']."\" " . ($food == $row['Food'] ? " selected" : "") . ">".$row['Food'].' - '.$row['Calories']."</option>");
?>

produces:

<option VALUE="90"  selected>hot Dog - 90</option>

I got rid of the curly braces and moved all the row array variables outside of the quotes. Perhaps there is a better way to do something like

 echo "ABC".$letters['next3']."DEF"; 

than to take the $letters variable out of the quotes but that's how I do it. Can anyone suggest a better solution?

thanks the two values are now being showed as one option in the drop down list .. 
the issue is that food chosen by the user are being saved in an array, and shown simultaneously.
but of course, only the calories value is shown in printing .. can it also be both..?



code for the list
<select name="foodType" onChange="autoSubmit();">
    <option VALUE=""></option>
<?PHP
  $query = "SELECT  Food,Calories FROM food";
  $result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)) {
    echo ("<option VALUE=\"".$row['Calories']."\" " . ($food == $row['Food'] ? " selected" : "") . ">".$row['Food'].' - '.$row['Calories']."</option>");
  }
?>
  </select>

// for saving the items chosen in an array
if (in_array($_POST['foodType'], $_SESSION['foodTypes']) === false) 
    {
      $_SESSION['foodTypes'][] = $_POST['foodType'];
    }
 


//code for printing the array simultaneously (after each add click)

 

 foreach ($_SESSION['foodTypes'] as $food)
{
  echo $food . '<br>';
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.