Forum Moderators: open

Message Too Old, No Replies

creating a variable from multiple rows of data

creating a variable from multiple rows of data

         

drooh

11:42 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Ok this is kinda of hard for me to explain but Ill try my best.

Im making a php application that creates and invoice txt file from data stored in a mysql database.

In the invloice I need to list out each date/time that work was done and what was done on that date/time.

All of the text for the invoice is stored in a single variable called $stringdata. In this variable I want to include another variable called $billing info. $billing_info will look something like this when put into the text file

2007-10-15 1.2 hrs worked on project
2007-10-16 3.5 hrs worked on data1
2007-10-18 2.7 hrs worked on data2

ok, so my php looks something like this


$result = mysql_query("SELECT * FROM `log_time` WHERE `p_id` = 1");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$p_id = $row['p_id'];
$hr = $row['hr'];
$mn = $row['mn'];
$yyyy = $row['yyyy'];
$mm = $row['mm'];
$dd = $row['dd'];
$desc = $row['desc'];
$b_id = $row['b_id'];
$billing_info = $yyyy.$mm.$dd."\t".$hr.".".$mn."\t".$desc;
}

$stringdata = "
======================================================================
INVOICE# 20071018045820
======================================================================
My Company Name
1234 Ave A.
Houston TX, 78123
(512) 555-1357
======================================================================
Billed to:
$company_name
$contact_name
$address
$phone
$email
======================================================================
Service of:
$p_name

$billing_info

total due:

======================================================================
Please Make all Checks payable to:
My Biz
======================================================================
\n";

$file = fopen("invoices/invoice.txt","w");
fwrite($file,$stringdata);
fclose($file);

The problem with this is that there is more than 1 record in the DB for this result. With my current code I am only able to list out 1 billing_info.

How can I accumlate all of them into one variable?

I have looked at an older post of mine [webmasterworld.com...] to try and figure this out but I am still not able to get it. Thanks for any help!

dreamcatcher

7:02 am on Oct 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you not store the billing info in a seperate colum for each entry?

dc

drooh

8:34 am on Oct 24, 2007 (gmt 0)

10+ Year Member



I am not sure exactly what you mean? Could you explain a little more?

Thanks!

how would i do something like this

while($row = mysql_fetch_array($result)){
$id = $row['id'];
$p_id = $row['p_id'];
$hr = $row['hr'];
$mn = $row['mn'];
$yyyy = $row['yyyy'];
$mm = $row['mm'];
$dd = $row['dd'];
$desc = $row['desc'];
$b_id = $row['b_id'];
$billing_info = $yyyy.$mm.$dd."\t".$hr.".".$mn."\t".$desc;
echo $billing_info;
}
this outputs several rows of biiling info such as
2007-10-15 1.2 hrs worked on project
2007-10-16 3.5 hrs worked on data1
2007-10-18 2.7 hrs worked on data2

how would I get
$billing_info_total = "2007-10-15 1.2 hrs worked on project<br />
2007-10-16 3.5 hrs worked on data1<br />
2007-10-18 2.7 hrs worked on data2";

drooh

8:39 am on Oct 30, 2007 (gmt 0)

10+ Year Member



anyone..?

Habtom

8:50 am on Oct 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am taking part of your code and show you how to do that:

$billing_info = $yyyy.$mm.$dd."\t".$hr.".".$mn."\t".$desc;
echo $billing_info;
}

Change to the following:

$billing_info .= $yyyy.$mm.$dd."\t".$hr.".".$mn."\t".$desc;
$billing_info = $billing_info." <br>";
}

echo $billing_info;

drooh

7:24 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



thanks for the help but that only echo's the last row in the database, and if i put the echo inside the array it echos all the rows fine, but thats not what Im trying to accomplish.

I am trying again to get the output from that array into a single variable. Once in that variable I want to have that echoed into a txt file and email. make sense, please see this entire post for details. thank you!

Habtom

4:56 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That shouldn't echo the last line.

Look carefully at the bold part it is not just (=) it is (.=)

$billing_info .=$yyyy.$mm.$dd."\t".$hr.".".$mn."\t".$desc;
$billing_info = $billing_info." <br>";
}

echo $billing_info;

drooh

5:14 am on Oct 31, 2007 (gmt 0)

10+ Year Member



Ok, great that seems to do it! Such a simple solution that I've been looking for!

So can you refer me to some documentation or explain to me how this works?

I'm assuming that the . (period) is the concatenator and in this manner its adding to itself through each cycle.

Habtom

5:21 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm assuming that the . (period) is the concatenator and in this manner its adding to itself through each cycle.

Yea, that is pretty much it.

[edited by: Habtom at 5:21 am (utc) on Oct. 31, 2007]