SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie: Dynamically writing Maths functions to the page

    In order to learn some PHP, I'm trying to write a "poll" script that will accept a users answer and then show him/her the results depending on numbers stored in a text file. I am trying to get these results shown as a percentage. Here's the important section of the script:

    Code:
    $lines = file ('poll.txt');
    $everyday=$lines[0];
    $sometimes=$lines[1];
    $never=$lines[2];
    $total=$everyday+$sometimes+$never;
    echo $total;
    
    echo "<table width='25%' border='1'>
        <tr> 
          <td colspan='2'>How often do you take the train?</td>
        </tr>
        <tr> 
          <td width='25%'>
            ($everyday+$total)*100;
               ....
    The problem occurs in the last line. Rather than carrying out the mathmatical function (as I was rather hoping that it might do!), PHP is writing ($everyday+$total)*100; directly to the screen.

    Is there some way to force my script to carry out the maths and print the result to the screen instead?

    Thanks for your help!

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <tr>
        <td width='25%'>
            ".($everyday+$total)*100."
            ...

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry - it's still printing out the string ".($everyday+$total)*100." rather than the answer to the maths

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it should have given out a parse error, try
    PHP Code:
    echo "foo ".(($everyday+$total)*100)." bar"

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid I'm still getting the same problem, although I had to change Gaheris' statement a bit due to the fact that all of the HTML for my table is already in an echo statement. Could it be this which is causing the problem?

    To try and clarify things, I thought I'd post that whole section of the script:

    Code:
    if ($_SERVER['REQUEST_METHOD']=="POST"){
    
    $lines = file ('poll.txt');
    $everyday=$lines[0];
    $sometimes=$lines[1];
    $never=$lines[2];
    $total=$everyday+$sometimes+$never;
    
    echo "<table width='30%' border='1'>
        <tr> 
          <td colspan='2'>Thank you. Here are the results.($total votes)</td>
        </tr>
        <tr> 
          <td width='25%'>
            Everyday
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$everyday'> $everyday </td>
        </tr>
        <tr> 
          <td width='25%'>
            Sometimes
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$sometimes'> $sometimes</td>
        </tr>
        <tr> 
          <td width='25%'>
            Never
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$never'> $never</td>
        </tr>
        <tr> 
          <td width='25%'>&nbsp;</td>
          <td width='75%'> 
            <div align='right'>
              <input type='submit' name='Submit' value='Submit'>
            </div>
          </td>
        </tr>
        </table>";

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried your code and it works fine, but there isn't the (($everyday+$total)*100) part in it anyway. What exactly is your problem now?

  7. #7
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for being a muppet. I've been messing around with the script too much that I posted an ammended version. Here's the script I'm using. I've put the maths part in bold:
    Code:
    if ($_SERVER['REQUEST_METHOD']=="POST"){
    
    $lines = file ('poll.txt');
    $everyday=$lines[0];
    $sometimes=$lines[1];
    $never=$lines[2];
    $total=$everyday+$sometimes+$never;
    echo "<table width='30%' border='1'>
        <tr> 
          <td colspan='2'>Thank you. Here are the results.($total votes)</td>
        </tr>
        <tr> 
          <td width='25%'>
            Everyday
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$everyday'> (($everyday/$total)*100) </td>
        </tr>
        <tr> 
          <td width='25%'>
            Sometimes
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$sometimes'> (($sometimes/$total)*100)</td>
        </tr>
        <tr> 
          <td width='25%'>
            Never
          </td>
          <td width='75%'><img src='poll.jpg' height='5' width='$never'> (($never/$total)*100)</td>
        </tr>
        <tr> 
          <td width='25%'>&nbsp;</td>
          <td width='75%'> 
            <div align='right'>
              <input type='submit' name='Submit' value='Submit'>
            </div>
          </td>
        </tr>
        </table>";

  8. #8
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's ok - I think I've got it sorted now. I needed to break down the echo statement a few times so that it didn't include the maths with in its quotations marks.

    Thanks loads for the help though

  9. #9
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh, that's exactly what I have been telling you with this.
    PHP Code:
    echo "foo ".(($everyday+$total)*100)." bar"
    Anyway, glad you sorted it out.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •