farahZ Posted May 3, 2013 Share Posted May 3, 2013 hey teami'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> ?> Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/ Share on other sites More sharing options...
computermax2328 Posted May 3, 2013 Share Posted May 3, 2013 Are you getting any errors?? Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428047 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 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";*/ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428053 Share on other sites More sharing options...
PaulRyan Posted May 3, 2013 Share Posted May 3, 2013 (edited) 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 May 3, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428061 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) no errors .. but getting an empty dropdown listits not reading data from the tablei double checked the table name, attributes .. no error in that Edited May 3, 2013 by farahZ Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428069 Share on other sites More sharing options...
davidannis Posted May 3, 2013 Share Posted May 3, 2013 (edited) You are mixing mysql and mysqli. You can not do that. Try $query = "SELECT Food,Calories FROM food"; $result = mysqli_query($query,$conn); while($row = mysqli_fetch_array($result)) Edited May 3, 2013 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428071 Share on other sites More sharing options...
davidannis Posted May 3, 2013 Share Posted May 3, 2013 I would also suggest using mysqli_fetch_assoc instead of mysqli_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428072 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 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>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428075 Share on other sites More sharing options...
computermax2328 Posted May 3, 2013 Share Posted May 3, 2013 mysqli_query() is $connection, $query and not the other way around. See reference link here Should be like this.... $result = mysqli_query($conn,$query); Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428077 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) 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 May 3, 2013 by farahZ Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428080 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 Lol I found a way by just adding the calories in the food column Example: cucumber-30 But keeping the calories column unchanged, which isbthe value I need for later usage Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428083 Share on other sites More sharing options...
Jessica Posted May 3, 2013 Share Posted May 3, 2013 Just concatenate the two values either in PHP or mysql. Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428099 Share on other sites More sharing options...
davidannis Posted May 4, 2013 Share Posted May 4, 2013 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>"); Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428112 Share on other sites More sharing options...
farahZ Posted May 4, 2013 Author Share Posted May 4, 2013 there's a parse error with the -Parse error: syntax error, unexpected '-', expecting '}' in D:\xampp\htdocs\inshapewebsite\Untitled-1.php on line 24 i tried . - + ,nothing works Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428140 Share on other sites More sharing options...
davidannis Posted May 4, 2013 Share Posted May 4, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428180 Share on other sites More sharing options...
Jessica Posted May 4, 2013 Share Posted May 4, 2013 You can use curly braces Echo "abc{$var['key']}123"; Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428183 Share on other sites More sharing options...
Jessica Posted May 4, 2013 Share Posted May 4, 2013 Oh I see you did that - but you need them around each variable. So where you have {$row['Food'] - $row['calories']} It has to be {$row['Food']} - {$row['calories']} Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428186 Share on other sites More sharing options...
farahZ Posted May 5, 2013 Author Share Posted May 5, 2013 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 arrayif (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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/277605-drop-down-list-from-mysql/#findComment-1428320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.