p26528
p26528

Reputation: 31

Storing a mysql query

I am new to coding, so please excuse if this is a dumb question. I have a database table

primary_key | main | heading | subheading | subsubheading

and have to return the primary key.

I have a webpage, where users go through each heading as a separate page.

I thought I could query the database based on where the user is, but then I realized I would lose what I was querying (ex. the heading) when the user went to a new page.

Is there anyway to get around this?

Upvotes: 0

Views: 74

Answers (3)

Mas0490
Mas0490

Reputation: 86

try

(your normal data getting query)

and then for page navigation

location.href='newpage.html?primarykey=<?php echo $row[primary_key]?>';"

and then on the new page....

<?php 
$primarykey= $_GET["primary_key"]; 
?>

which will grab it from the url

Upvotes: 1

BobbyTables
BobbyTables

Reputation: 4685

Well, if you want to keep your state between navigations, there are a number of approaches. One could be to set a session (the server keeps track of the state), another could be to use query-strings in the url (the url keeps track of state), and a last one would be to use the browsers localStorage (users browser keeps track).

Depending on your knowledge and requirements all could be used to solve this.

Sessions work like this, and keeps that variable until the user closes the tab or you destroy the session:

<?php
   // Starting session
   session_start();

   // Storing session data
   $_SESSION["whateverVariableName"] = 12;
?>

For the JS approach with localStorage you would just keep track of the current state, and not an entire database. Localstorage could store whatever is the current article index, or scroll position or whatever, write it to localStorage, and whenever the user returns it would be requested again from the server

Upvotes: 1

Asif Uddin
Asif Uddin

Reputation: 409

You can pass the "primary key" on url and can grab that by '_GET' request or you can create session to hold the params so you access it from any where. BTW also you can cache all of your data and store in it so any time you can access any data you want.

Upvotes: 0

Related Questions