Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,403 members, 7,808,444 topics. Date: Thursday, 25 April 2024 at 12:00 PM

Need Help On This Basic Php - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Need Help On This Basic Php (3570 Views)

For Young And Basic Web Development HOP IN / Pls After Learning D Basic Php Wat Next? / Keywords / Reserved word In Q-BASIC (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: Need Help On This Basic Php by Nobody: 7:58pm On Feb 21, 2016
Djade007:
Ooo oga dhtml18... You should start researching o because op specifically mentioned you.
Did you not read the place I conceded defeat above? this basic PHP don big pass me (am even thinking of retirement as i don become olodo).

ChinenyeN:

Truthfully, you cannot get a file's contents unless you open the file. Additionally, you cannot open a file without first loading it into memory. This part has little to nothing to do with programming, and everything to do with how computers are designed. Whenever you tell the computer that you want to use a certain file on your hard disk, the computer takes your command, locates the file and makes it available for you in RAM. Even the operating system that manages your file request is running in RAM. It's simply how computers work.
And on the programming side, it is the functions like fopen, file and file_get_contents that allow us to manage our file requests. In simple terms, that is how the implementation works. Even if you used another programming language and they had a different function called whatever, it would still make the same request to the OS as fopen, file and etc.
Now, if your only objective is to get the file contents as an array, without using any function with "file" in it, then I think I understand. That is something different altogether. It can easily be done using fopen, fgetcsv and fclose (the word "file" is not written out in these functions).

<?php

$names = []; // declare here for scope purposes

// make sure file can be accessed
if(($file = fopen( "names.txt", "r" )) !== FALSE) {

// read each line in the file
// you have the names delimited by semilcolon, so use that in the function
while(($line = fgetcsv($file, ";" )) !== FALSE) {

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// done with the file, so we can now close it
fclose($file);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => alex
* [1] => wellington
* )
* )
***/
?>
You don fail already, did you not see the place above that the OP said he does not want to use stuffs like fopen, file_get_contents, file and their families.

paranorman:

i know this one too bro. Done it.
But's still uses an extended file() function.
I am talking....:
fopen
while (!feof..)
fgets
..i used these, but the code is unnessarily long and uses lots of manipulations and overwrites the whole file.
You can do better.
It seems he actually want us to do a freaking miracle like this dude:

Nmeri17:
All of you up there are big Olodos angry in fact, you all should collectively get a mention whenever "Olodo" is typed on nairaland undecided that said, we get straight to business. OP, here is the code variant you have sought all your life!

<?php
include " paranorman_miracle_working_class.inc";

$cached_file = PMWC::para_read(PMWC::para_open ("names.txt", PMWC::size("names.txt"wink, "r+"wink;

$swap_string = PMWC::replace ("nmeri17 is a god", "Alex", $cached_file);

$case_closed = PMWC::para_read($swap_string, "x"wink;

if ($case_closed) {
echo "paranorman is a nice monicker" . "\wink ";
}
?>
Re: Need Help On This Basic Php by ChinenyeN(m): 8:11pm On Feb 21, 2016
dhtml18:
You don fail already, did you not see the place above that the OP said he does not want to use stuffs like fopen, file_get_contents, file and their families.
My guy, I read his posts five times over just to make sure I understood well enough, and concluded that he may not have known the right words to use to describe his situation. From how I understand it now, he doesn't want to use functions that have the word "file" in them. I take it that he meant it literally (as in, if the function has the word "file", then he doesn't want to use it). So, I stuck with the f-type functions (they don't have the word "file" in them). Now, if he comes around and says that is not what he is looking for, then I will be at a loss. There's just no other way around this save for juju and there is no juju in computer programming.

1 Like

Re: Need Help On This Basic Php by Nobody: 8:15pm On Feb 21, 2016
ChinenyeN:

My guy, I read his posts five times over just to make sure I understood well enough, and concluded that he may not have known the right words to use to describe his situation. From how I understand it now, he doesn't want to use functions that have the word "file" in them. I take it that he meant it literally (as in, if the function has the word "file", then he doesn't want to use it). So, I stuck with the f-type functions (they don't have the word "file" in them). Now, if he comes around and says that is not what he is looking for, then I will be at a loss. There's just no other way around this save for juju and there is no juju in computer programming.
Hmn, I read the post like 20 times at least, and yet I still feel like monkey-banana, this OP has made a monkey-out-of-me!
Re: Need Help On This Basic Php by Nobody: 12:33am On Feb 22, 2016
I am still waiting o, not going to sleep o.
Re: Need Help On This Basic Php by asalimpo(m): 12:49am On Feb 22, 2016
See why basic communication skills are important. The other day a full grown papa wandered into this section and farted some of foulest english this room has seen in a long time. He stuck to his guns and other programmers stood by him too.
Now, see miscommunication keeping some1 up all night, spawning multiple posts all to get at the gist of what the op was trying to say! (excess typing , excess time, excess brain cycles constitutes wasted resources).
Re: Need Help On This Basic Php by Nobody: 6:37am On Feb 22, 2016
And i am still waiting. . . .

<?php
while (waiting==true) {
continue;
}
?>
Re: Need Help On This Basic Php by paranorman(m): 8:52am On Feb 22, 2016
Nmeri17:
All of you up there are big Olodos angry in fact, you all should collectively get a mention whenever "Olodo" is typed on nairaland undecided that said, we get straight to business. OP, here is the code variant you have sought all your life!


<?php
include " paranorman_miracle_working_class.inc";

$cached_file = PMWC::para_read(PMWC::para_open ("names.txt", PMWC::size("names.txt"wink, "r+"wink;

$swap_string = PMWC::replace ("nmeri17 is a god", "Alex", $cached_file);

$case_closed = PMWC::para_read($swap_string, "x"wink;

if ($case_closed) {
echo "paranorman is a nice monicker" . "\wink ";
}
?>

this is twice as nice than rice!

Thank goodness you understood o.

As a rookie, I never see this kain syntaxes before o..
Re: Need Help On This Basic Php by paranorman(m): 8:58am On Feb 22, 2016
ChinenyeN:


Truthfully, you cannot get a file's contents unless you open the file. Additionally, you cannot open a file without first loading it into memory. This part has little to nothing to do with programming, and everything to do with how computers are designed. Whenever you tell the computer that you want to use a certain file on your hard disk, the computer takes your command, locates the file and makes it available for you in RAM. Even the operating system that manages your file request is running in RAM. It's simply how computers work.

And on the programming side, it is the functions like fopen, file and file_get_contents that allow us to manage our file requests. In simple terms, that is how the implementation works. Even if you used another programming language and they had a different function called whatever, it would still make the same request to the OS as fopen, file and etc.

Now, if your only objective is to get the file contents as an array, without using any function with "file" in it, then I think I understand. That is something different altogether. It can easily be done using fopen, fgetcsv and fclose (the word "file" is not written out in these functions).

<?php

$names = []; // declare here for scope purposes

// make sure file can be accessed
if(($file = fopen( "names.txt", "r" )) !== FALSE) {

// read each line in the file
// you have the names delimited by semilcolon, so use that in the function
while(($line = fgetcsv($file, ";" )) !== FALSE) {

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// done with the file, so we can now close it
fclose($file);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => alex
* [1] => wellington
* )
* )
***/
?>
boss, I not implying that you do not use the fopen, fgets, fputs, fwrite functions, what I mean is that you do not use functions with 'file' in its syntax; like readfile(), file_get_contents, file(), e.t.c.
Re: Need Help On This Basic Php by paranorman(m): 9:04am On Feb 22, 2016
ChinenyeN:

My guy, I read his posts five times over just to make sure I understood well enough, and concluded that he may not have known the right words to use to describe his situation. From how I understand it now, he doesn't want to use functions that have the word "file" in them. I take it that he meant it literally (as in, if the function has the word "file", then he doesn't want to use it). So, I stuck with the f-type functions (they don't have the word "file" in them). Now, if he comes around and says that is not what he is looking for, then I will be at a loss. There's just no other way around this save for juju and there is no juju in computer programming.
you are correct...!
So, you haven't replaced the string though..
Re: Need Help On This Basic Php by paranorman(m): 9:08am On Feb 22, 2016
dhtml18:
And i am still waiting. . . .

invalid argument supplied on line 2..

Hehehehe
Re: Need Help On This Basic Php by Nobody: 9:26am On Feb 22, 2016
paranorman:


this is twice as nice than rice!

Thank goodness you understood o.

As a rookie, I never see this kain syntaxes before o..
It is a miraculous syntax in case you failed to realize it

paranorman:

boss, I not implying that you do not use the fopen, fgets, fputs, fwrite functions, what I mean is that you do not use functions with 'file' in its syntax; like readfile(), file_get_contents, file(), e.t.c.
If that is the case, use fopen in the code i supplied earlier - i am not going to write any more codes on this thread anymore for sanity reasons.

paranorman:

invalid argument supplied on line 2..
Hehehehe
We await your solution. . . .
Re: Need Help On This Basic Php by ChinenyeN(m): 2:41pm On Feb 22, 2016
paranorman:
you are correct...!
So, you haven't replaced the string though..

Replacement can easily occur in the while loop, just by adding an if condition.

<?php

while(($line = fgetcsv($file, ";" )) !== FALSE) {

// check for "alex" in $line[0], since that should be its index
if(strcmp("alex", $line[0]) == 0) {

// once found, replace "alex" with "whatever you want"
$line[0] = "whatever you want";

}

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => whatever you want
* [1] => wellington
* )
* )
***/
?>
Re: Need Help On This Basic Php by Nobody: 2:53pm On Feb 22, 2016
ChinenyeN:


Replacement can easily occur in the while loop, just by adding an if condition.

<?php

while(($line = fgetcsv($file, ";" )) !== FALSE) {

// check for "alex" in $line[0], since that should be its index
if(strcmp("alex", $line[0]) == 0) {

// once found, replace "alex" to "whatever you want"
$line[0] = "whatever you want";

}

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => whatever you want
* [1] => wellington
* )
* )
***/
?>
Chisox! you guys no go kill human-being for this thread, i am still awating the miraculous solution mind you. . . .
Re: Need Help On This Basic Php by paranorman(m): 2:57pm On Feb 22, 2016
ChinenyeN:


Replacement can easily occur in the while loop, just by adding an if condition.

<?php

while(($line = fgetcsv($file, ";" )) !== FALSE) {

// check for "alex" in $line[0], since that should be its index
if(strcmp("alex", $line[0]) == 0) {

// once found, replace "alex" with "whatever you want"
$line[0] = "whatever you want";

}

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => whatever you want
* [1] => wellington
* )
* )
***/
?>
thanks for your time on this thread, you guys were really helpful.
Re: Need Help On This Basic Php by Nobody: 3:46pm On Feb 22, 2016
paranorman:

thanks for your time on this thread, you guys were really helpful.
You need to submit the trivial/basic code o, otherwise, i go para for you seriously o.
Re: Need Help On This Basic Php by ChinenyeN(m): 6:28pm On Feb 22, 2016
paranorman:
thanks for your time on this thread, you guys were really helpful.
You should be aware that the solution provided will replace all instances of "alex" at index 0. So if you only want to replace a certain "alex" or do different replacements at certain indexes, you will have to add to the if-statement or create a decision tree of some kind.
Re: Need Help On This Basic Php by ChinenyeN(m): 6:30pm On Feb 22, 2016
dhtml18:
Chisox! you guys no go kill human-being for this thread, i am still awating the miraculous solution mind you. . . .
Haha. You fit wait until universe come collapse?
Re: Need Help On This Basic Php by Nobody: 7:54pm On Feb 22, 2016
ChinenyeN:

Haha. You fit wait until universe come collapse?
Na until i e-kill person with an e-knife on this thread before you guys know that I am not joking!
Re: Need Help On This Basic Php by paranorman(m): 2:06am On Feb 23, 2016
dhtml18:

Na until i e-kill person with an e-knife on this thread before you guys know that I am not joking!

bros, which kain solution you dey wait for na? rookie like me wey no understand anything?! That was why I laid the question all bear here, though it was kinda ambiguous at first; you guys pulled it through.

Here is my own nonsense version of the code jare, I bet you and ChinenyeN will understand me after checking it out - it's kinda unnecessarily long jare, that was why I was looking for a 'shortcut':

$file = "names.txt";
$fp = fopen($file, "r+"wink;
$output = "";
while (!feof($fp)) {
$line = fgets($fp);
$output[] = $line;
} //end while loop

$newOutput = "";
foreach ($output as $lineData) {
$lineArray = explode(";", $lineData);
$newLineArray = "";
foreach ($lineArray as $name) {
$name = trim($name);
$newLineArray[] = $name;
} //end foreach
$newOutput[] = $newLineArray;
} //end for loop

$textFile = "";
foreach ($newOutput as $data) {
$newData = "";
foreach ($data as $txt) {
if ($txt == "john"wink{
$txt = str_replace($txt, "johnson", $txt);
} //end if
if ($txt == $data[sizeof($data) - 1]) {
$txt = $txt."\n";
} else {
$txt = $txt.";";
}
$newData[] = $txt;
}
$textFile[] = $newData;
} //end loop

$updateOuput = "";
foreach ($textFile as $strng) {
$finalText = implode("", $strng);
$updateOuput .= $finalText;
} //end if

fclose($fp);
$newFp = fopen($file, "w"wink;
fwrite($newFp, $updateOuput);
fclose($newFp);
//var_dump($updateOutput);
Re: Need Help On This Basic Php by Nobody: 1:17pm On Feb 23, 2016
Shouts: Lord, God, Almighty!
*Does sign-of-the-cross*
#and#
*Dies*
Re: Need Help On This Basic Php by ChinenyeN(m): 5:50pm On Feb 23, 2016
Paranorman, I would say that you did good, being a rookie. I always try to remind people that programming is about critical thinking, algorithms and problem-solving. I also always try to encourage newer programmers to focus more on their algorithm than the language features itself, because you will always learn the language features as you go along.

Looking at the code, I see you have a clear algorithm there. You know the necessary steps needed to solve your problem, and that is excellent. Now, as you learn more about PHP and its features, you will begin to see more appropriate functions or learn to write more appropriate methods to help you implement your algorithms in the future.

Don't hurt your development as a programmer by thinking of things as "long cuts" or "short cuts". In reality, there are no "short cuts" in programming. Rather, its all about how appropriate your algorithm is and also how you implement it.
Re: Need Help On This Basic Php by paranorman(m): 6:18pm On Feb 23, 2016
ChinenyeN:
Paranorman, I would say that you did good, being a rookie. I always try to remind people that programming is about critical thinking, algorithms and problem-solving. I also always try to encourage newer programmers to focus more on their algorithm than the language features itself, because you will always learn the language features as you go along.

Looking at the code, I see you have a clear algorithm there. You know the necessary steps needed to solve your problem, and that is excellent. Now, as you learn more about PHP and its features, you will begin to see more appropriate functions or learn to write more appropriate methods to help you implement your algorithms in the future.

Don't hurt your development as a programmer by thinking of things as "long cuts" or "short cuts". In reality, there are no "short cuts" in programming. Rather, its all about how appropriate your algorithm is how you implemented it.

thanks bro.

dhtml self.. Hehehehe
Re: Need Help On This Basic Php by Nobody: 9:37pm On Feb 23, 2016
paranorman:


thanks bro.

dhtml self.. Hehehehe
*That your code has killed me on this thread* I am typing this from e-heaven!

(1) (2) (Reply)

Learn And Practice Python Programming / How To Make Money In Game Development For Developers. / If Programming Languages Were Religions...

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 74
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.