Hello, I have a problem with selecting from database. I have a db and a table. In the table I have one column with car name and one column with the date when the car was added in the database table. I want to select from that table and to view on my website all cars with the date when was added. I know to make the code for what i wrote until here. The problem is: I want to output all the cars which were added in july (for example) and for august separately in another div tag and for september in another separately one (but on the same page) and so on. So I want to output all the cars arranged by month and each month to be displayed in a separate (div tag for example). Note that I want automaticaly do this because I have to display cars from last 10 years. I need only the script that make this sort by month and with the possibility to display each month cars in a separate div tag . Sorry for my bad english. Please help me with this problem. Any help is apreciated. Thank you in advance.
I'm giving you this code to start with, I haven't test it yet but I know It will work. I assume that you stored your date in your table in a date data type column. Just change this example to your table name and row name. $query = 'SELECT car_name,add_date FROM tbl_motors ORDER BY add_date ASC'; $result = mysql_query($query); $m = ''; //month while($row = mysql_fetch_array($result)){ $int_month = substr($row['add_date'],5,2); if(($int_month != $m) && ($m != '')){ $m = $int_month; $div .= '</div><div>Car Name: '.$row['car_name'].' Date Added: '.$row['add_date'].'<br>'; }else{ $div .= 'Car Name: '.$row['car_name'].' Date Added: '.$row['add_date'].'<br>'; }//end else }//end while echo '<div>'.$div.'</div>'; PHP:
Thank you php-lover !! This is what I was looking for. Thank you very much for your help. A++ It works perfectly. ><