₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,306 members, 8,421,246 topics. Date: Saturday, 06 June 2026 at 05:52 AM

Toggle theme

Lil Php Question - Webmasters - Nairaland

Nairaland ForumScience/TechnologyWebmastersLil Php Question (1214 Views)

1 Reply (Go Down)

Lil Php Question by Nobody: 3:06pm On Aug 27, 2013
Am still a starter in php.. And i kinda lack the knowledge on how to implement php inorder to solve this problem..

Please Ur Response is Needed..

Help me solve this using PhP.. Different methods of approach is OK..

Create a form that asks the user to enter an amount
of money into a textbox.

When the button is clicked, the PHP page shows
what happens if you can double your money each
year for the next 10 years.

Hints: your PHP code should only have two
variables: one variable to store the amount of
money you have at the end of each year and one
variable to track the number of years.

Also, you need to have a print statement before the
loop (for the first line) and another print statement
inside the loop for the remaining 10 lines.

PS: The solution should look like this::

Solution:

If you start with $200 dollars, Then
after 1 year(s), you will have $400
after 2 year(s), you will have $800
after 3 year(s), you will have $1600
after 4 year(s), you will have $3200
after 5 year(s), you will have $6400
after 6 year(s), you will have $12800
after 7 year(s), you will have $25600
after 8 year(s), you will have $51200
after 9 year(s), you will have $102400
after 10 year(s), you will have $204800...
Re: Lil Php Question by sisqology(m): 6:21pm On Aug 27, 2013
Tshw
Re: Lil Php Question by omoelu1(m): 10:36am On Aug 28, 2013
@django.
if you have d answer, pls post it..
Re: Lil Php Question by DualCore1: 11:42am On Aug 28, 2013
By my understanding of this logic:
Djangocode: When the button is clicked, the PHP page shows
what happens if you can double your money each
year for the next 10 years.
This your proposed solution is wrong:
Djangocode: PS: The solution should look like this::

Solution:

If you start with $200 dollars, Then
after 1 year(s), you will have $400
after 2 year(s), you will have $800
after 3 year(s), you will have $1600
after 4 year(s), you will have $3200
after 5 year(s), you will have $6400
after 6 year(s), you will have $12800
after 7 year(s), you will have $25600
after 8 year(s), you will have $51200
after 9 year(s), you will have $102400
after 10 year(s), you will have $204800...
The solution should look like:
If you start with 200, then
after 1 year(s), you will have 400
after 2 year(s), you will have 800
after 3 year(s), you will have 1200
after 4 year(s), you will have 1600
after 5 year(s), you will have 2000
after 6 year(s), you will have 2400
after 7 year(s), you will have 2800
after 8 year(s), you will have 3200
after 9 year(s), you will have 3600
after 10 year(s), you will have 4000


And the PHP code for that is:
<?php
$amount = "200";

print "If you start with $amount, then <br />";

for($i=1; $i <= 10; $i++){
print "after $i year(s), you will have ".$amount * 2 * $i." <br />";
}
?>


Note I fed in the amount as $amount variable, not from a form's POST or GET. I didn't set out to give you a complete solution. Figure out the easy part of connecting this to a form and supplying the amount as a PHP GET or POST global variable.
Re: Lil Php Question by omoelu1(m): 2:52pm On Aug 28, 2013
Dual Core: By my understanding of this logic:


This your proposed solution is wrong:


The solution should look like:
If you start with 200, then
after 1 year(s), you will have 400
after 2 year(s), you will have 800
after 3 year(s), you will have 1200
after 4 year(s), you will have 1600
after 5 year(s), you will have 2000
after 6 year(s), you will have 2400
after 7 year(s), you will have 2800
after 8 year(s), you will have 3200
after 9 year(s), you will have 3600
after 10 year(s), you will have 4000


And the PHP code for that is:
<?php
$amount = "200";

print "If you start with $amount, then <br />";

for($i=1; $i <= 10; $i++){
print "after $i year(s), you will have ".$amount * 2 * $i." <br />";
}
?>


Note I fed in the amount as $amount variable, not from a form's POST or GET. I didn't set out to give you a complete solution. Figure out the easy part of connecting this to a form and supplying the amount as a PHP GET or POST global variable.
thank you jare baba dual core... am almost crying trying to solve dis code..
am starting to think “ so, i don't know anything yet in php“..
gbosa to u , boss
Re: Lil Php Question by Nobody: 6:56am On Sep 04, 2013
@DualCore... Thanks boss.. I aint surprised cuz you are a geek..

@omoelu. Brov, that question almost killed ma brain cells..

@Dual core.. Pls, anytime I run php form scripts in which I embed my html forms and run it on ma localhost server(wamp), I get results buh whenever I seperate the html form and the corresponding php script that will run it, all I get is a blank page.. Any solutions?? And please what are the advantages of embedding php scripts inside html page n the disadvantages..

Thanks..
Re: Lil Php Question by omoelu1(m): 10:01am On Sep 04, 2013
like u mean when you direct the “action“ call to another php page for processing?
Re: Lil Php Question by DualCore1: 2:39pm On Sep 04, 2013
omoelu1: thank you jare baba dual core... am almost crying trying to solve dis code..
am starting to think “ so, i don't know anything yet in php“..
gbosa to u , boss
You can know all you need to know about PHP here: http://www.php.net/manual/en/
I advise you leave PHP eBooks alone and just read up the official PHP documentation first (http://www.php.net/manual/en/).
In my experience PHP eBooks are just like religious pamphlets which make reference to the Bible (in this case http://www.php.net/manual/en/) and are given an interpretation as understood by the writer of the eBook/book. So skipping the pamphlets and going straight to the PHP Bible (http://www.php.net/manual/en/) will do you more good, if you meet things you don't understand...Google it or Nairaland it (you will most likely find a solution to your problem on Stackoverflow.com through Google's search results.

You don't have to memorize what you read or know how to use all PHP functions. Just have an idea of what can be done with what function. When you actually have to use a function in your work you can look up it's usage in the PHP manual and apply. The more regular your usage of a function is the less you'll be consulting the manual to see its usage.

Djangocode: @Dual core.. Pls, anytime I run php form scripts in which I embed my html forms and run it on ma localhost server(wamp), I get results buh whenever I seperate the html form and the corresponding php script that will run it, all I get is a blank page.. Any solutions?? And please what are the advantages of embedding php scripts inside html page n the disadvantages..

Thanks..
I have no idea what the advantages and disadvantages are of either options. I just keep all my form processor files in a different folder for organization. You can go through Google to see what's best to work with.

About the issue of separating your form from its processor and the problem you encounter, this explains it:
omoelu1: like u mean when you direct the “action“ call to another php page for processing?
When you separate your form from its processor, the "action" attribute of your <form></form> should direct the form to the processor. So if your processor (e.g processor.php) is in the same directory as your html file then the form declaration should be <form action="processor.php" ...
Re: Lil Php Question by Nobody: 9:39am On Sep 05, 2013
@Dualcore... Thanks for the tip about the php main manual.. I've downloaded it.. Will start going 2ru it..

I also wanna have the idea of how I can implement my php script when using a CMS for example wordpress or joomla.. Maybe I created a custom registration page with php and I want to implement it while building a site with a CMS..

Any ideas..
Re: Lil Php Question by DualCore1: 9:48am On Sep 05, 2013
I have no working experience with the opensource CMSes. Others can help you here or you can Google "implementing custom code with CMS".
Re: Lil Php Question by Nobody: 12:07pm On Sep 05, 2013
@Dualcore..

Thanks Boss.. Sorry about my naïve questions..

If I have a form with a submit button, I use an isset function to check whether d user clicked it and also to check whether the user filled the form before submit using the !empty function..

Example:

If(isset($_POST['submit']))
{
If(!empty($_POST['name'])) #Based on html form
{

}
}

Do I need to declare the variables for the html inside the isset function or before the function...??
Re: Lil Php Question by omoelu1(m): 3:02pm On Sep 05, 2013
perhaps boss is busy, he will attend to you soon.
Re: Lil Php Question by teemy(m): 1:20pm On Sep 10, 2013
Seems to me you are trying to create an investment portfolio guide for like say forex. Using you initial estimates of doubling every year,

If you start with $200 dollars, Then
after 1 year(s), you will have $400
after 2 year(s), you will have $800
after 3 year(s), you will have $1600
after 4 year(s), you will have $3200
after 5 year(s), you will have $6400
after 6 year(s), you will have $12800
after 7 year(s), you will have $25600
after 8 year(s), you will have $51200
after 9 year(s), you will have $102400
after 10 year(s), you will have $204800...
Try this,

<?php
echo "<center>";

$maxyears = 12; //just edit the number of years for your drop down menu
echo "<p>
<form method=post action=''><table border=0>
<tr><td align=right>Input your starting capital $</td><td><input type=text name=initial_balance></td></tr>
<tr><td>Number of years to run</td><td><select name=max_years>";
for($i=1;$i<=$maxyears;$i++){
echo "<option value=".$i.">".$i."</option>";
}
echo "
</select><input type=submit name=submit value=Calculate!></td></tr>
</table></form>
";


If(isset($_POST['submit']))
{
If(empty($_POST['initial_balance'])){
echo "<font color=red><b>Initial Balance</b> Field must not be empty</font>";exit;}
if(!preg_match("/^[0-9]/", $_POST['initial_balance'])){
echo "<font color=red>Invalid <b>Initial Balance</b> Format</font>";exit;
}
// comment out the two lines below to use tabular format
echo "If you start with <b>$".$_POST['initial_balance']."</b>, then<br>";
echo "<table border=0><tr><td>";

// remove the two comments below to use in tabular format
//echo "<table border=1>";
//echo "<tr><td><b>No of Years</b></td><td><b>Returns</b></td></tr>";

for($i=1;$i<=$_POST['max_years'];$i++){
// comment out line below to use tabular format
output($i,$_POST['initial_balance']);

// remove comments below to use in tabular format
//echo "<tr align=center><td>".$i."</td><td>$".($_POST['initial_balance']*pow(2,$i))."</td></tr>";
}
// remove comments below to use in tabular format
//echo "</table>";

// comment out line below to use tabular format
echo "</td></tr></table>";exit;

}

function output($num,$balance){
if($num>1){$time="years";}else{$time="year";}
echo "after <b>".$num."</b> ".$time.", you will have <b>$".$balance*pow(2,$num)."</b><br>";
}
echo "</center>";
?>

Re: Lil Php Question by Nobody: 7:12am On Sep 11, 2013
Damn..

@Teemy That was classic.. Thanks a Bunch.. I will try and run it and get back to you soon..
Re: Lil Php Question by DualCore1: 9:30am On Sep 11, 2013
Sorry I have been busy. Your question about where to declare your POST variables can be answered from Teemy's code.
Re: Lil Php Question by Nobody: 12:14pm On Sep 13, 2013
@Dualcore.. Thanks a Bunch..

@Teemy and Dualcore: Please i have a question...

Its about include and require... I know what it does but i have not yet used it.

Now my questions are:

1) Which one is more useful(any security threats)....??

2) If i download a free web template and edit the css and html to my taste, i may want to make my website dynamic by turning the whole site into php.. What are the exact steps i need to take to achieve this?? I saw the importance of using this code since i read that it reduces the way one edits his website due to the functions being called.. Please explain..

Thanks...
Re: Lil Php Question by teemy(m):
Its about include and require... I know what it does but i have not yet used it.
Say for instance you have file1.php and file2.php and there are common functions that are used in both files, you could group those functions in an include file say

include.php/inc/cache (there might be other extensions)

So instead of having to rewrite those general functions in all your files, you could have them in an include file which you can call at the point of need.

Calling out the file
include 'include.php';
require 'include.php';
(N.B You can use sinngle or double quotes)

Take an example, your database connection logins.

let's create database.inc (just save as so in your textpad/IDE)

<?php

function connection(){
//database connection and selection
define ('DB_HOST', 'localhost');
define ('DB_USER', 'Djangocode');
define ('DB_PASS', 'password');
define ('DB_NAME', 'mydatabase');

$ilink = mysql_pconnect(DB_HOST, DB_USER, DB_PASS) or die("Connection Unsuccessful!" );
$getDB = mysql_select_db(DB_NAME, $ilink) or die("database Not Detected!" );
}
?>
At the point of connection to your database before making mysql queries you can

include 'database.inc';
connection();
or

require 'database.inc';
connection();
and presto you can begin your 'select' statements.

Consider a case where you have completed your project on your local pc using wamp and while trying to install your sql script on your web server online you create a

database and username host_Djangocode and host_mydatabase respectively instead of your local Djangocode and mydatabase. If you did not have an include file you would

have to make changes to ALL your website scripts that make use of database connections. Think of it as an external css file that makes your work neater for one place

changes.

1) Which one is more useful(any security threats)....??
I heard one is a bit faster than the other, can't remember which one but the difference is negligible I believe but for security I dont know the difference.

One tip on security on the net I can say is there is no security anywhere as long as someone really wants to deal with you so just do your beat and hope for the best.

Work on sql injections, bots and the rest. It also would not be bad to at the end of your scripts

//I hail thee oh hacker, at thy exit do leave these pieces as thou has met them. Respects
Don't be surprised one day to see a new line saying

//Fadeyi Waz Hia, In and Out, Cheers Amigo
Really just pray you are not targeted. Yes I said pray.

2) If i download a free web template and edit the css and html to my taste, i may want to make my website dynamic by turning the whole site into php.. What are

the exact steps i need to take to achieve this?? I saw the importance of using this code since i read that it reduces the way one edits his website due to the

functions being called.. Please explain..
Grab everything into your text editor/IDE and simply save as a .php file.
E.g

<html>

alltheblahblahblahblah

<?php
phpcodeImightuse();
?>

moreblahblahblah

</html>
Save as newphpfile.php

Cheers
Re: Lil Php Question by Nobody: 11:31pm On Sep 14, 2013
@Teemy..

Thanks Brov.. That was classic..

Will get back to u soon.. Are u on BBM, Facebook, whatsapp,, Twitter..

Just send me Ur I'd's..

Thanks a Bunch..

Will be back with more questions..
Re: Lil Php Question by teemy(m): 7:32am On Sep 15, 2013
Check my profile, cheers
Re: Lil Php Question by CODEEATER(m): 5:25pm On Sep 15, 2013
Impressive... hope I will c activity like this in my coming programmers forum...

Still setting it up sha
Re: Lil Php Question by teemy(m): 5:33pm On Sep 15, 2013
CODE-EATER:
Impressive... hope I will c activity like this in my coming programmers forum...

Still setting it up sha
Wishing you the best.
Re: Lil Php Question by Nobody: 7:08am On Sep 16, 2013
@CODE-EATER.. Don't forget to make me an admin...

@Teemy.. The "echo" and the "print" statement.. Are they not the same..

PS - Do u have any hard cover php textbook..?? Would love to have a Copy.. Am Based in Abuja..
Re: Lil Php Question by teemy(m): 7:40am On Sep 16, 2013
'echo' does what it looks like i.e reveberates what you ask it to to the web browser. Print likewise. I guess I am quite used to the echo construct. In the meantime download the php documentation from http://php.net/download-docs.php (preferably the tar.gz file which you can extract with winrar) as it contains the list of functions you will be using. You can start learning from the index.html page or jump to the indexes.html for the list of php functions and their uses.

P.S Hope you are working on a project that will redefine the webspace we know of. Cheers
Re: Lil Php Question by Nobody: 7:46am On Sep 17, 2013
@Teemy..

Thanks Dude.. Probs is, Most times when i wanna read, i'll be experiencing light issues.. That is why i asked if u have or can recommend any hard cover textbook on PHP/mysql...

As per working on a Project, I'll definitely get there. As long as the Drive remains in me which i believe will be on for a long time.

Thanks Dude..

Yo profile didnt show me much about u.. Maybe u can drop your twitter handle, whatsapp, bbm..
Re: Lil Php Question by Kidstell: 11:15am On Sep 17, 2013
omoelu1: thank you jare baba dual core... am almost crying trying to solve dis code..
am starting to think “ so, i don't know anything yet in php“..
gbosa to u , boss
i think the oaths have you more itch than did the code. Follow up on more maths to help your logic
Re: Lil Php Question by teemy(m):
Djangocode:
@Teemy..

Thanks Dude.. Probs is, Most times when i wanna read, i'll be experiencing light issues.. That is why i asked if u have or can recommend any hard cover textbook on PHP/mysql...

As per working on a Project, I'll definitely get there. As long as the Drive remains in me which i believe will be on for a long time.

Thanks Dude..

Yo profile didnt show me much about u.. Maybe u can drop your twitter handle, whatsapp, bbm..
08057030733*. those days i did not have a whatsapp account. i believe you do not need me now. btw i don wash compared with new things on ground. You can join Cy's group 'Web UI / UX'.
1 Reply

A Lil CSS QuizA Lil Help Plz. How Do I Patent My New Web Application?Lil Problem While Trying To Upload To Test My Little Work234

Full Practical Web Development Training For September At YemlatremodifiedWhat Is Google Penguin 4.0?