Welcome Guest, Not a member yet? Register   Sign In
Find - IfFound - IfNotFound in CodeIgniter
#1

I want to carry out a simple set of functions that I can do easily in a text editor but I cant find how it can be done in CodeIgniter or PHP.

I would set out the functions in a text editor as follows;

PHP Code:
Open "C:\Folder1\File1.txt"
Find "abc"
IfNotFound
"Open "C:\Folder2\File.txt"
Find "
abc"
EndIf
IfNotFound
"
Open "C:\Folder3\File.txt"
Find "abc"
EndIf
IfNotFound
"Open "C:\Folder4\File.txt"
"
say something"
ExitMacro
EndIf
IfFound
"
Open "C:\Folder5\File.txt"
Find "def"
EndIf
IfNotFound
"Open "C:\Folder6\File.txt"
"
say something different"
EndIf
IfFound
"
Open "C:\Folder7\File.txt"
"say something else"
EndIf 

Does anybody know if the same set of functions can be done in CodeIgniter?

I know how to "fopen" in CodeIgniter but that is all.
Reply
#2

Since you know about fopen I assume you have found the PHP - Filesystem - Manual.

Another piece you need is the PHP - Directory Functions - Manual.

Here are a few functions you might find useful
glob — Find pathnames matching a pattern
scandir — List files and directories inside the specified path
Reply
#3

(This post was last modified: 09-01-2019, 10:07 AM by Wouter60.)

Put the contents of each file in a string.
Use strstr() or stristr() to search for text in that string.
If strstr() or stristr() returns false, then the text wasn't found.

Besides that, put the files you need to search in an array and use a loop to check all files.

Example:
PHP Code:
$found false;
$this->load->helper(array('directory','file'));
$filenames directory_map('./mydirectory/');
foreach (
$filenames as $filename) {
  $filetext read_file('./mydirectory/' $filename);
  if (stristr($filetext,'abc')) {
      $found true;
      $found_in $filename;
      break;  //exit the foreach loop
  }
}
if (
$found) {
  echo 'The string "abc" was found in ' $found_in;
}
else {
  echo 'The string "abc" was not found in any file in ./mydirectory/';

Reply
#4

(09-01-2019, 06:47 AM)Wouter60 Wrote: Put the contents of each file in a string.
Use strstr() or stristr() to search for text in that string.
If strstr() or stristr() returns false, then the text wasn't found.

Besides that, put the files you need to search in an array and use a loop to check all files.

Example:
PHP Code:
$found false;
$this->load->helper(array('directory','file'));
$filenames directory_map('./mydirectory/');
foreach (
$filenames as $filename) {
  $filetext read_file('./mydirectory/' $filename);
  if (stristr($filetext,'abc')) {
      $found true;
      $found_in $filename;
      break;  //exit the foreach loop
  }
}
if (
$found) {
  echo 'The string "abc" was found in ' $found_in;
}
else {
  echo 'The string "abc" was not found in any file in ./mydirectory/';


Thanks Wouter60, I've formulated the coding below which doesnt work but should indicate what Im trying to do.

PHP Code:
$file1 directory_map('C:xampp/htdocs/file1/name.php');
$file2 directory_map('C:xampp/htdocs/file2/name.php');
$file3 directory_map('C:xampp/htdocs/file3/name.php');
$file4 directory_map('C:xampp/htdocs/file4/word.php');
{
$filetext read_file($file1);
if (
strstr($filetext,'$name'))
{
$found false;
exit;
}
if (
$found// A PHP Error was encountered - Message: Undefined variable: found
$filetext read_file($file2);
if (
strstr($filetext,'$name'))
{
$found false;
exit;
}
if (
$found// A PHP Error was encountered - Message: Undefined variable: found
$filetext read_file($file3);
if (
strstr($filetext,'$name'))
{
$found false;
exit;
}
if (
$found// A PHP Error was encountered - Message: Undefined variable: found
{
echo 
'$name was not found';
}
{
$found true;
$found_in = ($file1);
exit;
}
{
$found true;
$found_in = ($file2);
exit;
}
{
$found true;
$found_in = ($file3);
exit;
}
if (
$found)
{
$filetext read_file($file4);
if (
strstr($filetext,'$word'))
$found true;
$found_in $file4;
exit;
}
if (
$found)
$this->load->view('work');
{
$found false;
exit;
}
if (
$found)
{
echo 
'$word is incorrect';
}


Can you please correct where it is wrong?
Reply
#5

Quote:Can you please correct where it is wrong?

I can, but it seems rather useless. I don't mean to be rude, but your question indicates that you have poor understanding of php. If you want to move forward with CodeIgniter, some basic knowledge of php, javascript, html and css is really necessary. It hardly helps if I write the code for you. I already gave you a good example. Analyze that line by line until you completely understand what this code is doing. Then you will be able to work it out for your own situation.
Good luck!
Reply
#6

(09-02-2019, 08:54 AM)Wouter60 Wrote:
Quote:Can you please correct where it is wrong?

I can, but it seems rather useless. I don't mean to be rude, but your question indicates that you have poor understanding of php. If you want to move forward with CodeIgniter, some basic knowledge of php, javascript, html and css is really necessary. It hardly helps if I write the code for you. I already gave you a good example. Analyze that line by line until you completely understand what this code is doing. Then you will be able to work it out for your own situation.
Good luck!

I did analyze line by line but your script was completely out of line with what I'm trying to do. I don't need to loop through multiple files, it is only 3 known files initially then maybe a 4th that need to be searched.

Yes, I have a poor understanding of php, but I'd rather say its average, however the only way I can learn is by others writing the script. And I only need to be told once. I do have basic knowledge of php, javascript, html and css. Its not much that I'm asking. And I might add the userguide does not provide scripts as I need here, nor can I find anything by Googling. I only ask a question here when I'm completely exhausted from searching elsewhere.

So can I ask that you to be a little patient with people who are learning.
Reply
#7

(This post was last modified: 09-03-2019, 08:52 AM by Wouter60.)

OK, I've found some time to explain a few things.

directory_map is a helper function which will return the filenames in a given directory.
If you already know the filenames, it's easier to do this:
PHP Code:
$file1 'C:\\xampp\\htdocs\\file1\\name.php'
On a Windows file system, you must escape the backslashes!

If you have more than one file to perform the same type of actions on, I would prefer an array (like in my example). Otherwise, you'll have to write a lot of repetitive code.
One of the principles of programming is: DRY (don't repeat yourself).

To make an array of the files you want to search:
PHP Code:
$filenames = array(
  'C:\\xampp\\htdocs\\file1\\name.php',
  'C:\\xampp\\htdocs\\file2\\name.php',
  'C:\\xampp\\htdocs\\file3\\name.php',
  'C:\\xampp\\htdocs\\file4\\word.php',
); 

To prevent an error message: Undefined variable: found
make sure you define the variable $found before you do anything else.
That's why I put it at the beginning of my code block:
PHP Code:
$found false;  //if nothing happens, $found has the initial value false 

It's not logical at all (and bad practice) to set $found to false if the value you are looking for, is found.
Set it to true if the result is positive, or false if the result is negative. Not the other way around.

You have this line of code:
PHP Code:
if (strstr($filetext,'$name')) 
This will return a result if the literal string '$name' (with dollar sign!) occurs in the $filetext string.
If $name is meant as a variable, don't use quotes around it. But remember: the $name variable should be defined before you use it!
Reply
#8

(This post was last modified: 09-12-2019, 07:25 AM by christaliise.)

(09-03-2019, 08:51 AM)Wouter60 Wrote: OK, I've found some time to explain a few things.

directory_map is a helper function which will return the filenames in a given directory.
If you already know the filenames, it's easier to do this:
PHP Code:
$file1 'C:\\xampp\\htdocs\\file1\\name.php'
On a Windows file system, you must escape the backslashes!

If you have more than one file to perform the same type of actions on, I would prefer an array (like in my example). Otherwise, you'll have to write a lot of repetitive code.
One of the principles of programming is: DRY (don't repeat yourself).

To make an array of the files you want to search:
PHP Code:
$filenames = array(
  'C:\\xampp\\htdocs\\file1\\name.php',
  'C:\\xampp\\htdocs\\file2\\name.php',
  'C:\\xampp\\htdocs\\file3\\name.php',
  'C:\\xampp\\htdocs\\file4\\word.php',
); 

To prevent an error message: Undefined variable: found
make sure you define the variable $found before you do anything else.
That's why I put it at the beginning of my code block:
PHP Code:
$found false;  //if nothing happens, $found has the initial value false 

It's not logical at all (and bad practice) to set $found to false if the value you are looking for, is found.
Set it to true if the result is positive, or false if the result is negative. Not the other way around.

You have this line of code:
PHP Code:
if (strstr($filetext,'$name')) 
This will return a result if the literal string '$name' (with dollar sign!) occurs in the $filetext string.
If $name is meant as a variable, don't use quotes around it. But remember: the $name variable should be defined before you use it!

I spent many hours trying to get your snippet to work and came up with the following. It functions without error but does not do what I want, that is find, copy & open another file.

PHP Code:
$name $this->input->post('name');
$found false;
$a directory_map("C:xampp/htdocs/file1/name.php""r"); //$a cannot contain an integer
$filetext read_file("C:xampp/htdocs/file1/name.php" $a);
if (
strstr($filetext,'$name')) //strstr() expects par1 be string find 1st "world" inside "Hello world!" & return rest of string:
$found true;
if (isset(
$found))
echo (
"$name was found in $filetext");
else
echo (
"$name was not found in $filetext");
exit;
fclose($a); 

I got more sense out of the following;

PHP Code:
if(!file_exists("C:xampp/htdocs/file1/name.php")) exit ("$name DoesNotExist");
if(!
file_exists("C:xampp/htdocs/file2/name.php")) exit ("$name DoesNotExist"); 

But it only works "sometimes" so I'll explain. It is not precisely what I want but it is as close and workable as I can get, that is if I can get it working "all the time".

If I have that coding on its own, and when refreshing, it just displays the message "DoesNotExist".

But if I comment out by // that allows the view page to display with the text boxes. If I enter the right names into the text boxes and click submit, and then go back to the Controller and remove the // then back to the browser and click submit again, everything works good, and including if I enter a wrong name.

But if I refresh again it displays the message.

I've tried numerous variations & coding to no avail. However if I start a new function and surround the coding with curly brackets { } it doesn't display the message, but also doesn't continue the way it should.

Wouter60 do you have any suggestions? My knowledge with functions is limited so I need some expertise.

I had considered Python and downloaded that language but it installed 32 bit whereas my machine is 64 bit so I uninstalled without investigating any further.

Do you know anything about Python?

Update 12/09/2019

I have coding similar to below. The first line works but the 2nd & 3rd lines don't work.

PHP Code:
$name $this->input->post('name');

if(!
file_exists("C:xampp/htdocs/file1/$name")) exit ("$name DoesNotExist");
if(!
file_exists("C:xampp/htdocs/file2/$name")) exit ("$name DoesNotExist");
if(!
file_exists("C:xampp/htdocs/file3/$name")) exit ("$name DoesNotExist"); 

I've tried looping "foreach" but can't get that to work either. Looping appears to only return "echo" and that is not what I want.

Can a loop "exit" if the result is true? In other words if the file does not exist can it "exit"?
Reply
#9

Exit ends your php script.
Use break to jump out of a for each loop.
Reply
#10

(09-14-2019, 03:30 AM)Wouter60 Wrote: Exit ends your php script.
Use break to jump out of a for each loop.

I have everything working simply by rearranging the file paths and without looping or functions and at the end I had to insert the following.

PHP Code:
if(isset($_POST['submit'])) redirect(BASE_URL.'home'); 

But that nullifies the form validation. How do I overcome that?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB