Jump to content

Need help get this query and extraction correct


zid

Recommended Posts

Hi guys,

I need help to understand and get this to work.

I'm trying to lean php/mysql so this is a small script of mine.

 

I wanna try get a webpage tell me what type of cost I have each month for water, electricity and so forth.

I also want it to be displayed into a table like this:

 

        Jan        Feb        Mars
---------------------------------------
Water   1200       340        550
El      1100       300        600

 

...you get my point.

 

So started to read about how to have a good MySQL setup so started small using only three rows: type, date and cost

type = water, electricity, ensurance...

date = 2012-08-16 (dont know if thats a god way of doing it- only want to have that cost for that month displayed)

cost = the cost

 

As you can see in my code below im not off for a good start. Im a tru' rookie and I've read tons of guides online but I cant find a way of putting things where I want them into my table...

 

?>
<table>
<tr>
<th>Cost</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
</tr>
<?php

$query = 'SELECT * FROM test_data';
$result = mysql_query($query, $connect);
while ($row = mysql_fetch_assoc($result))
{
  echo "<tr>\n";
  echo "<td>".$row['type']."</td>\n";
  echo "<td>".$row['cost']."</td>\n";
  echo "<td>".$row['cost']."</td>\n";
  echo "<td>".$row['cost']."</td>\n";
  echo "</tr>\n";   
}
?>
</table>

 

My quesions are:

1. Have I completely misunderstood how I should start out with the SQL layout? maybee should have another "layout"?

2. In my loop I want to put the correct cost for that month in the correct place

3. I don't want to display the date any where, just for a query to know for what month that cost belongs to...

 

Please help me out, and describe what things are for what - or I can never learn...

 

Any help appreciated!

 

If you read the thread, you'll see what kind of output it gives. Also, do try to play around with it, while using some example values from a database, and you'll see for yourself. The PHP manual will also help explain what the different function does, so I highly recommend reading through it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.