Lines from a file as variables - php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>squared.php</title>
<link href="/library/skin/tool_base.css" type="text/css" rel="stylesheet" media="all" />
<link href="/library/skin/durham/tool.css" type="text/css" rel="stylesheet" media="all" />
<script type="text/javascript" src="/library/js/headscripts.js"></script>
<style>body { padding: 5px !important; }</style>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>Squared</title>
<link rel ="stylesheet" type="text/css" href="sample.css">
<h1> Squared </h1>
</head>
<?php
//Save the three values from the file
$beginningNum = $_POST['beginningNum'];
$endingNum = $_POST['endingNum'];
$incrementer = $_POST['incrementer'];
//Open the file and read the numbers
$dataFilePointer = fopen("output.txt", "r");
$beginningNum = fgets($dataFilePointer);
$endingNum = fgets($dataFilePointer);
$incrementer = fgets($dataFilePointer);
fclose($dataFilePointer);
// Loop through beginning with beginningNum until you get to endingNum and increment by incrementer
// The first statement says "Start x at the entered beginningNum
// The second statement says "Loop until $x gets to the endingNum value
// The third statement says "Increment $x by the incrementer after every loop (also known as counter)
// Check to make sure each value is a numeric then do the for loop, if not - print an error message
if (is_numeric($beginningNum) and is_numeric($endingNum) and is_numeric($incrementer)) {
for ($x = $beginningNum; $x <= $endingNum; $x += $incrementer) {
//Square $x for every increment of it using pow
$squaredNum = pow($x, 2);
// Print $squaredNum to the user
print("<p> The Final Results are $squaredNum.</p>");
}
} else {
print("<p>Error.</p>");
print("Return to Form.");
}
?>
This is what I have so far. I want to write a squared.php file that receives a filename from squared.html. It should read three values from that file:
The first line in the file will contain the starting value
The second line in the file will contain the ending value
The third line in the file will contain the value by which the counter is updated (often increments, but not always)
** Wait for instruction on Wednesday about this:
Read these 3 values, and confirm that they are numeric values using the `php` function `is_numeric()`.
If not, display an error message and offer the user a link to return to the `HTML`.
If it is valid input, your code should contain the following logic:
Use a for loop that will use the start, ending, and update values to display the those values squared.
For example, if a user enters: (I will test your code with other values)
Start Value: 5
End Value: 20
Increment: 5
Your results should be:
5 squared is 25
10 squared is 100
15 squared is 225
20 squared is 400

Related

how to make an operation with php and two csv files using same ID

I'm new to php and I'm looking to do something with two different csv files with php.
here are screenshots of my two csv files :
https://www.noelshack.com/2019-25-4-1561015547-csv1.png
https://www.noelshack.com/2019-25-4-1561015547-csv2.png
In my two csv I have similar ID and different prices ("Prix HT" column).
What I want to do is check similar ID, and do an operation with the price.
For example in CSV1 for the ID 96885 I have the price of 40.
in CSV2 for the same ID I have two prices : 45 and 48.
What I want in php is a way to create another column that calculate the difference of the price of the csv2 file according to the csv1 file.
In example above the code should result 5 and 8.
I'm planning to do such a thing on massive CSV files.
I only figure how to make the csv files appear in an array with php using the fgetcsv function but I don't know how to do next.
here is the code I have so far, not so much because I don't know how to do next :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TITLE</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<?php
set_time_limit (0);
$testcsv = fopen('test.csv', 'a+');
$testcsv2 = fopen("test2.csv", "a+");
$fichierFinal = fopen('final.csv', 'w');
while($row=fgetcsv($testcsv, 99999,';')){
$ID_product = $row[0];
$price_product = $row[3];
while($row1=fgetcsv($testcsv2, 99999,';')){
$ID_combination = $row1[0];
$price_combination = intval($row1[3]);
if($ID_product == $ID_combination ){
$fprice = $price_combination - $price_product;
$row1[3]=$fprice;
}
fputcsv($fichierFinal, $row1, ";");
};
}
?>
</body>
</html>
I tried the script. Pay attention, I use two CSV files with just one column each:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TITLE</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<?php
set_time_limit (0);
$row1= array();
$row =array();
$rowFinal = array();
$testcsv = fopen('file1.csv', 'a+');
$fichierFinal = fopen('totale.csv', 'w');
while($row=fgetcsv($testcsv, 99999,';')){
$ID_product = intval($row[0]);
$testcsv2 = fopen('file2.csv', 'a+');
while($row1=fgetcsv($testcsv2, 99999,';')){
$ID_combination = intval($row1[0]);
if($ID_product == $ID_combination ){
$fprice = $ID_product + $ID_combination;
//echo $fprice;
$rowFinal[]= $fprice;
echo $fprice . " ";
}
}
fclose($testcsv2);
}
fputcsv($fichierFinal, $rowFinal, ";");
?>
</body>
</html>
Let me know.

Automatically insert certain special characters at specific locations in a string using php codeigniter

I have a text field to enter a code, and it structure is like 200-00-0000.
When I enter the code in text field I have to generate the hyphen symbol(-) while typing the code at the specific locations as shown in the example code.
I'am using the PHP codeigniter.
You can use the following code:
<?php
$data = '200000000';
echo "".substr($data, 0, 3)."-".substr($data, 3, 3)."-".substr($data,6); // 200-000-0000
?>
check the DEMO here.
In case if you wants to create phone number format then you can use this as follows:
<?php
$data = '2000000000';
echo "(".substr($data, 0, 3).") ".substr($data, 3, 3)."-".substr($data,6); // (200) 000-0000
?>
DEMO for phone number format
Now; In case if you wants to go with some js code then you can use that as follows:
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.1.62/jquery.inputmask.bundle.js"></script>
</head>
<body>
<input type='text' id='customtext' name='customtext' />
</body>
</html>
JS CODE:
$(window).load(function()
{
var phones = [{ "mask": "###-###-####"}, { "mask": "(###) ###-##############"}];
$('#customtext').inputmask({
mask: phones,
greedy: false,
definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
});
JS DEMO LINK

Including PHP script to change external CSS depending on month

I know you can change the CSS by changing the stylesheet files themselves and adding a .php extension but I need to find a way of using the PHP script on the index.php page to tell the header which stylesheet to put in to action depending on what month of the year it is.
My code at the moment is accurate to the best of my knowledge but for some reason it isn't being read in and implemented with any of the stylesheets.
CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$date = getdate();
$month = $date["mon"];
$style = "style.css";
$autumn = "autumn.css";
$xmas = "christmas.css";
if($month == 10)
{
echo '<link rel="stylesheet" type="text/css" href="$autumn"/>';
}
elseif($month =="12")
{
echo '<link rel="stylesheet" type="text/css" href="$xmas"/>';
}
else
{
echo '<link rel="stylesheet" type="text/css" href="$style"/>';
}
?>
</head>
Any help would be much appreciated!
You're trying to use variables inside single quotation marks ('). That does not work, variables are only interpreted inside double quotation marks ("). Therefore, your code most proabably literally outputs
<link rel="stylesheet" type="text/css" href="$autumn"/>
The necessary code line would be (adapt accordingly for the other lines, of course):
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$autumn\"/>";
Alternatively, you could replace the escaped double quotation marks inside the HTML code with single quotation marks. Browsers don't really care about that (I'm not sure about the specification).
If you want to avoid some redundancy in your code, you could try an approach like this instead:
<?php
$style = "style"; // Set the default stylesheet name
// then check for the special cases
$date = getdate();
if ($date["mon"] == 10) $style = 'autumn';
if ($date["mon"] == 12) $style = 'christmas';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="<?php echo $style; ?>.css"/>
</head>
This may also be easier to maintain than echoing out a bunch of HTML.
If you're using a HTML5 doctype you don't need the type attribute on your link element. I thought I'd throw this approach into the pile:
switch( $month ) {
case 10:
$file = 'autumn.css';
break;
case 12:
$file = 'christmas.css';
break;
default:
$file = 'style.css';
}
<link rel="stylesheet" href="<?= $file ?>">
I think you need to try:
echo '<link rel=stylesheet" type="text/css" href="'.$style.'"/>';

Having trouble implementing random background images

I'd like to get this random background image coding to work: http://css-tricks.com/snippets/php/randomize-background-image/
I've double checked file names, paths, all the code and the instructions. I think there's something I don't understand.
You can see the page here:
http://agentboris.com/newwebsite/index.html
Thanks for your help.
Your file at http://agentboris.com/newwebsite/index.html is in .html, it should be in .php for you be able to use php code from tutorial.
Try with this simple code (don't forget to save your index file as php extension):
<?php
$bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<!DOCTYPE html>
<html>
<head>
<title>Tests purposes</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
</body>
</html>

Issue with Cookie Values being read correctly - php

Am trying to set the path for an image to be changed according to a php variable that gets determined by reading a cookie for window.innerWidth ( which is set in the head of the file)
The problem is that the cookie is not being read correctly on the first render by the browser, i have to refresh the browser x 2 to get the correct image.
Could anybody point me in the right direction please ? is there a method in php to get the cookie to always be read correctly the first time - at present it looks like it is being cached, or something like that.
Thank you
Example here : http://markaprice.co.uk/2012dev/r-img-set/image-test-ht.php
html below:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>
<link rel="stylesheet" type="text/css" href="css/img-styles.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<?php require_once('deliver-images.php'); ?>
<img class="ri" src="<?php echo $imgPath ?>the-landscape.jpg" alt="the landscape">
<img class="ri" src="<?php echo $imgPath ?>the-landscape-b.jpg" alt="the landscape">
php script(deliver-images.php) is below:
<?php
$device_width = 0;
$imgPath='';
// Read the device viewport dimensions
if (isset($_COOKIE['device_dimensions'])) {
$device_width = $_COOKIE['device_dimensions'];
echo($device_width);
}
if ($device_width > 0) {
// Low resolution image
if ($device_width <= 480) {
$imgPath = 'images/mobile/';
}
// Medium resolution image
else if ($device_width <= 1024 && $device_width > 480) {
$imgPath = 'images/tablet/';
}
else {
$imgPath = 'images/desktop/';
}
} else {
$imgPath = 'images/desktop/';
}
?>
This following line of yours sets the cookie.
<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>
And this php file is where you check this cookie to make the decision about from which folder you are going to serve images.
<?php require_once('deliver-images.php'); ?>
Looking at your code it seems like you are thinking it that way.
First your script tag run and cookie got set
And then your PHP who use that cookie set in previous step to do some precessing
But that's wrong PHP is processed first on the server and then that page is sent to browser and your script runs.
Conclusion : First time when you open the page your cookies were not set because the script will run after the php code runs.

Categories