zid Posted August 16, 2012 Share Posted August 16, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/267193-need-help-get-this-query-and-extraction-correct/ Share on other sites More sharing options...
Christian F. Posted August 16, 2012 Share Posted August 16, 2012 You can have a look at a previous post I made, which showcases how to make a HTML table from an array with unknown length. Quote Link to comment https://forums.phpfreaks.com/topic/267193-need-help-get-this-query-and-extraction-correct/#findComment-1370012 Share on other sites More sharing options...
zid Posted August 16, 2012 Author Share Posted August 16, 2012 Hi Christian, Hmm what does it do? Saves all the dates and costs in various variables for easier printing? hmmm Quote Link to comment https://forums.phpfreaks.com/topic/267193-need-help-get-this-query-and-extraction-correct/#findComment-1370013 Share on other sites More sharing options...
Christian F. Posted August 17, 2012 Share Posted August 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267193-need-help-get-this-query-and-extraction-correct/#findComment-1370094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.