/    Sign up×
Community /Pin to ProfileBookmark

Display data in table

Hi,
I have a table called employee which has got two fields name and age. I would like to display in table name in column wise and age in row wise. Can anyone pls help me?

The format should be like this

Name/age age1 age2 age3 age4 age5 age6 age7
name1
name2
name3
name4
name5
name6
name7

Here is my code

[code=php]<?php
include “config.php”;
$Sql = “select * from employee”;
$Res = mysql_query($Sql) or die(mysql_error());
?>
<table border=”1″>
<tr>
<td>Name/age</td>
<?php
while($row=mysql_fetch_array($Res))
{
?>
<td><?php echo $row[‘name’];?></td>
<td><?php echo $row[‘age’];?></td>
<?php
}
?>
</tr>
</table>[/code]

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@our5Apr 30.2009 — If I am understanding properly what you are looking to do, you would need two loops, one within the other. So it would go something like this (didn't test it, but you should get the idea)....

while($row=mysql_fetch_array($names))

{


?>

<tr>

<td><?php echo $row['name'];?></td>

<?php

while($row=mysql_fetch_array($ages))

{

?>

<td><?php echo $row['age'];?></td>

<?php

}

echo '</tr>';

}?>
Copy linkTweet thisAlerts:
@dai_hopApr 30.2009 — I'd go with something like this:

[code=php]<?php
include "config.php";
echo '<table>';
$Sql = "select * from employee";
$Res = mysql_query($Sql) or die(mysql_error());
while ($row = mysql_fetch_array($Res))
{
echo '<tr></td>' . $row['name'] . '<td>';
$Sql = "SELECT * FROM employee WHERE name = '" . $row['name'] . "'";
$Res2 = mysql_query($Sql) or die(mysql_error());
while ($row2 = mysql_fetch_array($Res2))
{
echo '<td>' . $row2['age'] . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>[/code]
Copy linkTweet thisAlerts:
@JTweedieApr 30.2009 — I don't see why you would want to display them in that format? An employee cannot have multiple ages?
Copy linkTweet thisAlerts:
@kavionlyauthorMay 04.2009 — Its not coming properly. age should come in a single row.
Copy linkTweet thisAlerts:
@kavionlyauthorMay 05.2009 — Based on name and age i want to display type. I am getting in column wise i want in row wise. Can anyone help me pls

It looks something like this


Name/age age1 age2 age3 age4 age5 age6 age7

name1 A B C D E F G

name2 K B C D E F G

name3 A L M P O S R

name4 A B C D E F G

name5 A B C D E F G

name6 A B C D E F G

name7 A B C D E F G


Here is the code

[code=php]<?php
include('config.php');
$Sql = "select * from tbl_employee ";

$result = mysql_query($Sql) or die(mysql_error());

$Sql1 = "select * from tbl_employee ";

$Res1 = mysql_query($Sql1) or die(mysql_error());

$C = "select count(age) from tbl_employee ";

$R = mysql_query($C) or die(mysql_error());

$Crow = mysql_fetch_array($R);

$NoAge = $Crow[0];
//We will place these results into a table, build table.

echo '<table cellpadding="0" cellspacing="0" border="1">';

//Lets make it nice, and add table headers.
echo '<th>Name/Age</th>';
while($row1=mysql_fetch_array($Res1))
{
echo '<th>'.$row1[age].'</th>';

}

//Pull the data and store it in an array, so we can sort our $query.
while ($row = mysql_fetch_array($result))
{
$name = $row[name];

$age = $row[age];

$get = "select * from tbl_employee where id='$row[id]' and name='$name' and age='$age' ";

$get_result = mysql_query($get) or die(mysql_error());

$type = $row[type];

$storage["$name"] = $age;


echo '<tr><td width="100"><b>' . $name . '</b></td>';

while($get_row = mysql_fetch_array($get_result))
{
//echo $get_row['pallet_qty'];

for ($i = 1; $i <= $NoAge; $i++) //$i = 1 which is first value of $count // $i <= 6 which is highest value of $count;
{
echo '<td width="100">'.$get_row['type'].' </td>' ;
}
}
//End the row
echo '</tr>';
// }
//}
}
echo '</table>';
?> [/code]
Copy linkTweet thisAlerts:
@dai_hopMay 05.2009 — Name/age age1 age2 age3 age4 age5 age6 age7

name1 A B C D E F G

name2 K B C D E F G

name3 A L M P O S R

name4 A B C D E F G

name5 A B C D E F G

name6 A B C D E F G

name7 A B C D E F G[/QUOTE]


I'm confused. Is this the output your currently getting - or the output you'd like?
Copy linkTweet thisAlerts:
@kavionlyauthorMay 05.2009 — No, I would like to display in that format.
Copy linkTweet thisAlerts:
@dai_hopMay 05.2009 — That is what my code should output for you. What happens when you run my code?
Copy linkTweet thisAlerts:
@kavionlyauthorMay 05.2009 — I am getting age in multiple row. I want Age only on first row and Based on name and size type should come in row wise.

It should look like this

Name/age age1 age2 age3 age4 age5 age6 age7

name1 A B C D E F G

name2 K B C D E F G

name3 A L M P O S R

name4 A B C D E F G

name5 A B C D E F G

name6 A B C D E F G

name7 A B C D E F G

age1,age2,age3,...... age7

name1,name2,name3,.....name7 are the values getting from database. Based on name and size type should come i.e A B C....etc

Can u pls tell me how this can be done?
Copy linkTweet thisAlerts:
@dai_hopMay 05.2009 — My code can't output age in multiple rows. It creates and new row for each employee name and loops to output each age in a new cell of the same row until there are no more ages left, then it ends the row and starts a new one for next employee. Look at the HTML source code it generates.

The only way I can see this code falling over is if each employee has a different number of ages. Is this the case?

For example, employee A has 5 different ages where as employee B only has 2?

If this is the case you'll need to take empty cells into consideration.
Copy linkTweet thisAlerts:
@kavionlyauthorMay 05.2009 — [B]The only way I can see this code falling over is if each employee has a different number of ages. Is this the case?[/B]

Yes, Each employee has got different age. So how this can be done?
Copy linkTweet thisAlerts:
@dai_hopMay 05.2009 — You're being too vague, does each employee have a different [B]number[/B] of ages?
Copy linkTweet thisAlerts:
@kavionlyauthorMay 05.2009 — Practically not at all. In my code am using like that.
Copy linkTweet thisAlerts:
@SyCoMay 05.2009 — Multiple ages are of course possible to store in a database per employee. An age can store information related to the age. People have a single date of birth but as many ages as you wish to record. Imagine you store employees age/salary every year storing multiple ages can make sense, and is presumably something like what is being done here (although we don't know). I might store date of birth and date of record calculating the age of the employee but that just another way to do it.

Running SQL queries inside a loop that will grow with additional data, is a very bad idea. If you designed a site with 5 categories then you control how many subloops there are and you can loop the categories then the items in the categories. The code that queries a table that will grow every time a new person is employed is growing beyond your control. Imagine the company grows to 1000 employees, you don't need to run 1000 queries to gather all the information. One query to get all the data then use PHP to loop through. You should be minimizing the number of times you go from the web server to the SQL server. Basically any time you run a query in a loop there is most likely a better way to do it. Any time you're querying the same table inside a loop you're definitely doing it wrong.

kavionly please post your pseudo code in the code tags [noparse] [/noparse] so it lines up. [B]Please preview[/B] your post so you can see it looks OK and is easy to understand. You can copy and paste an empty tabbed space from a text editor to easily line things up , but still please preview before submitting.

You need to be more clear about what you are trying to achieve, I understand it's frustrating but if the people trying to help are confused its up to you to explain again and in more detail. Please explain your data structure. Perhaps post the result of
[CODE]show create table tablename[/CODE]
or
[CODE]desc tablename[/CODE]
So we know how the data is coming from SQL. Next provide an example of what the finished product should look like. Finally provide some example data. You might then get a completely different solution that will work way better. If you really want to get fast, quality help make it easy for the other forum members.

Provide the create table SQL. Provide a sample of SQL insert data so someone can simply copy the commands into SQL and have a copy of your table to play with. You will then get the help you need for sure! The first few lines from a mysql dump will give you all this info in a few seconds. From a command line.

[CODE]mysql -u username -pPassword database > textfile.txt[/CODE]

And another user can rebuild your table with your data. Then with your example of what you want to do, in your case an HTML table created by hand of what you want to achieve with PHP.
Copy linkTweet thisAlerts:
@kavionlyauthorMay 06.2009 — To be frank i was just using employee name and age without any sense. I know an employee will be having only one age. Actually i wanted it for different one i.e Size and Gsm. So now no confusion i think, i am going to provide all the data related to this.

Based On Size and Gsm i want to display pack_type and pack_quality. I am getting the format but its not matching. Pls check my code and let me know where i am going wrong.


[B]Code[/B]

[code=php]<?php
include "config.php";
echo '<table border="1">';
$Sql = "select * from jos_details where category_id='44' and sub_category_id='71' group by size";
$Res = mysql_query($Sql) or die(mysql_error());

$Sql5 = "select * from jos_details where category_id='44' and sub_category_id='71' group by gsm";
$Res5 = mysql_query($Sql5) or die(mysql_error());
echo '<th>' ."Size/Gsm". '</th>';
while($r = mysql_fetch_array($Res5))
{

echo '<th>'.$r[gsm].'</th>';
}
while ($row = mysql_fetch_array($Res))
{
echo '<tr><th>' . $row['size'] . '</th>';
$Sql = "SELECT * FROM jos_details WHERE category_id='".$row['category_id']."' and sub_category_id='".$row['sub_category_id']."' and size = '" . $row['size'] . "'";
$Res2 = mysql_query($Sql) or die(mysql_error());
while ($row2 = mysql_fetch_array($Res2))
{
echo '<td>' . $row2['pack_type'] . ' '. $row2['pack_quality'].'</td>';
}
echo '</tr>';
}

echo '</table>';
?> [/code]



[B]Table [/B]

[code=php]CREATE TABLE jos_details (
id int(11) NOT NULL auto_increment,
category_id int(11) default NULL,
sub_category_id int(11) default NULL,
size varchar(32) NOT NULL,
gsm varchar(32) NOT NULL,
published tinyint(4) default NULL,
color varchar(50) default NULL,
environmental varchar(50) NOT NULL default 'Virgin Fibre',
pack_type varchar(50) NOT NULL,
width varchar(100) NOT NULL,
length varchar(100) NOT NULL,
mic varchar(100) NOT NULL,
description varchar(80) NOT NULL,
quality_description varchar(80) NOT NULL,
pack_quality varchar(50) NOT NULL,
full_itemno varchar(40) NOT NULL,
product varchar(50) NOT NULL,
selling_group varchar(50) NOT NULL,
recycled varchar(50) NOT NULL,
pallet_qty varchar(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE[/code]




[B]Dumping data for table jos_details[/B]

[code=php]INSERT INTO jos_details (id, category_id, sub_category_id, size, gsm, published, color, environmental, pack_type, width, length, mic, description, quality_description, pack_quality, full_itemno, product, selling_group, recycled, pallet_qty) VALUES
(94, 44, 71, '420X297', '90', 1, '', 'FSC Mixed Credit', 'W', '', '', '100', '4CC UNCOATED NEW SHADE 173011 ', '4CC COLOUR COPIER (CUT SIZE)', '2500', '908258790', '82587', '33', '', '50000'),
(95, 44, 71, '420X297', '200', 1, '', 'FSC Mixed Credit', 'W', '', '', '196', '4CC UNCOATED NEW SHADE 373002 ', '4CC COLOUR COPIER (CUT SIZE)', '1250', '908259190', '82591', '33', '', '25000'),
(96, 44, 71, '420X297', '220', 1, '', 'FSC Mixed Credit', 'W', '', '', '215', '4CC UNCOATED NEW SHADE 373010 ', '4CC COLOUR COPIER (CUT SIZE)', '1000', '908259290', '82592', '33', '', '20000'),
(97, 44, 71, '420X297', '250', 1, '', 'FSC Mixed Credit', 'W', '', '', '245', '4CC UNCOATED NEW SHADE 373015 ', '4CC COLOUR COPIER (CUT SIZE)', '1000', '908259390', '82593', '33', '', '20000'),
(98, 44, 71, '210X297', '90', 1, '', 'FSC Mixed Credit', 'W', '', '', '100', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '2500', '908510890', '85108', '33', '', '100000'),
(99, 44, 71, '210X297', '100', 1, '', 'FSC Mixed Credit', 'W', '', '', '113', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '2000', '908511090', '85110', '33', '', '80000'),
(100, 44, 71, '210X297', '120', 1, '', 'FSC Mixed Credit', 'W', '', '', '125', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '2000', '908511190', '85111', '33', '', '80000'),
(101, 44, 71, '210X297', '160', 1, '', 'FSC Mixed Credit', 'W', '', '', '157', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '1500', '908511290', '85112', '33', '', '60000'),
(102, 44, 71, '210X297', '200', 1, '', 'FSC Mixed Credit', 'W', '', '', '196', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '1250', '908511390', '85113', '33', '', '50000'),
(103, 44, 71, '210X297', '220', 1, '', 'FSC Mixed Credit', 'W', '', '', '215', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '1250', '908511490', '85114', '33', '', '50000'),
(104, 44, 71, '210X297', '250', 1, '', 'FSC Mixed Credit', 'W', '', '', '245', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '1000', '908511590', '85115', '33', '', '40000'),
(105, 44, 71, '210X297', '280', 1, '', 'FSC Mixed Credit', 'W', '', '', '275', '4CC UNCOATED (SGS COC 2101) 21', '4CC COLOUR COPIER (CUT SIZE)', '1000', '908511690', '85116', '33', '', '32000'),
(106, 44, 71, '420X297', '100', 1, '', 'FSC Mixed Credit', 'W', '', '', '113', '4CC UNCOATED (SGS COC 2101) 42', '4CC COLOUR COPIER (CUT SIZE)', '2000', '908511890', '85118', '33', '', '40000'),
(107, 44, 71, '420X297', '120', 1, '', 'FSC Mixed Credit', 'W', '', '', '125', '4CC UNCOATED (SGS COC 2101) 42', '4CC COLOUR COPIER (CUT SIZE)', '2000', '908511990', '85119', '33', '', '40000'),
(108, 44, 71, '420X297', '160', 1, '', 'FSC Mixed Credit', 'W', '', '', '157', '4CC UNCOATED (SGS COC 2101) 42', '4CC COLOUR COPIER (CUT SIZE)', '1500', '908512090', '85120', '33', '', '30000'),
(109, 44, 71, '420X297', '280', 1, '', 'FSC Mixed Credit', 'W', '', '', '275', '4CC UNCOATED (SGS COC 2101) 42', '4CC COLOUR COPIER (CUT SIZE)', '800', '908512490', '85124', '33', '', '16000'),
(110, 44, 71, '450X640', '160', 1, '', 'FSC Mixed Credit', 'W', '', '', '157', '4CC UNCOATED (NEW SHADE)173024', '4CC UNCOATED (FOLIO)', '250', '908280190', '82801', '33', '', '10000'),
(111, 44, 71, '450X640', '200', 1, '', 'FSC Mixed Credit', 'W', '', '', '196', '4CC UNCOATED (NEW SHADE)373005', '4CC UNCOATED (FOLIO)', '250', '908280290', '82802', '33', '', '7500'),
(112, 44, 71, '450X640', '200', 1, '', 'FSC Mixed Credit', 'W', '', '', '196', '4CC UNCOATED (NEW SHADE)373005', '4CC UNCOATED (FOLIO)', '250', '908280390', '82803', '33', '', '7500'),
(113, 44, 71, '450X320', '200', 1, '', 'FSC Mixed Credit', 'W', '', '', '196', '4CC UNCOATED (NEW SHADE)373003', '4CC UNCOATED (FOLIO)', '250', '908281090', '82810', '33', '', '15000'),
(114, 44, 71, '450X320', '220', 1, '', 'FSC Mixed Credit', 'W', '', '', '215', '4CC UNCOATED (NEW SHADE)373012', '4CC UNCOATED (FOLIO)', '125', '908281190', '82811', '33', '', '12500'),
(115, 44, 71, '450X640', '100', 1, '', 'FSC Mixed Credit', 'W', '', '', '113', '4CC UNCOATED FSC 173070 450X64', '4CC UNCOATED (FOLIO)', '500', '908475090', '84750', '33', '', '15000'),
(116, 44, 71, '450X640', '120', 1, '', 'FSC Mixed Credit', 'W', '', '', '125', '4CC UNCOATED FSC 173071 450X64', '4CC UNCOATED (FOLIO)', '250', '908475190', '84751', '33', '', '12500'),
(117, 44, 71, '450X320', '90', 1, '', 'FSC Mixed Credit', 'W', '', '', '100', '4CC UNCOATED FSC 173073 450X32', '4CC UNCOATED (FOLIO)', '500', '908475390', '84753', '33', '', '30000'),
(118, 44, 71, '450X320', '100', 1, '', 'FSC Mixed Credit', 'W', '', '', '113', '4CC UNCOATED FSC 173074 450X32', '4CC UNCOATED (FOLIO)', '500', '908475490', '84754', '33', '', '30000'),
(119, 44, 71, '450X320', '120', 1, '', 'FSC Mixed Credit', 'W', '', '', '125', '4CC UNCOATED FSC 173075 450X32', '4CC UNCOATED (FOLIO)', '250', '908475590', '84755', '33', '', '25000'),
(120, 44, 71, '450X320', '160', 1, '', 'FSC Mixed Credit', 'W', '', '', '157', '4CC UNCOATED FSC 173076 450X32', '4CC UNCOATED (FOLIO)', '250', '908475690', '84756', '33', '', '20000'),
(121, 44, 71, '450X320', '250', 1, '', 'FSC Mixed Credit', 'W', '', '', '245', '4CC UNCOATED FSC 373074 450X32', '4CC UNCOATED (FOLIO)', '125', '908476090', '84760', '33', '', '12500'),
(122, 44, 71, '450X640', '280', 1, '', 'FSC Mixed Credit', 'W', '', '', '275', '4CC UNCOATED FSC 373078 450X64', '4CC UNCOATED (FOLIO)', '125', '908476190', '84761', '33', '', '6500'),
(123, 44, 71, '450X320', '280', 1, '', 'FSC Mixed Credit', 'W', '', '', '275', '4CC UNCOATED FSC 373079 450X32', '4CC UNCOATED (FOLIO)', '', '908476290', '84762', '33', '', '10000'),
(124, 44, 71, '450X320', '300', 1, '', 'FSC Mixed Credit', 'W', '', '', '300', '4CC UNCOATED FSC 373080 450X32', '4CC UNCOATED (FOLIO)', '125', '908476390', '84763', '33', '', '10000');[/code]
Copy linkTweet thisAlerts:
@SyCoMay 06.2009 — That's great but you still haven't provided an example of what you want to achieve. Make a mock up in regular HTML.
Copy linkTweet thisAlerts:
@kavionlyauthorMay 07.2009 — Here is the HTML mock up

[code=html]<html>
<body>
<table width="100&#37;" border="1" >

<tr>
<td >Size/Gsm</td>
<td>80</td>
<td>90</td>
<td>100</td>
<td>110</td>
</tr>

<tr >
<td>450X640</td>
<td>W 500B 25000</td>
<td>W 500B 22000</td>
<td>W 500B 19000</td>
<td>W 250B 16500</td>
</tr>

<tr>
<td>640X900</td>
<td></td>
<td>W 500B 12000</td>
<td>W 250B 8500</td>
<td>W 250B 9250</td>
</tr>

<tr>
<td>740X500</td>
<td>W 400B 13000</td>
<td></td>
<td>W 250B 2300</td>
<td></td>
</tr>

<tr>
<td>140X200</td>
<td></td>
<td>W 300B 1800</td>
<td></td>
<td>W 750B 200</td>
</tr>
</table>
</body>

</html>[/code]
Copy linkTweet thisAlerts:
@SyCoMay 07.2009 — ... and how does the data in our mock up relate to the data in what you supplied? Again we have no special skills here. I can see a w so that's environment, right? But I can't see a 250B and are some of the numbers product, pallet_qty?

The point is to make it easy for someone to help you. Here we are at post 19 and it's still unclear.

I think you need to build and array from the result of the query in a loop that displays nothing. You don't need to query a second time in a loop. You can use category ids as the array keys. If you need sub loop, build them using the category id and whatever other id you need in a multi dimensional array. Then you do your display loop as foreach and have the category id for each iteration to access the other items. You canthen sub loop if you have to, and again access the next level of the array. No query inside a loop that grows with new data is needed.
Copy linkTweet thisAlerts:
@kavionlyauthorMay 08.2009 — sorry for that. W is pack_type and here is my HTML mock up which matches data exactly what i have given.

I tried using an array but dint work for me. can u pls give me one small example?

[code=html]<html>
<body>
<table width="100&#37;" border="1" >

<tr>
<td >Size/Gsm</td>
<td>90</td>
<td>100</td>
<td>120</td>
<td>160</td>
<td>200</td>
<td>220</td>
<td>250</td>
<td>280</td>
<td>300</td>
</tr>

<tr>
<td>420X297</td>
<td>W 2500</td>
<td></td>
<td>W 2000</td>
<td>W 1500</td>
<td>W 1250</td>
<td>W 1000</td>
<td>W 1000</td>
<td>W 800</td>
<td></td>
</tr>

<tr>
<td>210X297</td>
<td>W 2500</td>
<td>W 2000</td>
<td>W 2000</td>
<td>W 1500</td>
<td>W 1250</td>
<td>W 1250</td>
<td>W 1000</td>
<td>W 1000</td>
<td></td>
</tr>

<tr>
<td>450X640</td>
<td>W 500</td>
<td></td>
<td>W 250</td>
<td>W 250</td>
<td>W 250</td>
<td></td>
<td></td>
<td>W 125</td>
<td></td>
</tr>

<tr>
<td>450X320</td>
<td>W 500</td>
<td>W 500</td>
<td></td>
<td>W 250</td>
<td>W 250</td>
<td>W 125</td>
<td>W 125</td>
<td>W</td>
<td>W 125</td>
</tr>

</table>
</body>

</html>[/code]
Copy linkTweet thisAlerts:
@kavionlyauthorMay 21.2009 — Am still struggling with that problem any help pls?
×

Success!

Help @kavionly spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...