Essentially what I'm trying to achieve is, if the user hasn't selected one of the dropdown options, then a default .txt is opened, read and displayed. When the user does decide on a drop down option, it echos the users option. It works by getting the value of the option and setting that to a variable to set the open path.
Instead of opening my default which is 'example' when the user hasnt selected, it seems to pick one of the values at random and select it.
If the user hasnt selected an option, the default file path is p-changelog/example.txt
If the user has chosen and option, the file path becomes p-changelog/$userOp.txt (UserOp being the users option from the drop down).
if(!isset($_POST['userDateOp'])) {
$userOp = "example";
}else{
$userOp = $_POST['userDateOp'];
}
if(!isset($_POST['submit'])) {
$changelog = fopen("p-changelog/$userOp.txt", 'r');
$pageTxt = fread($changelog, 25000);
echo nl2br($pageTxt);
}
Entire Page: Code
I am new to PHP before criticising anything I've done.
userDateOp is always going to be set, as its posted every time.
What you are looking for is its value being empty
That function is
if(empty($_POST['userDateOp'])) {
You don't need to check if submit is set because you've already validated the existence of userDateOp. So this will only lead to an undefined variable notice for $changelog for default.
Your simplified script should be
if(!isset($_POST['userDateOp'])) {
$userOp = "example";
}else{
$userOp = $_POST['userDateOp'];
}
$changelog = fopen("p-changelog/$userOp.txt", 'r');
$pageTxt = fread($changelog, 25000);
echo nl2br($pageTxt);
Related
I have a form on every page that is a quick navigation. Based on the value of the drop-downs it will redirect to a specific page. The URL after using it will look like /page?first=abc&second=def&third=ghi. If you leave the page and go to pageB the url will just say /pageB. I'm trying to create a SESSION for second if it's set and when it's no longer set (navigating to a different page not using the quicknav) still store the value when it was set. Here is what I have so far...
function storeVariable {
global $saved_second
$store_second = $_GET['second'];
$saved_second ='';
if (isset($store_second)) {
$_SESSION['second'] = $_GET['second'];
$sessionsecond = $_SESSION['second'];
$saved_second = $sessionsecond;
}
return $save_second
}
In the header I have
global $saved_second;
echo $saved_second;
The above code is fine for the initial page the quick navigation goes to. It shows the value of $saved_second in the header. I'm not sure how I would say
if (isset($saved_second) && (!isset($stored_second) {
USE SESSION THAT WAS CREATED WHEN $stored_second WAS SET
I thought something like...
if ($store_second != $sessionsecond){
but that doesn't work either because the session was created in an IF statement.
Then I tried...
IF (!isset($saved_second)) {
IF (isset($_GET['second'])) {
$_SESSION['second'] = $_GET['second'];
$saved_second = $_SESSION['second'];
}}
Any suggestions?
i want to display 5 php files content randomly but non repeating when clicked a button [NEXT] using php/javascript (that works in php desktop as well).
the code i used did displayed random web page on page load but i did came across repeated web page
this is the code i used for index.php file:
<?php
$RandomList = array();
$RandomList[] = "/review/review-a1.php";
$RandomList[] = "/review/review-a2.php";
$RandomList[] = "/review/review-a3.php";
$RandomList[] = "/review/review-a4.php";
readfile($_SERVER['DOCUMENT_ROOT'].$RandomList[rand(0,count($RandomList)-1)]);
?>
please suggest how to get non repeated files .
Just save paths you already used in the session:
//Read visited paths from session or create a new list.
//?? works only in PHP7+. Use isset()?: instead of ?? for previous versions
$visitedPaths = $_SESSION['visitedPaths'] ?? [];
//Your list, just slightly changed syntax. Same thing
$randomList = [
"/review/review-a1.php",
"/review/review-a2.php",
"/review/review-a3.php",
"/review/review-a4.php"
];
//Remove all paths that were already visited from the randomList
$randomList = array_diff($randomList, $visitedPaths);
//You need to check now if there are paths left
if (!empty($randomList)) {
//The user did not load all files, so we can show him the next one
//Use array_rand() rather than $array[rand(0, count($array) -1)]
$randomPath = $randomList[array_rand($randomList)];
readfile($_SERVER['DOCUMENT_ROOT'] . $randomPath);
//Now we need to save, that the user loaded this file
$visitedPaths[] = $randomPath;
//And we need to save the new list in the session
$_SESSION['visitedPaths'] = $visitedPaths;
} else {
//TODO: Add some logic in case all paths have been visited
}
I want the Code below to read individual line of text from dataFile.txt and show it in input field.
Problem is After reading first line from text document it shows all remaining lines of text from text file into input field. But on clicking submit it should show second line only then again on submitting it should show third line only, inside input field. please help.
<?php
$file = __DIR__."/dataFile.txt";
$f = fopen($file, "r");
$array1 = array();
<form action="datagGet.php" method="get">
<input type="text" value="
<?php while ( $line = fgets($f, 100) )
{
$nl = mb_strtolower($line);
echo $nl;
if(isset($_GET['done']))
{
$nl++;
}
else
{
break;
}
}
?>"
name="someText">
<input type="submit" name="done" >
</form>
You have several problems with you code. And the first comment above points to many of the. Key is the fact that the $_GET['done'] is set for the form submit and therefore you will echo all the lines of the output. It never breaks.
Also there is the fact that you are opening the file for reading each submit of the form. Although I don't see a simple way around this unless you store the file contents between requests.
One possible option is to use 'file()' to read the entire contents into an array. And then use sessions to store which line has been read. Then on each submit, look for the index of the array from the session read; advance it by one read the file again and return that line. Wow wasteful. But okay for simple site.
so use file to get the lines in an array.
output the first line into the value.
store the next index to be read in the $_SESSION variable like $_SESSION['next_line'] = 1
then upon further submissions. read it all back in. look up the 'next_line', and output that line.
so, for example
$array = file('your file name');
$output = $array[0];
if (isset($_SESSION['next_line']))
$_SESSION['next_line'] = intval($_SESSION['next_line']) + 1;
else
$_SESSION['next_line'] = 1;//prime the pump
echo the form with $output
then rinse and repeat. e.g. read, get output (next_line) with file, set $_session = next_line + 1; render output in form.
ps. some extra notes
* of course you'll need to start session on each request.
* you'll need to check if the $_SESSION['next_line'] is set. if not, set it to 1 (prime it)
I have a php file where I am using it to setup dynamically generated pages based on the input variables. It starts on and index.html page where the variables are gathered some of which are not simple strings but complex Google Earth objects. On the submit of that page it is posted to another page and you are redirected to the created file. The trouble is coming when I try to use that variable within the php include file that is used to generate the pages.How do i properly get a variable from this form and then pass it through to be able to use it on the new generated page. Here is what I am trying currently.
On the click of this button the variable flyto1view is set.
$("#flyto1").click(function(){
if (!flyto1view){
flyto1view = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
$("#flyto1view1").val(flyto1view)
}
else {
ge.getView().setAbstractView(flyto1view);
}
});
Then from here I have tried setting the value to an hidden field but Im not sure if that kinda of variable has a value that can be set like that. Whats the best way to get this variable to here after post
<?
if (isset($_POST['submit']) && $_POST['submit']=="Submit" && !empty($_POST['address'])) {//if submit button clicked and name field is not empty
$flyto1view1 = $_POST['flyto1'];
$address = $_POST['address']; //the entered name
$l = $address{0}; // the first letter of the name
// Create the subdirectory:
// this creates the subdirectory, $l, if it does not already exists
// Note: this subdirectory is created in current directory that this php file is in.
if(!file_exists($l))
{
mkdir($l);
}
// End create directory
// Create the file:
$fileName = dirname(__FILE__)."/$address.html"; // names the file $name
$fh = fopen($fileName, 'w') or die("can't open file");
// The html code:
// this will outpout: My name is (address) !
$str = "
<? php include ('template.php') ?>
";
fwrite($fh, $str);
fclose($fh);
// End create file
echo "Congradualations!<br />
The file has been created.
Go to it by clicking here.";
die();
}
// The form:
?>
Firstly. creating files from user input is pretty risky. Maybe this is only an abstract of your code but doing a mkdir from the first letter of the input without checking that the first letter is actually a letter and not a dot, slash, or other character isn't good practice.
Anyway, on to your question. I would probably use $_GET variables to pass to the second file. So in the second file you use <?php $_GET['foo'] ?> and on the first file you do:
echo "Congradualations!<br />
The file has been created.
Go to it by clicking here.";
You could also echo the variable into your template like so:
$str = '
<?php
$var = \'' . $flyto1view1 . '\';
include (\'template.php\')
?>';
I need to run a save/update query but only update those fields that aren't empty. My form contains a FILE field and if I don't upload any file then when the save() is executed this field goes blank even if previous data is presented so how can I save/updated but onlye the fields with content to update?
Cheers and thanks in advance
In your model you need to previously test if the data is empty or not and unset it in this case.
if ($this->data['file'] == "") {
unset($this->data['file']);
}
You always can add a condition which checks if the user uploads a file or not.
If not , select the current value in the database for that file and update it with the old value.
For example:
$update['description'] = $_POST['description']; //just another field
if($_POST['file']['name'] == "") //check if there's a new file
$update['filename'] = $this->book->file_name; //current file name
else
$update['filename'] = $_POST['file']['name']; //new file name in case the user uploaded a new file
$this->book->update($update); //update no matter what