WordPress - Update Database Value in real time. - php

I have a piece of code that counts the lines of code within a wordpress site. I have successfully managed to send that number to the wordpress database so I can call on it in a different location in my theme files (this is because the script needs to be in the root of the install to count the lines of code).
Whilst this works really nicely, the BIG issue I have is that the number won't update itself automatically. i.e. the code for counting lines is www.mysite.com/loc.php and I mus go to this page and let it load before it updates the value in the database.
Is there a way to make it so that the value just automatically updates, so I don't have to navigate to the page for this to happen? i.e. when I add more lines of code, the value in the database updates itself.
Code for sending value to the db is as follows:
$num_of_lines = $folder -> count_lines();
update_option('line_count', $num_of_lines);

look into jquery and ajax call
Wordpress provides for you the framework to call backend functions on the frontend
http://wptheming.com/2013/07/simple-ajax-example/
another example:
http://premium.wpmudev.org/blog/how-to-use-ajax-with-php-on-your-wp-site-without-a-plugin/

You need to create a trigger, in order to update automatically MYSQL database
Look at this, i hope it will help
How to automatically update a MYSQL column value using formula and data from other columns in same row?

Related

How to run a function in PHP when MySQL table is modified (e.g adding a row)

I'm trying to make a system where PHP gets information from the newest row, and displays them live on the page. (think of messaging apps) I have everything down except the part where I need to add the information to the page when the table is updated. Can anybody help? (sorry if this isn't clear, this is my first time posting on stackoverflow)

Echo new row when it got added to database

I am trying for a few days now to figure out how I can do this.
I have two pages, one is index.php, other is add.php .
1)Index.php - I want it do display last 5 rows from database table.
2)Add.php - contains form which adds new row to database.
How can I update values that are shown on index.php when I add new row?
So lets say I have index.php already opened on one tab, and on other one is Add.php. When I fill the form on add.php,
I want it to automatically notifies index.php that there is a new row,and that it should change values without refreshing the index.php tab.
Do I need VPS for this?What is the best way to do this theoretically?
There is no need in setting up a VPS for this. A VPS is Just a server in which u can install your own software. You are already using a server for PHP (local perhaps).
U can create a websocket server. Once you add a row you send a message tot the websocket server which will then tell the index page to resfresh.
Previous option is a lot of work for a rather simple functionaliteit. I'd suggest the simplest option as recommended by FrankerZ. Only if you refresh the page at a set interval using JavaScript (window.setInterval) then the page would keep refreshing. I don' t think that's ideal. You van however send a xhtmlhttprequest to store the contents for the index page and run that request every x seconds. Then u can compare the 2 contents and decide if the page should be refreshed or not.

Added something to the database but not reflecting in wordpress page

there is this wordpress plugin, the main functions of that plugin is to take data from the user and write that data to the database, it also Update data and Delete data, then display the changes to the page that the plugin provides.
Now, I want to add thousands of items, but I cannot manually add each items using the plug-ins interface because that would take so long, so what I did is I head over to phpmyadmin locate the tables of the plug-in and I use sql Insert to add data to the database, after trial and error, I SUCCESSFULLY inserted all of the data in all of the tables that is relevant to the plug-in, no errors!.. I analyzed it carefully. but when I head over to the page, and to the live site, the items that I inserted is not showing up. not even one. the items that is only showing is the items that I added using the plug-ins interface, what should I do?
Good job by the way, but make sure that
1. You're inserting data to the correct schematic & table.
2. You didn't insert a null or invalid data (like floats in int data type)
3. try refreshing the page if that's a cache problem, clear the cache and try again

Taking a value from a mysql table and adding 1

This is my first time posting in stackoverflow so please tell me if I need to add more information!
I am attempting to take a value from the mysql database and add 1 to it when the page loads. The user clicks on the link on the first page and when the linked page is loading and the php code below runs it adds 1 to the value of the column Clickcount in the entry with the Name value TheButton in the table "lots".
The following code worked when I ran it through Dreamweaver with WAMP:
<?php
$con=mysqli_connect("localhost","root","qwerty71","Lots");
mysqli_query($con,"UPDATE Lots SET Clickcount=Clickcount+1
WHERE Name='TheButton'");
$result=mysqli_query($con,"SELECT * FROM Lots where Name='TheButton'");
while($row= mysqli_fetch_array($result))
{
echo $row['Clickcount'];
}
mysqli_close($con);
?>
But when I put the same code into my website (only changing the values after the mysqli_connect to connect to the database) it doesn't work. The echo at the bottom is to show the value after it is changed, I have the same echo on the first page to show the value before it is edited by the second page. I set the value to zero in the database before I loaded the first page or clicked the link. The first page shows the value at zero, and after clicking the link the second page changes the value and displays a 1. When I try to click back to the first page it shows a 1 instead of a zero, but then both pages continue to display 1s instead of adding 1 every time the link is clicked.
I have tried multiple ways of changing the code after the SET Clickcount=
Including attempting to add a variable before query that I set equal to the Clickcount value and trying to add 1 to that.
Is there an error in my code that would prevent it from working properly, or is there an alternate way to set this up to get the desired result?
Re-explanation for clarity:
My table is as follows:
Name Clickcount
TheButton 0
I am trying to increase the value under "clickcount" by 1 every time a page is loaded. My code (posted above) functioned properly when run on a local server, I changed the parameters to match my server online.
The homepage has a portion of the code above to show the value of the Clickcount column and a link to the second page, where the code above is located.
The code functioned once, changing the value from zero to one, but then ceased to work on consecutive attempts.
Is there an error in my code? Or is there a better way to accomplish the task of increasing the value?
Further update:
The code seems to be working once every 5-10 minutes. I have left it as it is above, and it was not doing this before. I do not know what changed.
In addition, every few times that it does work, I get the result of receiving two different values when calling the following portion of the code on two separate pages:
$result=mysqli_query($con,"SELECT * FROM Lots where Name='TheButton'");
while($row= mysqli_fetch_array($result))
{
echo $row['Clickcount'];
}
mysqli_close($con);
This code is on the homepage, where it sometimes results in a number one less than what the value is recorded to be in the mysql table. Where on the page with the complete code listed above, it always shows the same value as in the table.
In my continuing effort to figure this out, I have removed all of the files off of the server where my site is hosted, and started again from scratch. After re-writing all of the code for the two pages, creating a new table in a new database, and implementing the code above on the second page, the error remains.
I have also just now signed up for a new hosting service to see if it was the server that was causing the problem. Sadly, that was not it. Does anyone have a suggestion for a solution?
Look to this points:
Copy your query and put in the database. It works?
In the end of your update line, put or die(mysqli_error($con)). If show errors, look to error;
There value 'TheButton' in the column Name in your remote table?
Try change in the clause where in update query, Name='TheButton' to Name LIKE 'TheButton'
Always throw messages errors in your codes. Is the best way to find mistakes. ;)
The page was loading a cached version instead of loading from the site itself. I added some coding to the header to remove the caching, this fixed the problem.

Opencart custom database lookup and display

Currently, trying to get this to work with Opencart, but I think my knowledge of the file structure needs some help.
I have added an additional field into the DB for Opencart, which I added to provide data based on the person logging in with an affiliate code.
After I query that file, I would like the fields echo'ed in the header.tpl output to the screen for every page.
If I query the database in the Index.php file, for the tracking number in the DB, it returns the right information the first time and displays it in the header, but then when I click on the products, the next screen there will be an error, because it looks like the Index.php file is constantly being re-run for every page on Opencart, even when the URL link changes ? Is there another file I am missing which I should put the query into, and also where do I place variables to display on each header file.
Thanks in advance.
It's hard to know what exactly you are wanting to do based on your question. If you are wanting to display affiliate data in the header, you should be using the file /catalog/controller/common/header.php to assign the data to the header view. You should then be adding the data in the /catalog/view/theme/your-theme-name/template/common/header.tpl file.
Really speaking, you should be using
if($this->affiliate->isLogged()) {
// Code here
}
To make sure that the affiliate is actually logged in before showing the data. To pull the data from the database, you are probably going to want to edit /system/library/affiliate.php and pull the data that way rather than in a controller file, so you can get any of the information across the store files

Categories