PHP variable is empty in second iteration of loop - php

Why is the PHP variable defined as $real in the code below empty but still set after one iteration of the while loop yet the $entry variable is both set and contains the correct value on every iteration of the loop. Does the calling of a function '' put the resulting variable outside of the scope and if so how can I fix this?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Total </title>
<link type="text/css" rel="stylesheet" href="course.css">
<script src="../jquery.js"></script>
<script src="../jquery-ui.js"></script>
</head>
<body>
<?php
if ($handle = opendir('../uploads'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$real= realpath($entry);
echo "<button class='files accordian'>$entry $real</button>";
$real= realpath($entry);
if(isset($real))
{
echo 'the real varibale is set';
}
else
{
echo 'the real varibale is not set';
}
if(empty($real))
{
echo 'the real varibale is empty';
}
else
{
echo 'the real varibale is not empty';
echo 'and is '. $real;
}
echo "<button class='files accordian'>$entry $real</button>
<div class='panel'>
<p>$real</p>";
}
}
}
?>
</body>
</html>

Please change your code where you are getting realpath like below,
$real= realpath("../uploads/".$entry);
Issue: you are just trying to get the realpath of a file. So, it is looking the file in your current path where you are executing your file. Hence, you need to append the origin too.

Related

how to add a parameter to the url in php?

I'm trying to add the parameter cs with a value of 1 to the url using a form and hidden type input. When I execute this, I have a simple if statement that checks to see if $_GET['cs'] is = to 1, but I get a Notice: Undefined index. I'm a php beginner, any help, explanations, or alternatives is very much appreciated.
echo '<form>';
echo '<input type="hidden" name="cs" value="1">';
echo '</form>';
if($_GET['cs'] == 1) {
echo'working';
}
else {
echo 'not working';
}
Only after form is submitted you will see 'cs' in $_GET array.
The $_GET array contains all parameters on the URL line.
For example if a URL is: https://example.com?param1=1&param2=2
You will get a $_GET array with two entries ('param1' and 'param2') with values 1 and 2.
You should add a check using the isset function (https://www.php.net/manual/en/function.isset.php). Otherwise it will try to access the parameter if it isn't sent. In your case the parameter was probably not sent.
Url in html:
test the get method
The url test
<?php
if ($_GET['varaible'] == true) {
$varaible = 'Its work';
$value = false;
}else{
$varaible = 'Oooppss';
$value = true;
}
echo $varaible . '<br>';
?>
Full code:
<?php
if ($_GET['varaible'] == true) {
$varaible = 'Its work';
$value = false;
}else{
$varaible = 'Oooppss';
$value = true;
}
echo $varaible . '<br>';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>get varaible</title>
</head>
<body>
test the get method
</body>
</html>

Animation on CSV array changes

I have a simple page that displays race results. The results are pulled from a CSV file which updates upon completion of each race. The changes could be subtle so would like to have an animation, fade in/out or colour change etc, only if results are different. I want to add a to the table ONLY if the CSV file has new results as a visual aid to the update.
EDIT: I am unsure where to start on implementation so no code is reflected below. The CSV is scanned every 1 second and table updated. The outcome I want is for the table results to have an animation if/when array results change, else no animation.
EDIT 2: I have attempted to output the current racer number from the results data ($csv[1]) to a file and then check that against the current race number but it doesn't seem to work. When I pull $lastrace[0] and $csv[1] they are always the same number. I thought having it higher in the PHP would get $lastrace before writing the new number but doesn't appear so.
// Get Racer No from previous race
$lastrace = str_getcsv(file_get_contents('lastraceno.txt'));
// Puts current racer no to CSV file
$file = fopen("lastraceno.txt","w");
fputcsv($file,explode(',',$csv[1]));
fclose($file);
My code as it stands is as below - have trimmed some irrelevant code.
index.php
<head>
<script src="jquery-2.1.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('table.php', function(){
setTimeout(refreshTable, 1000);
});
}
</script>
</head>
<body background="timebg.jpg">
<div id="tableHolder"></div>
</body>
table.php
<?php
$f_pointer=fopen("csv.txt","r"); // file pointer
while(! feof($f_pointer)){
$csv=fgetcsv($f_pointer);
foreach($csv as &$val){
if($val === "" || $val === false || $val === null) $val = "NA";
}
}
?>
<table>
<tr>
<td><?php echo $csv[2] ?> <?php echo $csv[3]?></td>
</tr>
</table>
Solved
The racer number is $csv1 in my Array from the results file. I found writing the $csv1 to a new CSV file and comparing $csv1 to $lastrace[0] each refresh to work.
Using an if else statement I compared the info and set a variable depending if there was a match or not.If the data matched the variable is '0' and no action is required. If $csv1 did not match $lastrace[0] the variable is '1' and the new race number sent to the lastraceno.txt for future updates.
I could then set a CSS class based on if the variable was 0 or 1.
CSS animation achieved with [https://daneden.github.io/animate.css/]Animate.CSS1
index.php
<head>
<script src="jquery-2.1.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('table.php', function(){
setTimeout(refreshTable, 1000);
});
}
</script>
</head>
<body background="timebg.jpg">
<div id="tableHolder"></div>
</body>
table.php
<?php
$f_pointer=fopen("csv.txt","r"); // file pointer
while(! feof($f_pointer)){
$csv=fgetcsv($f_pointer);
}
// Replace blank lines with NA
foreach($csv as &$val){
if($val === "" || $val === false || $val === null) $val = "NA";
}
// Get Racer No from previous race
$lastrace = str_getcsv(file_get_contents('lastraceno.txt'));
// Puts current racer no to CSV file
if ($lastrace[0]==="$csv[1]") {
$newrace = 0; // This is not a new race
} else {
$newrace = 1; // This is a new race
$file = fopen("lastraceno.txt","w");
fputcsv($file,explode(',',$csv[1]));
fclose($file);
}
if ($newrace == '1') {
$newtimes = "class='animated zoomIn'";
} else { }
<table <?php echo $newtimes ?>>
<tr>
<td>Text to animate</td>
</tr>
</table.

Why using operator in front of function?

I started learning PHP 1 week ago, i'm making some form tests to get more apprehension on the language, i get stuck in problem small problem for you guys....
I used in_array function from the PHP manuale to check an array off names at login
The function did not work to check the array tell i add !in_array !!!
Normal in_array did not work to check for names ...
So I used blaindly the operator "!" in front of the function, so why did it work with that operator and not without it ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forum</title>
<meta name="Nic" content="" />
<link rel="stylesheet" type="text/css" href="style.css"> </head>
<body>
<?php
if(isset($_POST['submit'])){
$names = array("Nicolaus", "Solo", "Yiu", "Yang", "Darius");
$minimum = 5;
$maximum = 10;
$username = $_POST['username'];
echo '<br>';
$password = $_POST['passwoard'];
if(strlen($username) < $minimum){
echo 'Username needs to be longer then 5 carcaters !';
}
if(strlen($username) > $maximum){
echo "Username can't be longer then 10 carcaters";
}
if(!in_array($username, $names)){
echo "You are not alowed , not in database !";
} else {
echo 'Welcome';
}
echo '<br>';
echo 'Your Username is : ' . "<b>" . $username . '</b>';
echo ' Your Password is : ' . "<b>" . $password . '</b>';
}
?>
<form action="my_from.php" method="post">
<input type="text" name="username" placeholder="username">
<input type="password" name="passwoard" placeholder="password">
<input type="submit" name="submit"> </form>
</body>
Ty
! means NOT, its an operator to make comparisons when you want to check if something is false.
$raining = false;
if (!$raining){
echo "its not raining";
}
Now that you posted your code:
if(!in_array($username, $names)){
echo "You are not allowed , not in database !";
} else {
echo 'Welcome';
}
in_array is a function that checks if some value is inside an array. You are testing if $username is inside $names array.
Your conditional checks if $username is NOT in array, if so, it means that $username is not allowed because it doesn't make part of the permitted $names.
Your ELSE clause identify that it is in the array, and then the user is able to login.
you can invert your logic:
if(in_array($username, $names)){
echo 'Welcome';
} else {
echo "You are not allowed , not in database !";
}
And it will work as well.
! symbol is used for negating the logic of the function.
What you actually want to do is, you want to reject an user with $username, if his/her name is NOT in the $names array. You have got a function to check whether an element is present in an array. So adding ! operator to it negates the logic.
if (!in_array($username, $names))
That means if the $username is NOT present in array $names.
Using ! is similar to checking with condition == FALSE. But we can't directly use that logic on that function so we opted for ! operator.
Another alternative for your example without ! is,
if (in_array($username, $names)) {
echo "Welcome";
} else {
echo "You are not alowed , not in database !";
}
Hope this helps! :)

How to validate php if form is filled

//So i have a php validation external and a html file. I'm trying to validate if //the input boxes are filled out correctly... so far i have one but I can't get it //to run and i tried testing it out doesn't work... do i need to download //something or is my code completely wrong. I just trying to check if its empty and if it has at least 3 characters
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cal5t</title>
</head>
<body>
<?php
$title= $_REQUEST["title"];
if ($title == "" or >3 ) {
echo "<p>5please!</p>";
?>
</body>
</html>
You are probably looking for something like:
if (($title == "") or (strlen($title) < 5))
{
echo "<p>5please!</p>";
}
if(!empty($title= $_POST['title']) && strlen($title) > 5){
echo "valid: $title";
} else {
echo "incorrect title: '$title'";
}
Also, its beter to use $_POST or $_GET over $_REQUEST.
I think you're looking for:
if(strlen($title) < 5){
echo '<p>The title you entered is not long enough.</p>";
}
You can make sure there is a value with the isset() function.
requird validation
$title = trim($_POST['title']); // it will remove stat and end spaces
if(strlen($title) <= 0)
{
echo "Title Field is required";
}

Embedding Javascript in PHP With Brackets

Alright so I've been trying to echo some JavaScript for the fancybox window to popup if a user logs in or out. This is what I have so far, but it keeps throwing a server error.
<link rel="stylesheet" type="text/css" href="scripts/fancybox/jquery.fancybox.css" />
<script type="text/javascript" src="scripts/fancybox/jquery.fancybox.js"></script>
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=0\',
title : \'Successfully Logged In!\'
}
], {
padding : 0
});
</script>';
}
else if (isset($msg['1'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=1\',
title : \'Successfully Logged Out!\'
}
], {
padding : 0
});
</script>';
}
?>
What am I doing wrong?
The problem isn't with the brackets. PHP won't echo out multiple lines of code. In order to do what you're trying to do you need to set up a variable and then echo that out:
$myVar = "";
$myVar .= "<script src='text/javascript'>";
$myVar .= "blah blah blah...";
echo $myVar;
Don't echo out the script, do something like this.
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); { ?>
<script>
//JS
</script>
<?php } ?>
if(isset($msg['0'])); {
Semicolon there? I think not. Besides is message an array? Maybe you wanted to do:
$msg = isset($_GET['msg']) ? $_GET['msg'] : false;
if($msg === '0') {

Categories