PHP - Print Dates In Descending Order Without Affecting The Query
i am printing the dates from the last 30 days in ascending order and then comparing them in my query to print the results. How can I can print the below array in descending order without affecting my query?
Code: [Select] $thirtydaysago = time() - (30 * 24 * 60 * 60); $oneday=24 * 60 * 60; for($i=0;$i<31;$i++) { $d[$i]= $thirtydaysago + ($i*$oneday); echo date('Y-m-d',$d[$i])."<br>"; } for($i=0;$i<31;$i++) { $postsql=mysql_query("SELECT DATE_FORMAT(FROM_UNIXTIME(dateline), '%Y-%m-%d') AS FmtDate, COUNT(postid) AS PostCnt FROM post " . "WHERE dateline < '" . $d[$i+1] . "' AND dateline >= '" . $d[$i] . "' GROUP BY DATE_FORMAT(FROM_UNIXTIME(dateline), '%Y-%m-%d') DESC") or die(mysql_error()); if($postsql_rows=mysql_fetch_assoc($postsql)) { $data_date[$i]["date"]=$postsql_rows['FmtDate']; $data_postcount[$k][$i]["postcount"]=$postsql_rows['PostCnt']; } } $k++; } Similar TutorialsHow can I make this output in descending order from greatest to least? Right now, it appears like: 1940 1941 1942 1943 1944 etc... i'd like it to be like 2012 2011 2010 2009 etc.. until it gets to 1940 any ideas? here is my code: <? $i = 1939; while ($i < 2012) { $i++; echo '<option value="'.$i.'">'.$i.'</option>'; } ?> I am trying to display images on a html page alphabetically in descending order by their filename. <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a><br />;"; } } closedir($handle); } ?> Any help is greatly appreciated! how do I order two tables by date so that the most recent are first and the most unrecent last? like table 1 should be first then a table 2 then a table 1. does this make sense? here is my code $query = "SELECT * from links ORDER BY field_4 DESC"; // assumes that your date column is a DATE data type so that ordering by it will sort the dates $result = mysql_query($query); // check if the query executed without error and if it returned any rows here... // retrieve and process the data from the query $last_date = ''; // initialize to a value that will never exist as data while($row = mysql_fetch_assoc($result)){ // test for a new date and output the date heading if($last_date != $row['field_4']){ echo "<p><font face='Arial, Helvetica, sans-serif'>"; echo $row['field_4'] . "<br />"; echo "</font></p>"; $last_date = $row['field_4']; // remember the new date } // output the data (under each date heading) echo "<img src=" . $row['field_1'] . " border=0>"; echo "<a href='" . $row['field_3'] . "'>"; echo $row['field_2'] . "</a><br />"; } mysql_close($con); i would like set the data under each heading to be ordered by a different column, how do i do so? Hi I have been trying to populate my data from 3 tables and display like this: DATE $acronym1 $acronym2 POSTS 1/9/2011 10 (this is no of posts) 3 POSTS 31/8/2011 20 10 and so on. the threads and user table will be similar as well. I am not able to populate the distinct dates since the dateline contains time as well. So when I try to print out the just dates(without time) , it prints with the dates repeated. it prints something like this: 1/9/2011 1 1/9/2011 1 and so on.. How can populate and print the the date and number of posts in the above format. Here is my complete code below: Code: [Select] <?php error_reporting(-1); //ini_set('display_errors',1); include("db.php"); //$thirty_days_ago = strtotime("-30 days"); $limit = strtotime("-1 month"); $sql=mysql_query(("SELECT * from new_site"),$con) or die(mysql_error()); while($row=mysql_fetch_assoc($sql)) { $include=$row['include']; $forumurl=$row['forumurl']; $url=$row['url']; $acronym=$row['acronym']; include("$include"); //echo $include."<br>"; $configdbname=$config['Database']['dbname']; $configdbconport=$config['MasterServer']['servername'].":".$config['MasterServer']['port']; $configusername=$config['MasterServer']['username']; $configpassword=$config['MasterServer']['password']; $configprefix=$config['Database']['tableprefix']; /* Connect to the required database */ $con2=mysql_connect($configdbconport, $configusername, $configpassword); if (!$con2) { die('Could not connect: ' . mysql_error()); } mysql_select_db($configdbname, $con2); $postdate=mysql_query("SELECT DISTINCT dateline,postid from post WHERE dateline >='$limit' ORDER by dateline DESC") or die(mysql_error()); while($postdate_results=mysql_fetch_assoc($postdate)) { $postdate_record=$postdate_results['dateline']; // echo $postdate."<br>"; $postdate_formatted=date('M dS Y ',$postdate_results['dateline']); $post_count=mysql_query("SELECT * from post WHERE dateline >='$postdate_record'"); while($post_count_results=mysql_fetch_assoc($post_count)) { //$postdate_formatted=date('M dS Y ',$post_dateline_results['dateline']); $posts=mysql_num_rows($post_count) or die(mysql_error()); //echo $acronym.":POSTS:".$posts."<br>"; echo '<table border="1">'; echo "<tr>"; echo "<th>Category</th>"; echo "<th>".$acronym."</th>"; echo "</tr>"; echo "<tr>"; echo "<td>POSTS:DATE:".$postdate_formatted."</td>"; echo "<td>".$posts."</td>"; echo "</tr>"; } $threaddate=mysql_query("SELECT * from thread WHERE dateline >='$limit' ORDER by dateline DESC") or die(mysql_error()); while($threaddate_results=mysql_fetch_assoc($threaddate)) { $threaddate_record=$threaddate_results['dateline']; $threaddate_formatted=date('M dS Y ',$threaddate_results['dateline']); $thread_count=mysql_query("SELECT * from thread WHERE dateline='$threaddate_record'"); while($thread_count_results=mysql_fetch_assoc($thread_count)) { $threads=mysql_num_rows($thread_count) or die(mysql_error()); //echo $acronym.":THREADS:".$threads."<br>"; echo "<tr>"; echo "<td>THREADS:DATE:".$threaddate_formatted."</td>"; echo "<td>".$threads."</td>"; echo "</tr>"; $userdate=mysql_query("SELECT * from user WHERE joindate >='$limit' ORDER by joindate DESC") or die(mysql_error()); while($userdate_results=mysql_fetch_assoc($userdate)) { $userdate_record=$userdate_results['joindate']; $userdate_formatted=date('M dS Y ',$userdate_results['joindate']); $user_count=mysql_query("SELECT * from user WHERE joindate='$userdate_record'"); while($user_count_results=mysql_fetch_assoc($user_count)) { $users=mysql_num_rows($user_count) or die(mysql_error()); //echo $acronym.":USERS REGISTERED:".$users."<br>"; echo "<tr>"; echo "<td>REGISTERED USERS::DATE:".$userdate_formatted."</td>"; echo "<td>".$users."</td>"; echo "</tr>"; } } } } } } echo "</table>"; ?> how do i grab and print on page in date order all .jpg files in a folder ? Hi
Been trying to run a query to get all rows from a table between two dates, but nothing seems to work.
$query = "SELECT * FROM table WHERE date BETWEEN '%2014-11-17%' AND '%2014-11-18%'";
Strange thing if I try a search instead a query in phpmyadmin I don't see an operator called 'BETWEEN' on the remote host, but I do on my localhost.
Does this mean it will never work on the remote host? Nevertheless it doesn't work on either and I do have records for both dates in the table.
phpmyadmin:
Version 4.1.14 localhost (wamp) Version 3.3.7deb7 on remote server Thanks Hi, my query doesn't work, I've got a date field in my MySql table, I want to get results of all employees that was added in a certain period. My query doesn't work but as soon as I type in values in my query instead of variables it works, what am I doing wrong? Code: [Select] $date = date('Y/m'); $date1 = strtotime('-6 month'); $date2 = strtotime('-6 month'); echo date('Y', $date1); echo date('m', $date2); $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $sql = "SELECT * FROM detail WHERE year(engaged) > '$date1' and month(engaged) > '$date2'"; $result=mysql_query($sql); echo mysql_num_rows($result); I need to write a query that will sum a column 'serv_cc_total' from the first day of the month to the date the record was saved to the db when the record for that date is viewed. Any help would be appreciated. Doug I have a mysql database where users enter their data using a form that includes a rundate input field that generates a calendar date in this type of format: xx/xx/xxxx into a mysql database field called rundate. I would like to query the database to search a string of dates that are in the database, such as 01/01/2011, 01/02/2011, 01/03/2011 but I'm not sure on how to code this into my search.php. I have attached the search page to this post. Any suggestions would be greatly appreciate, and if you need me to be more expanded on my explanation, just let me know....i try to keep it very simplistic. [attachment deleted by admin] I am attempting to order the results of a query using the get value but they wont order. here is the form: echo "<form name=\"order_form\" action=\"{$site_root}/index.php\" method=\"GET\">"; echo "<input type=\"hidden\" value=\"{$forum_id}\" name=\"forum\" />"; echo "<select name=\"order_by\" style=\"margin-left:5px;\">"; echo "<option value=\"asc\" name=\"order_option\" />Order By: Ascending</option>"; echo "<option value=\"desc\" name=\"order_option\" />Order By: Descending</option>"; echo "</select>"; echo "<select name=\"sort_by\" style=\"margin-left:5px;\">"; echo "<option value=\"topic_name\" name=\"sort_option\" />Sort By: Topic Name</option>"; echo "<option value=\"topic_poster\" name=\"sort_option\" />Sort By: Topic Author</option>"; echo "<option value=\"topic_time_posted\" name=\"sort_option\" />Sort By: Time Posted</option>"; echo "<option value=\"topic_views\" name=\"sort_option\" />Sort By: Topic Views</option>"; echo "<option value=\"topic_replies\" name=\"sort_option\" />Sort By: Topic Replies</option>"; echo "<option value=\"topic_last_poster\" name=\"sort_option\" />Sort By: Last Poster</option>"; echo "<option value=\"topic_last_post_time\" name=\"sort_option\" />Sort By: Last Post Time</option>"; echo "</select>"; echo "<input type=\"submit\" value=\"Order\" />"; echo "</form>"; here is the query: $topic_info_query = $db->query("SELECT f.forum_id, f.forum_name, m.user_id, m.user_username, m.user_group, t.thread_topic_id, t.topic_name, t.topic_poster, t.topic_time_posted, t.topic_views, t.topic_replies, t.topic_last_poster, t.topic_last_post_time, t.topic_locked, t.topic_sticky, t.topic_edited, t.topic_last_poster_id, t.topic_last_poster_group, t.topic_icon FROM ".DB_PREFIX."topics as t LEFT JOIN ".DB_PREFIX."members as m ON t.topic_poster = m.user_username LEFT JOIN ".DB_PREFIX."forums as f ON t.forum_id = f.forum_id WHERE t.forum_id = '$forum_id' '".$sort_by . ' ' . $order_by."'") and here is where $sort_by and $order_by are defined: if (isset($_GET['order_by'])) { $order_by = mysql_real_escape_string($_GET['order_by']); $order_by = strtoupper($order_by); } if (isset($_GET['sort_by'])) { $sort_by = 'ORDER BY t.'.mysql_real_escape_string($_GET['sort_by']); } when i echo '".$sort_by . ' ' . $order_by."' which is how it appears in the query i get: t.topic_last_post_time DESC which is exactly right. But the results are not being sorted. The echoed variables change as does the url but no sorting happens. any ideas? Script:
<?php $tqs_admin_flag = "SELECT * FROM `flags`"; $tqr_admin_flag = mysqli_query($dbc, $tqs_admin_flag) or die(mysqli_error($dbc)); while($row_admin_flag = mysqli_fetch_array($tqr_admin_flag)){ //print_r($row_admin_flag); $tqs_admin_flag_thread = "SELECT * FROM `thread` WHERE `id` = '" . $row_admin_flag['thread_id'] . "' ORDER BY `date_created` DESC"; $tqr_admin_flag_thread = mysqli_query($dbc, $tqs_admin_flag_thread) or die(mysqli_error($dbc)); while($row = mysqli_fetch_assoc($tqr_admin_flag_thread)){ include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/admin_flag_thread.php"); } } ?>This in comparison works: $tqs_admin_flag_thread = "SELECT * FROM `thread` ORDER BY `date_created` DESC";I would appreciate suggestions for which reasons the above script (the way it is) may not work. With the above script the querying and printing on screen does happen, though the "ordering by DESC" does not happen. Edited by glassfish, 24 October 2014 - 11:38 AM. I have a basic query where I am retrieving project records. I basically want to show the records from newest to oldest, but I also want to show the Featured projects first. Each Featured project is marked by "1" in the mysql table column. If it's not featured, then it's marked "0". $find_records = $db->prepare("SELECT * FROM projects ORDER BY project_id DESC, featured DESC LIMIT 10"); $find_records->execute(); $result_records = $find_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0) { foreach($result_records as $row) { } } The issue with above query is that the ORDER BY works only for the first component(project_id) and ignores the second component(featured). Do you see what's wrong with it? Hi, say I have a query like so: foreach($_REQUEST['r'] as $position => $row) { $row = implode("', '",$row); $result = mysql_query("Insert into player_stats (position,shirt_number,player_id,goals,cards,substituted,used_sub,injury,season,report_id) values('$position','$row','$currentSeason','$report_id')"); } How do I print the results of that query to my Browser for each time? Thanks Hello all, I am having this very frustrating issue, I am trying to print this query i got from my SQL table. However, If the query returns 10 [1,2,3,4,5,6,7,8,9] results it just prints 5 [2,4,6,8,10] . I have no idea what to do. Code: [Select] <!--Error Reporting Production Only[start]--> <?php error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors','1'); //Error Reporting Production Only[end]--> //Connect to Database [start]--> require_once 'sqllogin.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); //Connect to Database [end]--> //Process the search [start]--> //Initialize the search output variable $search_output = ""; $sql_command = "SELECT * FROM `Alpha` WHERE 1"; //See if the posted search field is set and has a value if (isset($_POST['searchquery']) && $_POST['searchquery'] !="") {//IF START 1 //run code if condition is met //Filter the search query user input $searchquery = preg_replace('#[a-z 0-9 A-Z]#i', '', $_POST['searchquery']); // Search Query********************* if($_POST['filter1'] == "All") {//all filter code goes her $sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE firstname LIKE '%$_POST[searchquery]%' OR lastname LIKE '%$_POST[searchquery]%' OR id LIKE '%$_POST[searchquery]%'"; } else if($_POST['filter1'] == "Last Name") {//Last Name filter code goes her $sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE lastname LIKE '%$_POST[searchquery]%'"; } else if($_POST['filter1'] == "First Name") {//First Name filter code goes her $sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE firstname LIKE '%$_POST[searchquery]%'"; } else if($_POST['filter1'] == "First Name") {//ID filter code goes her $sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE id LIKE '%$_POST[searchquery]%'"; } }//IF END 1 $query = mysql_query($sql_command) or die(mysql_error()); $count = mysql_num_rows($query); if ($count>0) { $search_output.= "<hr /> $count results for <strong>$_POST[searchquery]</strong><hr /> $sql_command <hr />**IF**POST = $_POST[searchquery] Count=$count, Query= $query<hr />"; /*for ($j = 0 ; $j < $rows ; ++$j) { $id = $row["id"]; $lastname = $row["lastname"]; $firstname = $row["firstname"]; $building = $row["building"]; $room = $row["room"]; $search_output .= "$id \t $firstname \t\t $lastname \t\t $building \t$room<br />"; } *************** Can be used instead but not here try putting it where $search_output is echo 'ID: ' . $row[0] . '<br />'; echo 'Last Name: ' . $row[1] . '<br />'; echo 'First Name: ' . $row[2] . '<br />'; echo 'Building: ' . $row[3] . '<br />'; echo 'Room: ' . $row[4] . '<br /><br />'; *********************** */ while($row = mysql_fetch_array($query)) { $row = mysql_fetch_array($query); $id = $row[0]; $lastname=$row[1]; $firstname=$row[2]; $building=$row[3]; $room=$row[4]; $search_output .= "$id $firstname $lastname $building $room<br />"; } } else { $search_output ="<hr /> 0 results for <strong>$searchquery</strong><hr /> $sql_command <hr />****ELSE**POST = $_POST[searchquery] Count=$count, Query= $query<hr /" ; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MC Reslife WEB APP Developed by; JB</title> <link href="styles.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="Header">Monroe College Resident's Life Web App</div> <div id="Page"> <div id="Menu"> <h1>Search</h1> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> <input name="searchquery" type="text" maxlength="19" size="19"/> <input name="myBTN" type="submit" /> <br /> Search By: <select name="filter1"> <option value="All">All</option> <option value="Last Name">Last Name</option> <option value="First Name">First Name</option> <option value="ID Number">ID Number</option> </select> </form> <hr /> <h1>Menu</h1> <p><a href="#">Alpha List</a></p> <p><a href="#">Lock Out Log</a></p> <p><a href="#">Current Probations</a></p> </div> <div id="Content"> <h1>Alpha List</h1> <?php echo $search_output; ?> <p>*************************</p> <?php echo "this is what in post $_POST[searchquery]"?> </div> </div> <div id="Footer">Content for id "Footer" Goes Here</div> </body> </html> please help me I am trying to use the following query with sorting: $query = mysql_query("SELECT * FROM Table WHERE Date LIKE '$PeriodSelected' ORDER BY Date DESC"); $PeriodSelected is basically supposed to get me all results from month selected and it works fine. Now I am trying to add sorting to this query and although I get no errors it does not matter if I use ASC or DESC my results are sorted in the same way. Not sure what I am doing wrong. i have this query and i'm trying to order all of the results by `created` ascending. I've tried putting the later query before the first but the single row from the first query(the way it is right now) gets stuck as the last row in the results. How can i order the results of both queries? Code: [Select] ( SELECT * FROM Account_activity WHERE DATE( created ) < '2011-08-01' AND username = '40' ORDER BY created DESC LIMIT 1 ) UNION ( SELECT * FROM Account_activity WHERE MONTH( created ) = '08' AND YEAR( created ) = '2011' AND username = '40' ORDER BY created ASC ) I am loading notifications from a database table called "notifications" and I am having a little trouble getting them to order in the correct way. my query I'm using right now: Code: [Select] $query = mysql_query("SELECT B.* FROM (SELECT A.* FROM notifications A WHERE A.user_id='$session' AND A.from_id!='$session' ORDER BY A.id ASC ) AS B ORDER BY B.state ASC LIMIT 7"); this works well as far as showing the unread notifications on top, then the read notifications below, however it's not ordering the two sets by ID (which the id auto increments so the higher id number is the newest) from newest on top to the oldest on bottom, still keeping them separated by the unread on top, read on bottom (column name is state for showing whether they're read or not). The order the notifications are displaying by their ID is: 3 5 2 4 1 when it should be: 5 3 2 4 1 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=332593.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312690.0 |