php输出php mysql 输出table rowspan的相关问题

百度知道,所提出来的问题 怎样删除?_百度知道
百度知道,所提出来的问题 怎样删除?
我有更好的答案
取消了问答删除功能,百度知道APP客户端从7.9版开始由于策略原因。如果需要处理,请到PC电脑浏览器网页端的百度知道上进行删除,因此无法在APP客户端上进行删除了,仅限尚未解决的问题
采纳率:77%
来自团队:
你先找到我
点 然后会有我的提问的
进去就可以删问题了
为您推荐:
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)php - Dynamic rowspan while fetching records from database - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
This is the query and html structure which has given the above output.
$sql = "select * from emp ";
$result= mysql_query($sql);
while($row=mysql_fetch_array($result))
&td rowspan=""
&&?php echo $row['ename']; ?&&/td&
&td&&?php echo $row['esal']?&&/td&
How can i get following output:
6,127113755
sorry for my poor english:
Here I had answered this question . Again let me try to answer this question. First lest us work on mysql query.
MySql Work:
In the mysql query you have not queried for order by. Because in real life, you can not expect that after all the records of tom, bills record will be there. For example take the following insertion.
INSERT INTO test_work(ename, sal)
VALUES("tom",
("bill", 450),
("bill", 100),
("bill", 250),
("bill", 400),
("James", 50);
SELECT * FROM test_
+-------+------+
| ename | sal
+-------+------+
+-------+------+
So your mysql query should be order by ename. Here also each person's sal should be ordred . So Our query:
SELECT * FROM emp ORDER BY ename,
The whole task we can divide into 3 parts.
Mysql Data fetch and storing in array.
Calculating rowspan
MySql Datafetching:
During data fetching from mysql server always we should try to use mysql_fetch_assoc function instead of mysql_fetch_array . Because mysql_fetch_assoc will return only ename and sal. But mysql_fetch_array will return array with indexes ename, sal, 0, 1.
# connect to mysql server
# and select the database, on which
# we will work.
$conn = mysql_connect('', 'root', '');
= mysql_select_db('test');
# Query the data from database.
= 'SELECT * FROM test_work ORDER BY ename, sal';
$result = mysql_query($query);
# Intialize the array, which will
# store the fetched data.
$sal = array();
$emp = array();
# Loop over all the fetched data, and save the
# data in array.
while($row = mysql_fetch_assoc($result)) {
array_push($emp, $row['ename']);
array_push($sal, $row['sal']);
Calculating Row Span:
# Intialize the array, which will store the
# rowspan for the user.
$arr = array();
# loop over all the sal array
for ($i = 0; $i & sizeof($sal); $i++) {
$empName = $emp[$i];
# If there is no array for the employee
# then create a elemnt.
if (!isset($arr[$empName])) {
$arr[$empName] = array();
$arr[$empName]['rowspan'] = 0;
$arr[$empName]['printed'] = "no";
# Increment the row span value.
$arr[$empName]['rowspan'] += 1;
when you will print_r the arr array the output will be:
[bill] =& Array
[rowspan] =& 4
[printed] =& no
[James] =& Array
[rowspan] =& 1
[printed] =& no
[tom] =& Array
[rowspan] =& 2
[printed] =& no
Printing with rowspan:
echo "&table cellspacing='0' cellpadding='0'&
&th&Ename&/th&
&th&Sal&/th&
for($i=0; $i & sizeof($sal); $i++) {
$empName = $emp[$i];
echo "&tr&";
# If this row is not printed then print.
# and make the printed value to "yes", so that
# next time it will not printed.
if ($arr[$empName]['printed'] == 'no') {
echo "&td rowspan='".$arr[$empName]['rowspan']."'&".$empName."&/td&";
$arr[$empName]['printed'] = 'yes';
echo "&td&".$sal[$i]."&/td&";
echo "&/tr&";
echo "&/table&";
Code Optimization:
Now we can combine the rowspan calculation and mysql data fetching. Because during saving the fetched data in array we can calculate the rowspan. So our final code:
&!DOCTYPE html&
table tr td, table tr th{
border: black 1
padding: 5
# connect to mysql server
# and select the database, on which
# we will work.
$conn = mysql_connect('', 'root', '');
= mysql_select_db('test');
# Query the data from database.
= 'SELECT * FROM test_work ORDER BY ename, sal';
$result = mysql_query($query);
# $arr is array which will be help ful during
# printing
$arr = array();
# Intialize the array, which will
# store the fetched data.
$sal = array();
$emp = array();
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
data saving and rowspan calculation
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# Loop over all the fetched data, and save the
while($row = mysql_fetch_assoc($result)) {
array_push($emp, $row['ename']);
array_push($sal, $row['sal']);
if (!isset($arr[$row['ename']])) {
$arr[$row['ename']]['rowspan'] = 0;
$arr[$row['ename']]['printed'] = 'no';
$arr[$row['ename']]['rowspan'] += 1;
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DATA PRINTING
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
echo "&table cellspacing='0' cellpadding='0'&
&th&Ename&/th&
&th&Sal&/th&
for($i=0; $i & sizeof($sal); $i++) {
$empName = $emp[$i];
echo "&tr&";
# If this row is not printed then print.
# and make the printed value to "yes", so that
# next time it will not printed.
if ($arr[$empName]['printed'] == 'no') {
echo "&td rowspan='".$arr[$empName]['rowspan']."'&".$empName."&/td&";
$arr[$empName]['printed'] = 'yes';
echo "&td&".$sal[$i]."&/td&";
echo "&/tr&";
echo "&/table&";
Something like this?
While data
&td rowspan="3"&&p&numbers&/p&&/td&
&td&&p&1&/p&&/td&
&td&&p&2&/p&&/td&
Please post something of your code
2,24322030
You can check condition like this with the last row and the current row.
$sql = "select * from emp";
$result= mysql_query($sql);
echo "&table&";
while($row=mysql_fetch_array($result))
$now=$row[0];
if($last!=$now) {
echo "&tr&&td&$row['ename']&/td&&td&$row['esal']&/td&&/tr&";
echo "&tr&&td&&&/td&&td&$row['esal']&/td&&/tr&";
$last = $row[0];
echo "&/table&";
I hope this will help you.
1,34211117
It should be like this
$sql = "SELECT * FROM emp ";
$result= mysql_query($sql);
while($row=mysql_fetch_array($result)):
$ename = $row['ename'];
// count the esal in each ename
$sql2 = "SELECT * FROM emp WHERE ename=$ename";
$result2 = mysql_query($sql2);
$count_result2 = mysql_num_rows($result2);
&td rowspan="&?php echo $count_result2; ?&"&&?php echo $row['ename']; ?&&/td&
// loop each esal
while($row2 = mysql_fetch_array($result2)):
&td&&?php echo $row['esal']; ?&&/td&
// endwhile for each esal looping
// endwhile for the main looping
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Upcoming Events
Stack Overflow works best with JavaScript enabledphp查询的问题_百度知道
php查询的问题
我做了个php查询界面,
&head&&title&查询结果&/title&&/head&
include(&connMysql.php&);
$quer=$_REQUEST[&quer&];
&table table border=&1& width=&20%& align=&center&&
if ($quer[&Name&]!='')
$tel_quer=&SELEC...
我有更好的答案
$row[&photo&&&&%&;/tr&&?phpinclude(&connMysql.php&/a&
echo &&td colspan=2 align=center &地址;&quot.php&&返回主页面&;
echo &&tr&&;td&
echo &&tr&&.&%' $nums=mysql_num_rows($result); //html&&td&; $result = mysql_query($sql);.$row[&table&&p align=&center&&&a href=&phone&].&&/或者亦可以用count //
echo &&tr&&;echo &&table table border=\&1\& width=\&20%\& align=\&center\tr&quer&quot.$quer:&/head&&body&&;
echo &&//tr&&;
echo &&td align=center &性别;%';
} } else {
echo &quot.&&/p&&lt!&lt&html&&head&&title&;
echo &Sex&].&&/
echo &&td align=center&;
echo &&){ //tr&&;/body&&/抱歉! 资料库没有此项资料;&; }}?&&/; if($nums&.$row[&查询结果&%&;0) {
while($row=mysql_fetch_array($result))
echo &quot.$row[&Name&].&&/&&);/td&&;&];&&td rowspan=3 align=center&&img src=&;&/td&&;/p&&SELECT * FROM students WHERE Name LIKE '&$sql=&SELECT count(-1) as num * FROM students WHERE Name LIKE 'td&&&&p align=center&$quer=$_REQUEST[&quot.&&.$row[&Arrd&&姓名:&;/title&tr&/查询数据库学生信息有多少条 $sql=&quot:&&lt:&];
echo &&td align=center &电话.$quer.&;if ($quer[&Name&]!='&#39
采纳率:25%
echo &&tr&&;td&;
echo &&td align=center&body&&姓名:&; width=\
echo &&td colspan=2 align=center &地址;&
echo &&tr&&;&td rowspan=3 align=center&&img src=&;];phone&].&&/;
echo && align=\
$result = mysql_query($sql):&td&.$row[&&quot!&Sex&].&&/;
echo &/tr&&;table&&p align=&center&&&a href=&];&lt.php&&返回主页面&center\&&&;head&&Name&;
echo &tr&gt:&&lt!=&#39:&&.$row[&&html&/td&&;table table border=\tr&
echo &&tr&&;&
echo &&td align=center &性别;&&/p&; }}?&&/&&lt.&抱歉! 资料库没有此项资料;html&/td&&;&title&查询结果&/Arrd&if ($quer[&
echo &&/'){ $sql=&SELECT *
FROM students
WHERE Name LIKE '%&&&p align=center&.$quer.&%'&:&
echo &&td align=center &电话;1\td&/tr&&;&/a&;
} } else {
echo &/p&/body&&/&&quot.$row[&photo&title&&/&connM&head&gt.&.$row[&&/tr&&;?phpinclude(& $nums=mysql_num_rows($result); if($nums&gt.php&);$quer=$_REQUEST[&quer&];echo &&quot.$row[&Name&].&&/&0) {
while($row=mysql_fetch_array($result))
echo &]给你修改了一下,注意变量的命名不要乱
if (exists($quer[&Name&])){
echo &抱歉&;}
为您推荐:
其他类似问题
您可能关注的内容
php的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。php输出数据表时 字段内容相同时另一列合并_百度知道
php输出数据表时 字段内容相同时另一列合并
教师姓名 授课课程 授课班级 额定津贴 总津贴
我有更好的答案
.<file fsid="241" link="/share/link稍等下.hiphotos.baidu.com/zhidao/pic/item/50da81cb39dbb6fddab.baidu,我给你个Demo,你看看是否和你的需求一致?shareid=&uk=" name="test.com/zhidao/wh%3D450%2C600/sign=ca7bfe14eef/50da81cb39dbb6fddab.jpg" />Demo已经上传到附件了.com/zhidao/wh%3D600%2C800/sign=ab899de1ad/50da81cb39dbb6fddab.jpg" esrc="http://d.hiphotos.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http://d.hiphotos..,下图是效果图:<a href="http://d.baidu
采纳率:76%
?php/while($row = myslq_fetch_assoc($a)){
//假设$name为老师的名字,不过这里做用老师的$nameid吧$a = mysql_query(&select * from $table&);$b = array()
这个貌似合并不了啊
我不是合并,为是把有属性一样的,也就是教师名称一样的,塞到同一个数组里面而已,这完全是可以的
本回答被网友采纳
首先计算出每位老师有多少个课程:select 表a.教师姓名, count(*) as count from 表a group by 表a.教师姓名,然后根据count的数量,php动态生成td标签的rowspan属性。
授课课程的时候只能输出一个 不知道什么情况
求您的直接代码
比如王的count=2那么下面的合并单元格输出代码应该为:&td rowspan=&?php echo $row[&#39;count&#39;]; ?& &&?php echo $row[&#39;教师姓名&#39;]; ?&&&#47;td&后面的内容按照正常&select * from table where 教师姓名= &.$row[教师姓名]。再进行一次query即可得到。
输出的时候,if判断。
可以具体一点吗
输出的数据,用if 判断 额定津贴 总津贴 是否相等,如果相等, 隐藏 td
额可能你理解错我的意思了 第一个表是查询出来的结果 但是我想输出时候是第二个表的样式 也就是说输出时候把教师姓名相同 的 合并起来
有2种方式,第一,输出的数据,判断,前面如果有重复的,就隐形第二个和后面的。第2种方式,是 jquery 筛选 行,百度搜一下
JQuery操作表格(隔行着色,高亮显示,筛选数据)
其他1条回答
为您推荐:
其他类似问题
php的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 php输出mysql查询结果 的文章

 

随机推荐