dougou8639 2013-05-10 14:13
浏览 21
已采纳

子级不显示正确父级的数据

I'm relatively new to php, and am stuck on one issue. My index.php file displays a list of states, and displays those states correctly. Each of those states has an p_id field that correspond to the alphabetized listing of states.

The database I'm using has a table that contains the following fields in the cat table: p_id (parent id), state (name of state), city, and id (child's id, which resets for every new state). This is the query I use for populating the states:

select distinct state, p_id from cat order by state asc

List_cities.php is the next page that displays after clicking on a state. Alabama is the first state alphabetically, so would be parent (p_id)=1. When I click on Alabama (or any other state), no cities are displayed. They are all listed in the cat table.

This is the query I use to display the cities:

select p_id, id, city, state from cat where p_id="'.$p_id.'" order by p_id, id

Any help would be greatly appreciated! Thanks!

  • 写回答

1条回答 默认 最新

  • dongwanqiang_2017 2013-05-10 15:10
    关注

    Imagine you have a STATE table like this:

    -------------
    id  |  state 
    -------------
    1   |  California
    2   |  New York
    3   |  Arizona
    

    and you the CITY table like:

    ------------------------------
    id  |  state_id  | name
    ------------------------------
    1   |     1      | Los Angeles
    2   |     1      | San Diego
    3   |     1      | San Jose
    

    The correct query to retrieve the states is:

    SELECT id, state FROM states ORDER BY state ASC;
    

    and the correct query to retrieve the cities, based on a state is:

    SELECT id, name, state_id FROM cities WHERE state_id = ?;
    

    Then obviously, you would use the PDO class to connect the database since it is more secure than whatever you can write. Check the pdo manual for more info. That '?' is actually the parameter which you bind with PDO so don't let it confuse you.

    From what I've understood you have created one table where you specify both the city and the state name. I would definitely seperate them, and create two tables as I did above.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。