I'm writing a script for a lead contact form that needs to send the first 10 leads to email 1, the second 10 leads to email 2, and so on until it gets to email 4, and then it goes back to email 1.
this is a rotator i have that was built for landing pages, but it rotates 1 each time rather than waiting 10 times, and then rotating. how would I modify this to suit my needs?
Also, it cant happen on every 'refresh' obviously. there would need to be a separate group of code which would go in the action="whatever.php" of the form and thats the code that would increment it.
<?php
//these are the email addresses to be rotated
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
//this is the text file, which will be stored in the same directory as this file,
//count.txt needs to be CHMOD to 777, full privileges, to read and write to it.
$myFile = "count.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$email_number = #fread($fh, 5);
#fclose($fh);
//see which landing page is next in line to be shown.
if ($email_number >= count($email_address)) {
$email_number = 1;
} else {
$email_number = $email_number + 1;
}
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $email_number . "\n";
fwrite($fh, $stringData);
fclose($fh);
//include the landing page
echo $email_address[$email_number];
//terminate script
die();
?>
What I understood from your question is there would be form to submit leads and when a lead is submitted, it has to follow your logic. Correct me if I'm wrong.
If that be the case,
use two a text file like track.txt. The initial contents of this text file would be 1,0. Which means leads are sent to first email id 0 times.
So in the action script of the form include the following code.
<?php
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
$myFile = "track.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$track = #fread($fh, 5);
#fclose($fh);
$track = explode(",",$track);
$email = $track[0];
$count = $track[1];
if($count >= 10)
{
$count=0;
if($email >= count($email_address))
{
$email = 1;
}
else
{
$email++;
}
}
else
{
$count++;
}
$track = $email.",".$count;
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $track);
fclose($fh);
//send lead to $email
?>
Related
So I have a JSON file containing basketball player information in the following format:
[{"name":"Lamar Patterson","team":1,"yearsLeft":0,"position":"PG","PPG":17},{"name":"Talib Zanna", "team":1,"yearsLeft":0,"position":"SF","PPG":13.1},....]
I want a user to be a able to add their own custom players to this file. To do this i try the following:
<?php
$json = file_get_contents('json/players.json');
$info = json_decode($json, true);
$info[] = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
file_put_contents('json/players.json', json_encode($info));
?>
This "sort of" works. But when I check the JSON file, I find that there are 3 new entries rather than 1:
{"name":"","team":null,"yearsLeft":4,"position":"","PPG":""},{"name":"","team":"3","yearsLeft":4,"position":"","PPG":""},{"name":"Jeff","team":null,"yearsLeft":4,"position":"C","PPG":"23"}
assuming $name="Jeff" $team=3 and $ppg=23 (populated via POST submission).
What's going on and how can I fix it?
You could try doing the following:
Untested code
<?php
if(!empty($name) && !empty($team) && !empty($position) && !empty($ppg)) {
$fh = fopen('json/players.json', 'r+') or die("can't open file");
$stat = fstat($fh);
ftruncate($fh, $stat['size']-1);//removes last ] char
fclose($fh);
$fh = fopen('json/players.json', 'a');
$info = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
fwrite($fh, ','.json_encode($info).']');
fclose($fh);
}
?>
This will append the only the new json to the file instead of opening the file, making php parse all the json and then writing it to the file again. In addition to that it will only store the data if the variables actually contain data.
Try this:
<?php
//get the posted values
$name = $_POST['name'];
$team = $_POST['team'];
$position = $_POST['position'];
$ppg = $_POST['ppg'];
//verify they're not empty
if(!empty($name) && !empty($team) && !empty($position) && !empty($ppg)) {
//Open the file
$fh = fopen('json/players.json', 'r+') or die("can't open file");
//get file info/stats
$stat = fstat($fh);
//final desired size after trimming the trailing ']'
$size = $stat['size']-1;
//file has contents? then remove the trailing ']'
if($size>0) ftruncate($fh, $size);
//close the current handle
fclose($fh);
// reopen the file for append
$fh = fopen('json/players.json', 'a');
//build your data array
$info = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
//if this is not the first item on file
if($size>0) fwrite($fh, ','.json_encode($info).']'); //append with comma
else fwrite($fh, '['.json_encode($info).']'); //first item on file
fclose($fh);
}
?>
Maybe your php config is not set to convert the post/get variables to global variables. This happened to me a couple of times so I rather create the variables I'm expecting from the post/get request. Also watch out for the page encoding, from personal experience you could be getting empty strings there.
I am writing an "unfriend" script for my mini social network, basically the unfriend action needs to remove their id called: $user from their friend list and my id called: $my_id. I have made the scripts that add user id's to each users files but I am not sure how to delete this?
My friend_list file:
1,2,3 // I am user 1
Their friend_list file:
1,2,3 // They are user 3
I currently use this to add each id into the text file:
if($action === 'accept'){
mysql_query("DELETE FROM `friend_req` WHERE `from`='$user' AND `to`='$my_id'");
$fp = fopen($user_data['friend_list'], 'a');
fwrite($fp, ",".$user."");
fclose($fp);
$their_id = friend_list_from_user_id($user);
$fp = fopen($their_id, 'a');
fwrite($fp, ",".$my_id."");
fclose($fp);
}
But to remove $my_id from their friend_list and their $userid from my friend_list I'm thinking I need to use something like this but it isn't working:
if($action === 'unfriend'){
$fp = fopen($user_data['friend_list'], 'a');
unset($user);
fclose($fp);
$their_id = friend_list_from_user_id($user);
unset($my_id);
fclose($fp);
}
But that doesn't seem to work, what should I do to delete only the specified usernames from the respective file?
Oh, and also, this may/may not be of use:
$myFile = $file;
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
$friend = explode(',', $theData);
for($i=0; $i < count($friend); $i++){
if($i >= 0)
{
if($friend[$i] == $user_id) {
$their_user_id = $user_id;
}
}
To solve your issue just do something like:
//get the contents of the file
$contents = file_get_contents($user_data['friend_list']);
$contents = str_replace(",".$friend_id, "", $contents); //remove the friend's id
//open the file again and rewrite the whole thing with the new contents without that id
$fp = fopen($user_data['friend_list'], 'w+');
fwrite($fp, $contents);
fclose($fp);
I have an issue with writing and reading to text file.
I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:
Write to text file:
$fp = #fopen ("text1.txt", "r");
$fh = #fopen("text2.txt", 'a+');
if ($fp) {
//for each line in file
while(!feof($fp)) {
//push lines into array
$thisline = fgets($fp);
$thisline1 = trim($thisline);
$stringData = $thisline1. "\r\n";
fwrite($fh, $stringData);
fwrite($fh, "test");
}
}
fclose($fp);
fclose($fh);
Read from the written textfile
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
echo rtrim($kw[$i]);
}
But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.
How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.
I'm not sure whether you have issues with the even line numbers or with reading the file back in.
Here is the solution for the even line numbers.
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$myValue = rtrim($kw[$i]);
if(i % 2 == 0)
{
echo $myValue;
}
}
I want to create new .txt files but as this code is always returning false, the ajax success function is not executed.
the all code is:
<?php
$nome = $_POST["nome"];
$datanasc = $_POST["datanasc"];
$genero = $_POST["genero"];
$nat = $_POST["nat"];
$morada = $_POST["morada"];
$mail = $_POST["mail"];
$existe = false;
$myFile = "Users.txt";
$myFile1 = "Current_User.txt";
$fh = fopen($myFile, "r")or die("can't open file");
while (($line_of_text = fgets($fh))) {
$Data = explode(';', $line_of_text);
if($nome == $Data[0] && $datanasc == $Data[1] && $genero == $Data[2] && $nat == $Data[3] && $morada == $Data[4] && $mail == $Data[5]){
$existe = true;
break;
}
}
fclose($fh);
if($existe == true){
$arrayToJs["existe"] = $existe;
}
else{
$arrayToJs["existe"] = $existe;
$fh = fopen($myFile, "a")or die("can't open file");
$stringData = $nome.";".$datanasc.";".$genero.";".$nat.";".$morada.";".$mail.";"."\n";
//print_r($stringData);
fwrite($fh, $stringData);
fclose($fh);
$fh1 = fopen($myFile1, "w")or die("can't open file");
fwrite($fh1, $stringData);
fclose($fh1);
there is the problem in the code cause is returning false and the ajax success function is not executed. . .
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($ffav);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($fcart);
}
}
echo json_encode($arrayToJs);
?>
Thank you all guys!
Use the file pointer ($handle) you created with fclose:
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($handle);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($handle);
}
Otherwise your file will always return PHP error when those files do not exist
Whether or not your AJAX success function gets called has nothing to do with a PHP code's "return value."
Assuming you're using jQuery or one of the other JavaScript frameworks, it has to do with the HTTP response code. Presumably, you're probably encountering a PHP error which is resulting in a 500 response back to the browser. This would end you up in the error handler instead of the success handler.
Have you tried using something like the network inspector in Chrome (or the Net tab in Firebug) to investigate the actual HTTP response?
This code is not returning any value please pass value (true/false) after file created to ajax response.
If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the current PHP directory.
I want to fetch json script and write it to a txt file undecoded, exactly how it was originally. I do have a script that I use that I am modifying but unsure what to commands to use. This script decodes, which is what I want to advoid.
//Get Age
list($bstat,$bage,$bdata) = explode("\t",check_file('./advise/roadsnow.txt',60*2+15));
//Test Age
if ( $bage > $CacheMaxAge ) {
//echo "The if statement evaluated to true so get new file and reset $bage";
$bage="0";
$file = file_get_contents('http://somesite.jsontxt');
$out = (json_decode($file));
$report = wordwrap($out->mainText, 100, "\n");
//$valid = $out->validTo;
//write the data to a text file called roadsnow.txt
$myFile = "./advise/roadsnow.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $report;
fwrite($fh, $stringData);
}
else {
//echo the test evaluated to false; file is not stale so read local cache
//print "we are at the read local cache";
$stringData = file_get_contents("./advise/roadsnow.txt");
}
// if/else is done carry on with processing
//Format file
$data = $stringData
Try this:
// Get JSON Data
$json_data = file_get_contents('http://somesite.jsontxt');
// Write JSON to File
file_put_contents('json_data.txt', $json_data);