Why this PHP code does not run - Code fitter Mail - php

I have code
require_once('verify.php');
$file = 'mail.txt';
$allfile = file($file);
for ($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i];
$dep = 'admin#ngocuong.net';
$kq = verifyEmail($cuong, $dep);
if($kq == '2') {
echo 'OK<br />';
} else {
echo 'Nope<br />';
}
}
echo 'Done!!!';
with $cuong = 'cuong.fl310#gmail.com'; code run. Return is OK.
But i put all my list email into file mail.txt. And run code read line in file txt. $cuong = $allfile[$i] code not run.
I had show
for($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i]
echo $cuong;
}
it print list email in file txt. With $i= 0; $cuong = 'cuong.fl310#gmail.com' But code not run. Return enqual: Nope !!!
Plz help me fix it.
Thanks for readding!!!

Related

<PHP> How to make echo stay on the page permanently with sessions?

I have code, which is supposed to echo if it's true.
I did this with an if, but every time I run the code it will replace the previously echo'd number.
Can I make it so echo doesn't replace the past echo, but add an additional $i every time I run the code?
$i = $_SESSION["priem"];
$i++;
$aantaldelers = 2;
for ($j = 2; $j < $i; $j++) {
if ($i%$j == 0) {
$aantaldelers++;
}
}
if ($aantaldelers == 2) {
echo "$i, ";
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
yes you can do it by adding a string in session same as variable
$i = $_SESSION["priem"];
$i++;
$str = isset($_SESSION["str"]) ? $_SESSION["str"] : ''; //my changes
$aantaldelers = 2;
for ($j=2; $j<$i; $j++)
if ($i%$j == 0)
$aantaldelers++;
if ($aantaldelers == 2) {
$str .= "$i, "; //my changes
echo $str; //my changes
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
$_SESSION["str"] = $str; //my changes

Why isn't this if statement working with item from a array?

Im not sure why this isn't working, can anyone help? Im trying to see if
$lines[$y] is the same as "../../Files/Person/$username/files/$share_person_post" but even if it is, it isn't running through the code.
if (file_exists("../../Files/Person/$username/shared.txt")) {
$lines = file("../../Files/Person/$username/shared.txt");
$size = sizeof($lines);
$size = $size - 1;
$shared_location = "Unknown";
for($y = 0; $y <= $size; $y++) {
echo $lines[$y];
if ($lines[$y] == "../../Files/Person/$username/files/$share_person_post") {
echo "Here";
$z = $y + 1;
$shared_location = $lines[$z];
$fmsg = "<p>This file is already shared at $shared_location</p>";
$ex = "0";
}
}
}
Try using explode, I know for me when I grab text files, they are all one long string. It might be my server or php version, but its worth a shot.
if (file_exists("../../Files/Person/$username/shared.txt"))
{
$data = file("../../Files/Person/$username/shared.txt");
$lines = explode("\n",$data);
$size = count($lines);
$size = $size - 1;
$shared_location = "Unknown";
for($y = 0; $y <= $size; $y++)
{
echo $lines[$y];
if($lines[$y] == "../../Files/Person/$username/files/$share_person_post")
{
echo "Here";
$z = $y + 1;
$shared_location = $lines[$z];
$fmsg = "<p>This file is already shared at
$shared_location</p>";
$ex = "0";
}
}
}

php merging or concatenation strings when assigning a variable

PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1

How to output arrays in a certain way using PHP?

I'm try to print an array where every other value is reassigned, as examples (from this):
17.34502870451717,62.46137370987033
To this:
62.46137370987033,17.34502870451717
That part have I succeeded with, but now I have this structure:
[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]
This is where I get stuck and do not know how to write.
The structure I want looks like this:
[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]
Here is my explode.php (GIST)
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i++) {
echo $wing . $minion[$i+1].",";
if($i%2==1) { echo "]<br />"; }
} echo $minion[0] . $wing;
?>
As I understand it, as long as there's always even pairs it should be as easy as;
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$eol = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
$eol=',<br/>';
}
echo '<br/>';
>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
Try This
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i+=2)
{
echo $kk = $wing . $minion[$i+1].",".$minion[$i]."],<br>";
}
Just a small modification to the given answers
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$str = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
$str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
}
echo rtrim($str,','); // to trim ',' at the end

PHP: Cannot use [] for reading - but it's not [] but [$counter]

in general I think I understand what the error message means. But in my case, it's a riddle I didn't succeed in solving...
$keywords_all = array();
$count = 0;
for ($z = 0; $z < $num_results; $z++)
{
$keywords_array = explode(",", $row['free_keywords']);
for ($i = 0; $i < count($keywords_array); $i++)
{
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all))
{
$count++;
}
else
{
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
}
}
$row = pg_fetch_array($result);
}
So, what's wrong with that one? The error message pops up in the line
$keywords_all[$count] = $keywords_array[$i];
I have no clue, seems to be alright to me. But guess, it's again a tiny, tiny thing I've neglected... Thanks for any hints!
I was not able to reproduce your error message. I did find a bug in your code though (I am assuming that you are putting all your keywords in the $keywords_all array without any duplicates). So you should not increment $count inside your IF but instead update the $keywords_all count. See below:
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all)) {
$count = count($keywords_all);
} else {
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
$count++;
}
You will increment $count after storing a value to your $keywords_all array.
$keywords_all = array();
$count = 0; // what for you neet this var ?
$myRow = 'keyW1,keyW2,keyW3,keyW2';
// for ($z = 0; $z < $num_results; $z++) // there is no variable $num_results at your code so I've replaced it with constant
for ($z = 0; $z < 1; $z++)
{
// $keywords_array = explode(",", $row['free_keywords']);
$keywords_array = explode(",", $myRow);
// for ($i = 0; $i < count($keywords_array); $i++)
foreach ($keywords_array as $keyword)
{
$keyword = strtolower( trim( $keyword ) ); // some syntax sugar would be nice
if ( !in_array( $keyword, $keywords_all) )
{
echo "<br />".$keyword;
$keywords_all[] = $keyword;
}
}
// $row = pg_fetch_array($result);
}
var_dump($keywords_all);
This code would be better for you i think, but if you just want to get rid of duplicated records
array_uniq( array("1", "1", "2", "1") )
would be better solution for you.

Categories