Jump to content

Recommended Posts

my only experience with php is from a book i bought about a year ago, and while it gave me a basic understanding, i'm still a complete novice with it. i don't program for a living, and i rarely do it for fun anymore. i have searched both in this and other forums, as well as general internet searches, but haven't been able to locate an answer and so here i post ...

 

i'm a member of a forum for people who have quit smoking, and have been trying to help out by creating a signature creator for the members.

 

what i'm trying to do is have a page (ie create.php), where the user enters the date they quit smoking, the number of cigarettes they used to smoke per day, and their cost/pack of smokes.

then take that info and create a dynamic sig for them, which would display the number of days since they quit, the money they have saved (# of cigs * 20 * cost/pack), and the number of cigarettes they haven't smoked (# of cigs * # of days).

not only create the sig (over a graphic which i've made), but also output the html and bbcode so they can copy/paste into their profiles.

 

i've written the first page (create.php), and i think its right, but now i'm completely lost and don't know which direction to go. any help to get to the final result would be greatly appreciated.

 

 

<div id="info"><img src="images/bg.png" /><br><br>
<form style="margin: 0px;" method="get" onsubmit="return checkDomain(this);" action="create.php" id="form1" name="form1">
<nobr>Cigarettes smoked per day:<input name="perday" type="text"></nobr><br><nobr>Cost Per Pack:<input name="perpack" type="text"></nobr><br>Quit Date: <select name='Month'>
        <option value='1' >Jan</option>
        <option value='2' >Feb</option>
        <option value='3' >Mar</option>
        <option value='4' >Apr</option>

</select>
<select name='Day'>
        <option value='1' >1</option>
        <option value='2' >2</option>
        <option value='3' >3</option>


</select>
<select name='Year'>
        <option value='2010' >2010</option>

        <option value='2009' >2009</option>
        <option value='2008' >2008</option>
        <option value='2007' >2007</option>


</select>
<br><input value="Generate" id="submit1" name="submit1" src="create.php" type="submit"></form>

 

Link to comment
https://forums.phpfreaks.com/topic/191129-creating-dynamic-forum-sigs/
Share on other sites

<?php
if ($_GET['submit1'] == 'Generate')
{
   $dateQuit = mktime(0,0,0,$_GET['Day'],$_GET['Month'],$_GET['Year']);
   $dateNow  = time();
   
   // find number of days since quit
   $numDaysSinceQuit = floor(($dateNow - $dateQuit) / 60 / 60 / 24);
   print('Days since quiting: '.$numDaysSinceQuit.'<br/>'."\n");
   
   // find number of cigs not smoked
   $numCigsNotSmoked = round($numDaysSinceQuit * $_GET['perday']);
   print('Number of cigs not smoked: '.$numCigsNotSmoked.'<br/>'."\n");
   
   // find money saved on cigs (assuming 20 cigs per pack)
   $moneySaved = (ceil($numCigsNotSmoked / 20) * $_GET['perpack']);
   print('Money saved from not smoking: $'.$moneySaved.'<br/>'."\n");
}
?>

 

Used mktime to create your date into seconds and then time() to get now in seconds. Find the difference by subtracting quit date in second from now in seconds then do simple math to obtain days since quit. The rest is simple math to work out the data.

 

PS: See script in action here: http://www.beardeddonkey.com/hosted/phpFreaksTest.html

I edited your HTML to make the action go to the phpFreaksTest.php script, which contains the above code.

thank you so much catfish. i did look at the script in action and its exactly what i'm looking for.

however, when i try it on my server it doesn't work, the resulting page (phpFreaktest.php) with variables just shows a blank page.

i don't understand that because i copied both your html and php verbatim, including naming conventions ... any ideas?

seehttp://joker63.com/ecfsig/phpfreaksTest.html

 

 

 

blank page means either 1. the page is not entering the if () condition code block. to debug this possibility, add a print() line inside the if() block like:

<?php
if ($_GET['submit1'] == 'Generate')
{
   print('We are inside the if block.<br/>'."\n");
   // etc
}

if it is printing the line it is evaluating the if() statement as true and should be printing everything else. if it is printing the debug line and NOT printing the other stuff, there is likely a problem with some code inside the if block.

 

2. the page could be failing to parse and an error is not being displayed, possibly due to server configuration. To check this, you will need to find a php error log file. There may be one generated in the directory the script resides in (this is how my webhost works) or there may be a global one on your account somewhere (look for a 'log' folder perhaps?). It should be adding an error to the log file if the script is failing.

 

I can't see any reason why it shouldn't work if you are using the same files as on my server.

thanks again catfish.

 

i've tried the print line, still get a blank page.

my log file is empty (cept for stuff from 6 months ago). also contacted my host(godaddy), who confirmed - no php errors logged.

 

do you (or anyone else) see any errors that would cause this?

<?phpif
($_GET['submit1'] == 'Generate')
{
   print('We are inside the if block.<br/>'."\n");
   
   $dateQuit = mktime(0,0,0,$_GET['Day'],$_GET['Month'],$_GET['Year']);
   $dateNow  = time();

   // find number of days since quit
   $numDaysSinceQuit = floor(($dateNow - $dateQuit) / 60 / 60 / 24);
   print('Days since quiting: '.$numDaysSinceQuit.'<br/>'."\n");

   // find number of cigs not smoked
   $numCigsNotSmoked = round($numDaysSinceQuit * $_GET['perday']);
   print('Number of cigs not smoked: '.$numCigsNotSmoked.'<br/>'."\n");

   // find money saved on cigs (assuming 20 cigs per pack)
   $moneySaved = (ceil($numCigsNotSmoked / 20) * $_GET['perpack']);
   print('Money saved from not smoking: $'.$moneySaved.'<br/>'."\n");

      }
      ?>

 

 

if it is not printing "We are inside the if block." it means the if condition is failing which would mean the data is not arriving to the script correctly (ie: $_GET data doesn't get passed) you can debug by adding:

print_r($_GET);
print('<br/>'."\n");

before the if () line.

<?phpif
($_GET['submit1'] == 'Generate')

I hope that is a copy & paste error and not how your code looks?

 

edit: http://joker63.com/ecfsig/phpFreaksTest.php?perday=10&perpack=25&Month=1&Day=1&Year=2010&submit1=Generate

looks like you got it going?

Days since quiting: 36

Number of cigs not smoked: 360

Money saved from not smoking: $450

1. yes, copy n paste error

 

2. and yes, it looks like its working.

 

that's bizarre, cause i haven't changed anything since my last post ... a gift horse perhaps :)

 

thanks again catfish!! now i have to work on placing in an image and producing the bbcode.

 

if i had to do this for a living i'd be dead ...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.