I'm very new to the idea of adding values to a database, so would like some advise on whether this is possible and how I would do it.
I have a code that counts the lines of code written in a project, the code has to be in the folder that it is counting the lines of code from however I want to display this number on my site elsewhere. My site is a wordpress one, so i thought of sending this number to the database and calling on it in a template file elsewhere.
Is there a way to send this number to my database and have it keep up to date in real-time so that I can call it from other places in my site?
My LOC script can be seen running here:
http://www.skizzar.com/wp-content/loc.php
and the number is displayed using the command:
$folder -> count_lines()
update
I've added the following code to my loc.php
$num_of_lines = $folder -> count_lines();
add_option('line_count', $num_of_lines);
And called it in my theme files using
echo get_option('line_count', '0');
So far this just returns 0 as the value and causes a fatal error on loc.php "call to undefined function..."
Second update
Got the code to work by adding
require_once('wp-config.php');
To the top of my code, the issue now is that it doesn't update in real time - is there a way to do this?
Assuming you are saving the value to the database with wordpress as well, use add_option and get_option. Something like this:
add_option('line_count', $num_of_lines);
And in your template, display it with:
echo get_option('line_count', '0');
Related
To put my question into context, I'm working on an entirely static website where 'post' pages are created by myself manually - there's no CMS behind it. Each page will require a <pre> <code> block to display code as text in a styled block. This could be very few - several which is why I'm trying to do this for ease.
Here's what I've done -
function outputCode($code) {
return "<pre class='preBlock'><code class='codeBlock'>".htmlentities($code)."</code></pre>";
}
The code works as expected and produces an expected outcome when it's able to grab code. My idea is to somehow wrap the code for the code block with this function and echo it out for the effect, fewer lines and better readability.
As I'm literally just creating pages as they're needed, is there even a way to create the needed code blocks with such function to avoid having to manually repeat all the code for each code block? Cheers!
EDIT:
I was previously using this function and it was working great as I was pulling code from .txt documents in a directory and storing the code for code blocks in a variable with file_get_contents(). However, now, I'm trying to get the function to work by manually inputting the code into the function.
Well. Wrapping the function input in ' ' completely slipped my mind! It works just fine now!
If I understand correctly, you want to re-use your outputCode function in several different PHP files, corresponding to posts. If yes, you could put this 1 function in its own file, called outputcode.php for example, and then do
include "outputcode.php";
in every post/PHP file that needs to re-use this function. This will pull in the code, from the one common/shared file, for use in each post/PHP file that needs it. Or maybe I'm misreading your last paragraph :(
Ive got some code that runs html code from a database in order to not have to make a new file for every page that I might want to make, problem with this is that if the page contains php code it wont run, and im pretty sure you can do this with eval however it has security risks so I was trying to find some alternatives. I can paste the code if necessary. Ive got a php script that gets data from table and a main php script that gets the formatting in HTML and PHP for the data however it will just run the PHP code as if it were strings from the database.
Here is an image of the code thats meant to be run from the php script :
Here is the php code in that row
And this is the main script that is meant to run it :
Why not make PHP functions? Like, for example if you wanted to spit out data from your databases using PHP, but not a lot of code, you can do something like... (for user profiles)
Like, you can make a funcs.php
then inside of it do:
function user_page($user_id){
?> [Create user page here] <?php
}
Then inside of any other file just do:
include 'funcs.php';
if(isset($_GET['username']) === true){
user_page($_GET['username']);
} else {
//if not loading user page then do something else
}
EDIT
Okay, I just saw your screenshot.
To do something like that, a function would be good.
Ex:
function print_data($info1,$info2,$info3,$info4){
echo("<center>$info1</center><br>$info2<br>$info3<br>$info4");
}
then just calling the function with the $row[] information you have in your screenshot.
Like so:
print_data($row['email'],$row['name'],$row['username'],$row['ip_addr']);
this question must be added to some "doing it wrong" list, honestly.
but, returning to your question you have 3 options, all of them more or less painful, and only one of them is right.
to do it right: rewrite your engine or take some cms/framework. store text data in database, scripts/template on disk
eval! (which you don't want)
parse. (it will be very hard and slow and totally crazy)
I'm trying to deal with Excel sheets in PHP and the following code causes irritating problems.
$getOrderFile = $this->db->query("SELECT order_file FROM orders WHERE id='".$order."'")->fetch_assoc();
$excelReader = PHPExcel_IOFactory::createReaderForFile('xls/'.$getOrderFile["order_file"]);
$excelReader->setReadDataOnly(true);
$Excel = $excelReader->load('xls/'.$getOrderFile["order_file"]);
return $Excel->setActiveSheetIndex(0)->getHighestRow();
Now, I checked the script with two variants of $getOrderFile["order_file"]:
1. excel1.xlxs
2. excel1.xls
When $getOrderFile["order_file"] equals excel1.xlxs - the page goes completely blank. There's no content displayed. And should there be any additional text in HTML - it isn't displayed. As if there was "exit;" somewhere but isn't.
When $getOrderFile["order_file"] equals excel1.xls - the pages goes "missing". I get "The webpage is not accessible" (404).
Can somebody tell me what is wrong? I just want to get the rowcount and... well. The whole system goes nuts.
Thanks for your help!
My other question about getting help with the programming side of wordpress was labeled off topic for some reason so I'm asking a different way. I'm trying to embed my wordpress posts. I'm using this tutorial:
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
The problem is with this code:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('./wordpress/wp-load.php');
query_posts('showposts=1');
?>`
When I try to run the page that this is inserted into, I get the error saying that file doesn't exist. Pretending my domain is blah.com, the file is in www.blah.com/wordpress/wp-load.php and the page that includes this PHP code is in www.blah.com/other/page.php.
How do I change the syntax of the link on the require line to make sure it's pointing to the right place since right now it doesn't seem to be working?
have you tried with
require('../wordpress/wp-load.php');
or anyway something like
require('../../wordpress/wp-load.php');
?
(depending on the depth of your file position)
Just like the above answer
This worked for me
require('../wordpress/wp-load.php');
I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)
I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?
Example, here is a function inside the functions.php
function randomPollNumber() {
///this gets a random active poll number
$sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
$result12 = mysql_query($sql12);
$myrow12 = mysql_fetch_row($result12);
$pollquestionid = $myrow12[0];
return $pollquestionid;
}
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Please help?
Very strange - some debugging tips:
Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?