dsoukup Posted December 18, 2008 Share Posted December 18, 2008 I apologize ahead of time for this newbie question. The website I am building is fairly simple and uses PHP/mySQL mainly to determine dates and retrieve info from the mySQL database based on those dates. For example, the table 'calendarium' is essentially a calendar which contains 'yday', 'mon', 'mday', etc...that resembles the array of getDate() along with specific information about each yday/mon/mday combination: 'feast', 'class', and so on. I have built the website so far so that $date = getDate(); I then query the db to find the specific info for $date. This works great as long as I only want people to view info specific for TODAY only. I would like to be able to allow people to switch dates by clicking on links for YESTERDAY or TOMORROW, for example. I thought I could accomplish this using a session: default it to today via getDate() but then modifying the session so that, for example: $_SESSION['date'][mday] = $_SESSION['date'][mday] + 1; Yes, I realize this could cause some date confusion so there will need to be some date validation going on, but I think this illustrates my point nonetheless. My question is how can I update that session variable via a link or button (labeled "Yesterday" for example) which would then refresh the page and allow the same page to be displayed but with the updated info pulled from the mySQL db. Below is an example of page where this would be used: <?php session_start(); # $_SESSION['date'] has already been defined elsewhere; contents are getDate(); echo "<div id=\"header\">"; /***************************************** * Connect to database: * Pass connection, user & password * If unable to connect, error out * Select db 'officium_breviarium' * If unable to select db, error out *****************************************/ $link = mysql_connect('localhost:/tmp/mysql5.sock', 'foo', 'bar'); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('officium_breviarium', $link); if (!$db_selected) { die ('Can\'t use officium_breviarium : ' . mysql_error()); } /***************************************** * Set dates * $today = GetDate() array of today * $roman_weekday = obtain name of day in Latin * $roman_date = obtain calendar date in Latin *****************************************/ $today = GetDate(); $roman_weekday = RomanWeekday($today[weekday]); $roman_date = RomanDate($today[mon], $today[mday]); /***************************************** * Echo title, subtitle & Latin date *****************************************/ echo "<h1>Martyrologium Romanum</h1>"; echo "<p class=\"subtitle\">Sanguis Martyrum Semen Christianorum</p>"; echo "<p class=\"info\">".$roman_weekday.". ".$roman_date.".</p>"; /***************************************** * Query calendarium to find * appropriate feast & commemoration(s) * then use PRINTF to display results *****************************************/ $query = sprintf("SELECT * FROM calendarium WHERE yday=%d", mysql_real_escape_string($today[yday]+1)); $res = mysql_query($query); $row = mysql_fetch_array($res); printf ("<p class=\"info\">%s, Classis: %s</p>", $row["feast"], $row["class"]); if($row[com1]!= NULL){ printf ("<p class=\"info\">Commemoratio: %s</p>", $row["com1"]); } if($row["com2"]!= NULL){ printf("<p class=\"info\">Commemoratio: %s</p>", $row["com2"]); } mysql_free_result($res); # Functions RomanWeekday($day) & RomanDate($month, $day) suppressed for forum readability ?> <div class="c2" id="navigation"> <a href="" target="Main">Yesterday</a> <span class="c1"> + </span> <a href="" target="Main">Today</a> <span class="c1"> + </span> <a href="" target="Main">Tomorrow</a> </div> I would like to be able to click on the "navigation" links for Yesterday, Today & Tomorrow and have the page essentially refresh with the appropriate information for those dates. I have searched various places online and have found references to using JavaScript, AJAX or a 1x1 pixel frame to help with this...but either their examples are too in-depth or address topics totally unrelated to what I want to do. Am I heading in the right direction to accomplish my goal of changing dates? Am I making this too hard or am I overlooking something obvious? I have only a month or so experience with PHP...essentially just enough to be able to query the db. Any help would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/137602-solved-newbie-update-session-refresh-page-with-link/ Share on other sites More sharing options...
Mad Mick Posted December 18, 2008 Share Posted December 18, 2008 You can change the link to goto to a processing page with a get variable e.g. <a href="procDate.php?date=yesterday">Yesterday</a> Then build a processing page called procDate.php. All that page needs to do is read/validate the get variable then update the session variable with the date. Then use header("Location:the_original_page.php") to go back to the original page. It can also be done on the same page instead but I personally like to separate my processing from the display page. Link to comment https://forums.phpfreaks.com/topic/137602-solved-newbie-update-session-refresh-page-with-link/#findComment-719314 Share on other sites More sharing options...
dsoukup Posted December 19, 2008 Author Share Posted December 19, 2008 Mad Mick - Thank you! That solution works perfectly, and it is very simple to adapt to the various needs of my site. Link to comment https://forums.phpfreaks.com/topic/137602-solved-newbie-update-session-refresh-page-with-link/#findComment-719392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.