Save JSON response from MySQL in Local Storage - php

I have a problem with my PhoneGap app. I need to get an ID selected by user from index.html to view.html. For example, user choose a link with name on index.html and then press on this link and the view.html page will display an information about "something". With Ajax I make a request to 1.php which returns me a JSON.
while($obj = mysqli_fetch_object($result_1)) {
$arr[] = $obj;}
echo '{"notes":'.json_encode($arr).'}';
The 1.php script return me the next JSON:
{"notes":[{"ID":"1000043","text":"Test ","dt":"2015-02-04 20:46:22"},{"ID":"1000037","text":"Here we go AGAIN!!","dt":"2015-02-03 07:42:42"},{"ID":"1000035","text":"Teoriya veroyatnosti - 27 ballov? ","dt":"2015-02-02 19:07:33"},{"ID":"1000032","text":"Test again?!","dt":"2015-01-20 12:48:47"}]}
Now if user choose the link with ID-1000043, the app should to display all info about this ID. I try tu use $_SESSION method, but I save only the first ID. After it I try the local storage. My request is:
var url="someurl.com/1.php";
$.getJSON(url,function(json){
$.each(json.notes,function(i,info){
var id = [];
id = info.ID;
localStorage.setItem('test_id',JSON.stringify(id));
On the view.html I have a function which display an ID from local storage... But it also save only the first ID, or an array of IDs'. But my purpose is to get a selected ID and then display an info for user.
Update #1.
At first attempt I user getItem. It only displays the first ID from MySQL. I use:
var store_id = JSON.parse(localStorage["id"]);
Thanks in advance.

Related

Last values are not getting stored in an array in session in codeigniter

I have a page with many like buttons against many images, as I am clicking the like button, the id of the individual image is sent to the controller and in the controller I am storing these values in the session variable in an array.
$this->load->library("session");
$id_image = $this->input->post('ids');
$image_id_session = $this->session->userdata('likes');
if (!is_array($image_id_session)){
$image_id_session = array();
}
$image_id_session[]=$id_image;
$this->session->set_userdata('likes',$image_id_session);
The problem is I am just getting the current image id, all other past ids in the current session are not getting stored in the array.
check print_r($_SESSION); If Value Is Saved Or Not

PHP/MySQL - how to add MySQL data to url in address bar without a form

I have a website at localhost:8888/documents/index.php.
In this file, I load a random row from my MySQL table. I have around 1000 rows, and it will randomly pick one row, and return some data from that row into my page. For example, if it chooses row 467 with:
name = George
age = 23
key = fe4v6
It will show on my index.php something like: hello, George (23 years old). The key is a random, unique variable for each row.
What I want: I want my URL in the address bar to update automatically on refresh with the key attribute. So I want: localhost:8888/documents/index.php?key=fe4v6. When I refresh the page, I want it to update the URL with the new key value for the corresponding row in my MySQL database.
I don't work with forms. I make a connection with my database and I use queries and fetch_assoc() on the random row. In my index.php I show the name like this:
<?php echo $row['name'] ?>
I don't know how easy or difficult this is, maybe there is a much easier solution. I would like to hear from you, thanks!
A quick solution would be to use a header redirect
if(!isset($_GET['UniqueKey'] {
header('location: localhost:8888/documents/index.php?UniqueKey='.$row['name']);
}
The code firsts checks if UniqueKey is set, so it doesn't redirect infinitely.
You can use header as follows. Add this snippet after you have the row value to be passed in as the query string parameter.
<?php header('Location: localhost:8888/documents/index.php?key='.$row['key']); ?>
You could make use of the History API if you only need to support fairly recent browsers.
<script>
var path = "<?php echo "localhost:8888/documents/index.php?key=".$row['key']; ?>";
var page_title = "Custom Title";
history.pushState({}, page_title, path);
document.title = page_title;
</script>
This will update the path to add the key as soon as this code is run in the browser (i.e. during the page load). No reload is required so there's no need to program any logic to prevent a new random row being assigned.

Retrieve saved text from MySQL database

I have a form that saves text into the MySQL database. The user does not need to register to save that form. How do I make it so that when someone submit's a form, it will display their text that THEY typed in a saved. I want it to be able to retrieve they're text that they submitted at that time and possibly give it a link like this: http://www.example.com/text115612 (the numbers are for different texts...) and make the retrieved text display on that page?
When the user submits the text, you can save the text together with an ID, in your example text115612.
After they've submitted, a server side script will redirect the user to the newly created text (Serverside, because the user can't predict what the ID will be).
If you wish to make the texts a little more private, you can make a harder ID so it's not possible for people to guess it.
What you need to do is retrieve the text and create new file
with unique name
$userttext = $textfromdb // get the user text
$file = fopen('text_file_name.txt','w') // open a new file
frwite($file,$textfromdb); // write to the file
fclose($file); // close the file
$link = "http://website.com/"."text_file_name.txt"; // create the website link
When user submits the form, you would do something like this.
//insert your text//
INSERT INTO table_name (text) VALUES ('text');
//get the primary key that was just inserted//
//column must be auto-increment//
$textid = SELECT LAST_INSERT_ID();
//you can also do this directly in PHP//
$textid = $mysqli->insert_id;
echo "http://www.example.com/" . $textid;

PHP multi-step form with neo4j

I am making multi step form for submission of a company information in PHP with neo4j graph database. In first step there is a submission of basic information and in second step , some advanced information for a user to fill.
Then problem is that when I am creating a company node in the first step of the form, it is created successfully but in the next step I am unable to get company id to store Step 2 information of this company. The step 2 form resides in another file.
I am using AJAX form submit method.
I basically need the company name or id that is generated in the first step, in the second step form to store step 2 information of the company.
Adding some code could only be helpful for us.
However, if you what you want to achieve is close to the lastInsertId in PDO/Mysql for e.g., you can achieve it with neo4j too with the RETURN statement that will return you the created node and you can get the id from it, pass it to the second step of your form and retrieve the node with the passed id.
The following code is an example using the PHP Client from Neoxygen https://github.com/neoxygen/neo4j-neoclient :
// Creating your company node
$q = 'CREATE (c:Company {name:"My Awesome Company"}) RETURN c';
$result = $client->sendCypherQuery($q);
$id = $result->getSingleNode()->getId();
Pass now the id to the next step of your form, and load the company from the id :
$id = $_SERVER['POST']['id'];
$q = 'MATCH (c:Company) WHERE id(c) = {company_id} RETURN c';
$params = array('company_id' => $id);
$result = $client->sendCypherQuery($q, $params);
$company = $result->getSingleNode();
// Want to get some info of the node ?
$companyName = $company->getProperty('name');
Hope it helped.
Chris

Update table value on page view?

I am trying to create a PHP trigger for when a user views certain pages on my website it will update the user table in the points section.
I understand the process would work something like this
on page view > update user > where user id is (**get username from session**) > add 5 to points row
Anyone have any idea how to set up something simple like this for giving users simple points for viewing pages?
My site is using PHP and mySQL for the database.
Use cookies or session variables to keep track of the user details like the username or ID. So making a pageview trigger would be as easy as adding a mysql query at the top of every page which would update the database table for views. Kinda the same way that forums operate.
E.g
<?php
session_start();
$db_connection = mysqli_connect('host','username','password','db');
$user_id = $_SESSION['userid']; //That is asssuming that you had gotten the user id on login
mysqli_query($db_connection, 'UPDATE page_views SET views_column=views_column+1 WHERE userid=$user_id');
?>
Yes, you could do something like (if you own the page the user has to visit):
<?php
$pointsForThisSite = 5;
include "points_adder.php";
?>
While Points_adder looks whether $pointsForThisSite is defined and > 0, then adds the Points to the database as you descripbed.
Is that what you are looking for?
Create a php function and call it everytime the user enter the page.
You don't need a mysql trigger because, the action is at the webpage.
function add_points($user, $page){
//If users visits too many maybe you don't want to gave him some points.
//add points
}
and invoke the function in that pages you want to score
The most unobtrusive way to do this is with an AJAX call after the page has loaded. The call should be to an include file that performs the database update operation and returns a 204 response so that the visitor's browser doesn't wait for response content.
For an Apache server;
header('HTTP/1.0 204 No Content');
header('Content-Length: 0', true);
header('Content-Type: text/html', true);
flush();
// do the table update here

Categories