开发者

Associating tables in PHP + MySql

So, i'm creating a money management s开发者_开发问答ystem. My database has 2 tables: main(id,timestamp,value1,value2) and fields(value1,value2).

Value1 and value2 are the categories in which the money are spent. In fact, the script display a table where the rows are the days and the columns are the fields where the money have been spent into (sport, work, girlfriend, etc).

Now, i have a problem. The main query is this:

SELECT * FROM main WHERE FROM_UNIXTIME(timestamp, '%M %Y') = '$date' ORDER BY timestamp ASC

as you see, i extract data only from the main table. So if i wanna list all the categories contents i have to write manually the category name for each one of them, like this:

echo "<td>".$row['value'] ."€</td>";
echo "<td>".$row['value2'] ."€</td>";

Is there any way it could list all the category name (showing the category content) automatically?


Maybe you just need to cycle through the $row array?

foreach ($row as $content) {
    echo "<td>$content €</td>"; 
}


I think I understood what you want... you want to group your expendings by category, correct? I didn't understand how is the relation is done on your two tables so I can't help on you on the SQL statement you are looking for, you need to explain it better, but what you are looking for are something very special in SQL, called JOINs.

Here is a good website with examples: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

Hope I've helped

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜