PHP Excel Reader 2 read can't read column if not filled - php

I've got an excel file like this: Excel example
I want to read the columns F, G ,H ,J when I do:
for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
error_log(
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][6]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][7]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][8]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][9]));
$count++;
}
I have the problem that I get errors:
PHP Notice: Undefined offset: 7 PHP Notice: Undefined offset: 8
PHP Notice: Undefined offset: 9
the F column is always full, but as you can see I've got problems with the others. How can I escape this error, so the field only gets checked if it's filled with data?

for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
$err=array;
for($j=6; $j<=9; $j++){
if(isset(FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j])))
$err[]=FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j]) ;
}
if(!empty($err){
$msg=implode('-',$err);
error_log($msg);
}
$count++; //I don't know what you're using $count for
}

if (isset($data->sheets[0]['cells'][$i][7])) {
$col7 = $data->sheets[0]['cells'][$i][7];
} else {
$col7 = '';
}
if (isset($data->sheets[0]['cells'][$i][8])) {
$col8 = $data->sheets[0]['cells'][$i][8];
} else {
$col8 = '';
}
if (isset($data->sheets[0]['cells'][$i][9])) {
$col9 = $data->sheets[0]['cells'][$i][9];
} else {
$col9 = '';
}
looks realy ugly but it works.

Related

Sort by time PHP

i have this function sort_by_time, the purpose of this function is to swap the time1 if the time2 is greater than time1 but my problem is im having an error of Undefined offset: 0. Sometimes the error is Undefined offset: 1. or Undefined offset: 2. Can someone help me to prevent this error in my code? I'm thinking this in these last 3 days but i can't think of any solution on this.
in this line the error occur.
if (Payroll2::convert_time_in_minutes($_time[$j]->time_in) > Payroll2::convert_time_in_minutes($_time[$j+1]->time_in))
This error occurs because the $_time[0] is not set.
Sample time. This is dynamic not only limited to 3 sometimes it's 4, sometimes it's 5 or 1.
1 => {#6356}
2 => {#6352}
3 => {#6257}
Here's my full function code
public static function sort_by_time($_time)
{
$count = 0;
$n = count($_time);
for ($i = 0; $i < $n-1; $i++)
{
for ($j = 0; $j < $n-$i-1; $j++)
{
if (Payroll2::convert_time_in_minutes($_time[$j]->time_in) > Payroll2::convert_time_in_minutes($_time[$j+1]->time_in))
{
// swap temp and arr[i]
$temp = $_time[$j];
$_time[$j] = $_time[$j+1];
$_time[$j+1] = $temp;
}
}
}
return $_time;
}
Try this:
public static function sort_by_time($_time)
{
usort($_time,function($ad,$bd)
{
$ad = Payroll2::convert_time_in_minutes($ad->time_in);
$bd = Payroll2::convert_time_in_minutes($bd->time_in);
if ($ad == $bd) {
return 0;
}
return $ad < $bd ? -1 : 1;
});
return $_time;
}
if it doesn't work the problem can be in function Payroll2::convert_time_in_minutes

I am getting Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) on line 21 unble to understand it

$testCase="";
$strength="";
$i="";$j="";$sum=0;
$submit=$_POST['submit'];
if(!empty($submit))
{
$testCase=$_POST['TestCase'];
$strength=$_POST['strength'];
}
else
{
die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>");
}
while($testCase>0)
{
for($i=1;$strength;$i=$i+1)
{
$user[$i]=$i+1;
}
//finding the total number of stickers
for($j=1;$j<$strength;$j=$j+1)
{
$sum=$sum+$user[$j];
}
}
echo "The total number of <strong>Stickers</strong> are = " . $sum;
Because you do this: while ($testCase > 0) { and do not decrase the $testCase ever. Also, it's better if you put your loops into your if condition.
And you do not need to create $strength variable. Try this:
$sum = 0;
if (!empty($_POST["submit"]) && !empty($_POST["testCase"]) && !empty($_POST["strength"])) {
$testCase = $_POST["testCase"];
while ($testCase > 0) {
for ($i = 1; $i < $_POST["strength"];$i++) {
$user[$i] = $i + 1;
}
for ($j = 1; $j < $_POST["strength"]; $j++) {
$sum = $sum + $user[$j];
}
//Here you need to decrase testcase
$testCase--;
}
} else {
die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>");
}
echo "The total number of <strong>Stickers</strong> are = " . $sum;
NOTE: In PHP arrays are always started with 0, if you do not specify keys directly. Check your for loops for this.
This line:
for($i=1;$strength;$i=$i+1)
should probably be:
for($i=1;$i<$strength;$i=$i+1)

How to fix undefined offset and undefined index

Hello I have this code which selects from a list of 20 items 12 and from those 12 1 is randomly selected and echoed:
class Gamble
{
public static function doPointsCheck()
{
global $Gamble;
$Gamble_points = '2';
if($Gamble_points <= 1)
return false;
else
return true;
}
public static function Spin()
{
global $Wheel;
if(!self::doPointsCheck())
return 'You need way too more points to gamble';
else
{
$Result = array();
$Result = range(1, 12);
shuffle($Result);
$Result = array_slice($Result, 0, 20);
$SendToCheck = self::CheckPrize($Result);
}
}
public static function CheckPrize($Array)
{
global $Wheel;
echo '<h1>List of items you can win today:</h1><form action="class.MysticWheel.php" method="post">';
for($i = 1; $i <= count($Array); $i++)
{
$Won = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Won;
}
echo '<input name="Feeling lucky" type="submit" value="Feeling Lucky" /></form>';
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$i = rand( 1, 12 );
$Prize = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Prize;
}
}
}
When I test it I recieve the following error:
Notice: Undefined offset: 12 on line 54
Notice: Undefined index: on line 54
So i won't show the last item, it will only show 11 possible items that could have been won and get the notice.
How can I fix it?
$Result = range(1, 12);
-->
foreach (range(1, 12) as $number) {
$Result[] = $number;
}
Try this.

PHP Error Undefined Offset

Getting Notice: Undefined offset: 25 in
C:\wamp\www\finalProjectDemo\search.php on line 32
I'm trying to read in from a file and search for a specific name and address within that for output. I know a database would be best. This is for a class assignment I'm giving out that's specifically set to work this way. I believe I almost have it all, but am just getting this problem. Fairly new to PHP.
I have this code:
<html>
<body>
<?php
// read lines into array
// search array for string
// get 7 lines from there.
$i = 0;
$fileName = "addresses.txt";
$readFile = fopen($fileName, 'r');
$readByLineArray = array();
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
while (!feof($readFile))
{
$readByLineArray[$i] = fgets($readFile);
//echo "$readByLineArray[$i] read from array position $i";
//echo "<br />";
$i++;
}
fclose($readFile);
$arrLength = count($readByLineArray);
$currentArrayPosition = 0;
for ($x=0;$x<=$arrLength;$x++){
if ($searchFirstName == $readByLineArray[$x])
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $readByLineArray[$x])
{
$searchLastNameSuccess = 1;
$currentArrayPosition = $x - 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
for ($y=0;$y<=7;$y++){
echo "$readByLineArray[$currentArrayPosition]<br />";
$currentArrayPosition++;
}
?>
</body>
</html>
Thanks for all your help!
Ben---
Try foreach :-
foreach ($readByLineArray as $temp){
if ($searchFirstName == $temp)
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $temp)
{
$searchLastNameSuccess = 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
Change your for loop like this..
for ($x=0;$x<$arrLength;$x++){ //<--- Should be < and not <=
Say if your array count is 3 , so the array elements keys are arranged as 0,1,2. When you put <= in the looping as condition , your code will check for an non-existent key with an index of 3 which will thrown an Undefined Offset notice.
EDIT :
The easier way....
<html>
<body>
<?php
$fileName = "addresses.txt";
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
foreach(file($fileName) as $recno=>$records)
{
if(stripos($records,$searchFirstName)!==false && stripos($records,$searchLastName)!==false)
{
$searchFirstNameSuccess = 1;
$searchLastNameSuccess = 1;
echo "Match Found at Position : $recno";
break;
}
}
?>
</body>
</html>

preg_match in preg_match_all

$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
$radompgr = preg_match_all($match_expression,$q2,$match, PREG_SET_ORDER);
if($radompgr == TRUE){echo "found $radompgr<br>";}else{echo "not found $radompgr<br>";} //found
for ($i = 0; $i < count($match); $i++) {
$mathcas = $match[$i][1];
$radom = preg_match('/[0-9a-z]{39,41}/',$mathcas,$matches2);
if($radom == TRUE){
$match11 = $matches2[1];
echo "found".$i.": ".$match11."";}else{echo"".$i."not found :(<br>";}
} // "found0", but don`t show $match11 variable.
Show "found0", but don`t show $match11. How to do $match 11 to show?
Return:
Notice: Undefined offset: 1 in C:\xampp\htdocs\page.php on line 75
found0:
Notice: Undefined offset: 1 in C:\xampp\htdocs\copy\page.php on line 75
found1:
Notice: Undefined offset: 1 in C:\xampp\htdocs\copy\page.php on line 75
found2:
Sorry if my English is not perfect, I'm not a native. :)
Thank you for your help.
You forgot to enclose the () in preg_match():
$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
$radompgr = preg_match_all($match_expression, $q2, $match, PREG_SET_ORDER);
if ($radompgr >= 1)
{
echo 'found ' . $radompgr;
for ($i = 0; $i < count($match); $i++)
{
$mathcas = $match[$i][1];
$radom = preg_match('/([0-9a-z]{39,41})/', $mathcas, $matches2);
if ($radom >= 1)
{
$na = $matches2[1];
echo 'found' . $i . ': ' . $na;
}
else
{
echo $i . 'not found';
}
}
}
else
{
echo 'not found ' . $radompgr;
}

Categories