duanhao9176 2016-06-14 16:18
浏览 43
已采纳

使用PHP从SQL获取行,并为每行添加li元素到ul

I am pretty new to this so just trying to figure something out. My overall goal is to create a drop down menu in my navbar that shows themes and when you click on an element it changes the color theme of the page. In my database I have 2 tables. One that lists the theme and an Id for the theme and another table that has the hex codes related to that theme.

Ordered as such:

themes table:

ID | Theme

colors table: (Theme ID and ID in themes table are the same)

ID | Theme ID | hex Code

I am trying to break this down into steps:

  1. retrieve theme names from SQL and make a new li in my ul for each row in the table
  2. When the li for a theme is clicked, retrieve hex codes for the theme colors from the colors table as php variables.
  3. (The stylesheet is a .php document with php variables used to assign colors to various css tags on the page). Use php variables from the previous step to change the variable values in this file ultimately changing the color scheme (theme) of the page.

If there is a smarter way to do this I would love to know about it.The trouble I am having right now is. I am new to PHP and I don't know how to do step 1. I think I can manage steps 2-3 but I can't find anything on how to add an li to a drop down list for each row retrieved.

         <li>themes
            <ul>
              <!-- add theme names here -->
            </ul>         
         </li>
  • 写回答

1条回答 默认 最新

  • douhei8633 2016-06-14 16:31
    关注

    do your query

    $toReturn="";
    $query="select * from themes,colors where Theme_ID=ID";
    $result= mysql_query($query); 
    

    in this way you get all your information. Now iterate on them

    while ($r=mysql_fetch_assoc($result)){
    $toReturn=$toReturn. "<li>themes<ul>".$r[name of field to extract]. "</ul></li>";}
    echo $toReturn;
    

    In this way you get what you want

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛