Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Generating table rows and column by data
Old 08-11-2008, 10:50 PM Generating table rows and column by data
Junior Talker

Posts: 4
Trades: 0
Hello World,
I'am a newbie.
I have 2 mysql database, let say db1 and db2.

db1 :

--------------
| id1 | name |
--------------
|......|.........|
--------------

db2 :

------------------------------
|-id2-|-id1-|-date-|-wrk_hrs-|
------------------------------
|......|.......|.........|.............|
------------------------------


I want to generate a report like this :

--------------------------------------------------------
|-date-|----------------name--------------------------|
| ....... |-----------------------------------------------|
| ....... |----$name#1--------|-----$name#2-------| ... | ---> (*)
--------------------------------------------------------
|-$date| $wrk_hrs of name#1 | $wrk_hrs of name#2 | ... |
--------------------------------------------------------
.
.
.

(*) = "generate from db1's data"

How to create report like above? Pls help me.

Thx b4.

Last edited by eprompt; 08-11-2008 at 10:51 PM..
eprompt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-12-2008, 12:46 AM Re: Generating table rows and column by data
Extreme Talker

Posts: 189
Trades: 0
try http://www.tizag.com/mysqlTutorial/mysqljoins.php
simster is offline
Reply With Quote
View Public Profile
 
Old 08-12-2008, 04:36 AM Re: Generating table rows and column by data
Junior Talker

Posts: 4
Trades: 0
Thx...
I have read the link.

But I still have problem to generate row (from db2) and column (from db1) and fill it with the correct value.
eprompt is offline
Reply With Quote
View Public Profile
 
Old 08-12-2008, 08:22 AM Re: Generating table rows and column by data
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Just query both databases seperately. Throw the information into arrays and then output it into the table. So name your first array 'name' and fill it with the name column from DB1. Then make another for 'date' and another for 'work hours'. From there just use a 'for' loop.

I think thats the easiest way to do it.
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 08-13-2008, 04:49 AM Re: Generating table rows and column by data
Junior Talker

Posts: 4
Trades: 0
This is my code :
PHP Code:
<?php 
$host
="localhost";
$myuser="root";
$mypass="admin";
$mydb="ex1";
mysql_connect($host,$myuser,$mypass);
mysql_select_db($mydb);
$query=mysql_query;
$fetch=mysql_fetch_array;

echo 
"<table border=1>
<tr valign=top bgcolor=#eeeeee>
<td>name</td>"
;
$e=$query("SELECT * FROM std ORDER by id1")or die(mysql_error());

while(
$headers=$fetch($e))
{
    echo 
"<td><div align=center>$headers[name]</td></div>";
}
echo 
"</tr>";
$e=$query("SELECT DISTINCT exam FROM exam ORDER BY id2");

while(
$exam=$fetch($e))
{
    echo 
"<tr valign=top><td bgcolor=#eeeeee>$exam[exam]</td>";

    
$d=$query("SELECT * FROM exam LEFT JOIN std ON std.id1=exam.id1 ORDER BY id2");
    while(
$e=$fetch($d))
    {
        if(!
$e[result])
        {
            
$e[result]=0;
        }
        else
        {
            
$e[result]="<b>$e[result]</b>";
        }
        echo 
"<td><div align=right>$e[result]</div></td>";
    }
    echo 
"</tr>";
}
echo 
"</table>";
mysql_close(); 
?>
And this is the database :

Code:
CREATE TABLE `std` (
`id1` int(5) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id1`)
) ENGINE=MyISAM;


INSERT INTO `std` VALUES ('', 'John');
INSERT INTO `std` VALUES ('', 'Matt');
INSERT INTO `std` VALUES ('', 'Jean');

CREATE TABLE `exam` (
`id2` int(5) NOT NULL auto_increment,
`id1` int(5) NOT NULL,
`exam` varchar(10) NOT NULL,
`result` int(5) NOT NULL,
PRIMARY KEY (`id2`)
) ENGINE=MyISAM;

INSERT INTO `exam` VALUES ('', '1', 'bio', '7');
INSERT INTO `exam` VALUES ('', '1', 'phy', '8');
INSERT INTO `exam` VALUES ('', '1', 'mat', '8');
INSERT INTO `exam` VALUES ('', '2', 'bio', '6');
INSERT INTO `exam` VALUES ('', '2', 'phy', '7');
INSERT INTO `exam` VALUES ('', '2', 'mat', '8');
INSERT INTO `exam` VALUES ('', '3', 'bio', '8');
INSERT INTO `exam` VALUES ('', '3', 'phy', '6');
INSERT INTO `exam` VALUES ('', '3', 'mat', '7');
But the result isnt exactly what I want.
The example of result what I want should be like this :

exam| John | Matt | Jean |
bio |---7------6------8
phy|---8------7------7
mat|---7------8------7


Pls help. Thx.
eprompt is offline
Reply With Quote
View Public Profile
 
Old 08-13-2008, 05:12 AM Re: Generating table rows and column by data
Junior Talker

Posts: 4
Trades: 0
sorry double post

Last edited by eprompt; 08-13-2008 at 05:16 AM.. Reason: double post
eprompt is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Generating table rows and column by data
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23854 seconds with 12 queries