dongxian5735 2013-05-27 09:18
浏览 41

回显标签中的不同字段

I'm trying to make a tab for my page, where each tab shows a different field of a mysql table row. I'd like to switch contents in the tab. Every row has a different id, and I would like to make a tabbed design. The data should be chosen by the id of the table. The tab works fine if i use just a simple text in the echo part, but I can't fetch the data from my database. I tried this code, but it doesn't work.

<?php

if (isset($data[0]) && is_numeric($data[0]) ) {
    $content = mysql_fetch_assoc ( mysql_query( "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name where id = $data[0];") );
} else {
    $content = mysql_fetch_assoc ( mysql_query( "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name order by id asc limit 1;") );   
}

switch($_GET['tabNum']) {
    case 1: echo strip_tags($content['tab_content_one']); break;
    case 2: echo strip_tags($content['tab_content_two']); break;
    case 3: echo strip_tags($content['tab_content_three']); break;
    case 4: echo strip_tags($content['tab_content_four']); break;
    case 5: echo strip_tags($content['tab_content_five']); break;
}

?>

I don't know what's wrong with my code. Do you have any idea?

  • 写回答

1条回答 默认 最新

  • doulang6013 2013-05-27 09:38
    关注

    You've got to enclose the mysql_fetch_assoc into a while loop! For your code it will be something like this:

    if (isset($data[0]) && is_numeric($data[0]) ) {
    $sql = "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name where id = $data[0];";
    } else {
    $sql = "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name order by id asc limit 1;"; 
    }
    $result = mysql_query($sql);
    
    while($content = mysql_fetch_assoc($result)){
        switch($_GET['tabNum']) {
        case 1: echo strip_tags($content['tab_content_one']); break;
        case 2: echo strip_tags($content['tab_content_two']); break;
        case 3: echo strip_tags($content['tab_content_three']); break;
        case 4: echo strip_tags($content['tab_content_four']); break;
        case 5: echo strip_tags($content['tab_content_five']); break;
    }
    }
    

    Anyway it's suggested to you as mysql_fetch_assoc it's going to be deprecated starting from php 5.5.0 to use the mysql_PDO extension instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办