Jump to content

Update Session Variable With Link


dsoukup

Recommended Posts

I apologize ahead of time for my (very likely) easy question. I have searched the forum and elsewhere for an explicit answer, but have not found one, yet.

 

I would like to be able to update a session variable with a link that would essentially reload the same page with new information related to the updated session variable. For example, I have a page that displays the contents of a .txt file. The .txt file to be read is determined by the date. Currently, I only use the current date, but would like the visitor to be able to click NEXT, TOMORROW or some other similar links and then have the page refresh, displaying the new appropriate date-determined text.

 

I thought the best way to do this would be to include a session variable which references the date required. It defaults to the current date, but thought the link could update that to whichever date is required. Then the page would refresh (or whatever) and display the correct text.

 

I also thought about writing the date to a mySQL table which would include the SID and date value, but unsure if that would be any more efficient or better.

 

I'm running into a wall on how to make this work. I can post some code examples that I have tried, but I am afraid that since I am so new to PHP, that the code will make those more advanced than myself go crazy.  :)

 

Any help, code samples, etc...would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/119385-update-session-variable-with-link/
Share on other sites

Im not sure if I#ve picked up the problem correctly so apologies if Im off the mark.

 

Are you saying that you have a php page which takes the contents of a text file and displays it, this text file is named by the date eg 20080812.txt for today (or the next 45 mins of it!), 20080813.txt for tomorrow etc etc

 

If so then would you not just pass the date of the file you want to read in a variable and set up the buttons to do the same?

 

eg

 

<a href="http://www.yourdomain.com/thepagename.php?yourfile=20080812">

 

then where you reference the text file you would append the .txt on the end of yourfile and use that

 

the tomorrow/ next buttons etc would just need to be constructed using the current date and forming tomorrows date from them to then produce:

 

<a href="http://www.yourdomain.com/thepagename.php?yourfile=20080813">

 

 

or am i way out on my understanding?

I do not quite think either of the solutions provided above work with my example.  Here is the main part of the code (site's URL is http://www.officiumdivinum.org/martyrologium_romanum.php):

 

  <?php

    $link = mysql_connect('XXXX', 'XXXX', 'XXXX');
    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());
    }

    $today = GetDate();

    echo "<div id=\"container\">";

      include("http://www.officiumdivinum.org/martyrologium_header.php");

    echo "<div id=\"content\">";


$query = sprintf("SELECT roman_date FROM calendarium_romanum WHERE month=%d AND day_month=%d",
    mysql_real_escape_string($today[mon]),
    mysql_real_escape_string($today[mday]));

// Perform Query
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
  printf ("<h2>%s</h2>", $row["roman_date"]);
}

mysql_free_result($result);


mysql_close($link);





    echo "<br>";
    $handle = @fopen("http://www.officiumdivinum.org/martyrologium/".$today[mon]."/".$today[mday].".txt", "r");
    if ($handle) {
      while (!feof($handle)) {
      $buffer = fgets($handle);
      echo "<p class=\"main\">".$buffer."</p><br>";
      }
      fclose($handle);
    }

    echo "<p class=\"main\">Et alibi aliorum plurimorum sanctorum Martyrum et Confessorum, atque sanctarum Virginum.</p><br>";
    echo "<p class=\"main\"><span class=\"red\">R.</span>  Deo gratias.</p>";

  echo "</div>";
  echo "</div>";
  ?>

The line $handle = @fopen("http://www.officiumdivinum.org/martyrologium/".$today[mon]."/".$today[mday].".txt", "r"); reads a text file, and subsequent lines print the .txt file line-by-line. The file path for today's file, for example, is /martyrologium/8/13.txt. I would like a way of passing any date (most importantly, the days surrounding today), into this directory. Does this clarify matters or am I simply misreading the solutions provided?  I apologize for my lack of understanding ... I have only been using PHP for a couple months now.

I'm pretty sure something like this will do what you want:

if (isset($_GET['p']))
   $addDays = $_GET['p'];
else
   $addDays = 0;
$handle = @fopen("http://www.officiumdivinum.org/martyrologium/".$today[mon]."/".$today[mday]+$addDays.".txt", "r");
echo "<a href=\"?p=".($_GET['p']+1)."\">Next</a>";

 

Of course you will want to consider the end of the month, but the logic makes sense.

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.