dssqq82402 2014-05-02 17:19
浏览 42

在一个选项中从数据库发布多个值

I am looking to populate a drop down field with a list of names from a database, and when an option is selected from that drop down, it will post all its values in the row, belonging to that chosen option.

This is probably hard to describe/understand, so to help illustrate, this is my table row:

db structure

I then proceed to populate the dropdown, and associate its value to whatever the selected option is posted

//////db_conx is db connection //////main_meal is table name
      <form action="#" method="post">
                        <?php  
                        $dropdown = $db_conx->query("SELECT * FROM main_meal") or die ("somethings broken");                       
                        while($array[] = $dropdown->fetch_object());
                        //echo '<option value ="'.$record['Mname'].'">'.$record['Mname'].'"</option>';                        
                        array_pop($array);                       
                        ?>

                        <select name="changeCal">
                            <?php    foreach($array as $option) :?>
                            <!--//get chosen value in drop down, and get its calories-->
                            <option value="<?php echo $option->calories;?>"><?php echo $option->Mname; ?></option>
                            <?php endforeach; ?>
                        </select>

This works great for one value, such as calories, in the above code, but I need more values.

For example, if choose Healthy egg and chips, the value will post 218, as the loop only associates calories and names at the moment.

I attempted various things, like this post:How to get multiple values from a single <select> variable in HTML/PHP?

But the foreach errors.

How can I something similar to what I have done, but store multiple values from one chosen option?

Thank you

  • 写回答

1条回答 默认 最新

  • douli1306 2014-05-21 17:41
    关注

    Well, I think I understood your problem, but I think that the way you want to use is not usable, maybe I recommend that you put the id in the value of the option in the < select> and then with php you can get all the data from the data base. For me that is the best way, but if you want do it like the example that you show, you can make an string, for example:

    <option value="id:5_calories:258_protein:11g">Healthy eggs & Chips</option>
    

    with your php should look like:

    echo '<option value="id:'.$option->id.'_calories:'.$option->calories.'_protein:'.$option->protein.'">'.$option->Mname.'</option>';
    

    you can make bigger the string with others values that you want to put. In the backend when you send the select you can catch the data with a:

    $myArray = explode("_", $_POST["changeCal"]);
    

    Then you will get an array with values like:

    $myArray[0]; // id:5
    $myArray[1]; // calories:258
    $myArray[2]; // protein:11g
    

    Then if you need for example the calories you can make an explode like:

       $calories = explode(":",$myArray[1]);
    

    And you will have:

       $calories[0]; //calories
       $calories[1]; //258   <= Here are the calories.
    

    Maybe if you want to do it of that way, this can be the easiest way, but I recommend send the ID.

    Let me know if you need more help. Regards, Have a nice day.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助