Getting two variables from a PHP loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chimambo@googlemail.com

    Getting two variables from a PHP loop

    Hi Everyone,
    I have a huge problem. I am trying to get two corresponding variables
    from a Checkbox and a Text box. e.g. If I check a value, say:
    enrolled? [] (checkbox) then I have to put the date enrolled as a
    text. So when inserting in a table I want the enrolled to correspond
    to each date. Here is what I am trying but not working (In the echos
    is where I am trying to have the values into the table):

    while (list($key, $val) = @each ($box))
    {
    echo "$val";
    while (list($key, $v_val) = @each ($dose))
    {
    echo "$v_val";
    }
    }
    echo "<form method=post action=''>";
    echo "<table border='0' cellspacing='0' style='border-collapse:
    collapse' width='100' >

    <tr bgcolor='white' >
    <td width='25%'><in put type=checkbox name=box[] value='John'></td>
    <td width='13%'>&nb sp;John</td>
    <td width='12%'><in put type=text name=date[] size=8></td>
    <td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
    <td width='13%'>&nb sp;Peter</td>
    <td width='12%'><in put type=text name=date[] size=8></td>
    .......
    ......

  • rickycornell@gmail.com

    #2
    Re: Getting two variables from a PHP loop

    On May 18, 7:02 am, chima...@google mail.com wrote:
    Hi Everyone,
    I have a huge problem. I am trying to get two corresponding variables
    from a Checkbox and a Text box. e.g. If I check a value, say:
    enrolled? [] (checkbox) then I have to put the date enrolled as a
    text.
    I'm kind of confused- you mean the form is submitted, and if the
    enrolled checkbox was checked, populate the enrollment date text
    field? I'll go on that assumption.
    So when inserting in a table I want the enrolled to correspond
    to each date. Here is what I am trying but not working (In the echos
    is where I am trying to have the values into the table):
    >
    while (list($key, $val) = @each ($box))
    {
    echo "$val";
    while (list($key, $v_val) = @each ($dose))
    {
    echo "$v_val";
    }}
    >
    echo "<form method=post action=''>";
    echo "<table border='0' cellspacing='0' style='border-collapse:
    collapse' width='100' >
    >
    <tr bgcolor='white' >
    <td width='25%'><in put type=checkbox name=box[] value='John'></td>
    <td width='13%'>&nb sp;John</td>
    <td width='12%'><in put type=text name=date[] size=8></td>
    <td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
    <td width='13%'>&nb sp;Peter</td>
    <td width='12%'><in put type=text name=date[] size=8></td>
    ......
    .....
    [?php
    if($enrolled == "on") {
    $enrollment_dat e = $dbh->query("SELEC T enroll_date FROM students
    WHERE id = 'studentid'");
    }
    ?]
    [input type='text' name='enrollmen t_date' value='[?=
    $enrollment_dat e; ?]']

    Like that? Am I missing something?

    Comment

    • chimambo@googlemail.com

      #3
      Re: Getting two variables from a PHP loop

      On 18 May, 15:10, "rickycorn...@g mail.com" <rickycorn...@g mail.com>
      wrote:
      On May 18, 7:02 am, chima...@google mail.com wrote:
      >
      Hi Everyone,
      I have a huge problem. I am trying to get two corresponding variables
      from a Checkbox and a Text box. e.g. If I check a value, say:
      enrolled? [] (checkbox) then I have to put the date enrolled as a
      text.
      >
      I'm kind of confused- you mean the form is submitted, and if the
      enrolled checkbox was checked, populate the enrollment date text
      field? I'll go on that assumption.
      >
      >
      >
      >
      >
      So when inserting in a table I want the enrolled to correspond
      to each date. Here is what I am trying but not working (In the echos
      is where I am trying to have the values into the table):
      >
      while (list($key, $val) = @each ($box))
      {
      echo "$val";
      while (list($key, $v_val) = @each ($dose))
      {
      echo "$v_val";
      }}
      >
      echo "<form method=post action=''>";
      echo "<table border='0' cellspacing='0' style='border-collapse:
      collapse' width='100' >
      >
      <tr bgcolor='white' >
      <td width='25%'><in put type=checkbox name=box[] value='John'></td>
      <td width='13%'>&nb sp;John</td>
      <td width='12%'><in put type=text name=date[] size=8></td>
      <td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
      <td width='13%'>&nb sp;Peter</td>
      <td width='12%'><in put type=text name=date[] size=8></td>
      ......
      .....
      >
      [?php
      if($enrolled == "on") {
      $enrollment_dat e = $dbh->query("SELEC T enroll_date FROM students
      WHERE id = 'studentid'");}
      >
      ?]
      [input type='text' name='enrollmen t_date' value='[?=
      $enrollment_dat e; ?]']
      >
      Like that? Am I missing something?- Hide quoted text -
      >
      - Show quoted text -
      Thanks for the reply but what I want is like this (hypothetically ):

      Student name [Peter] Enrolled? [] Date Enrolled [ ]
      Student name [John ] Enrolled? [] Date Enrolled [ ]

      Then applying the loop to insert the two records. Hope this makes
      sense..

      Comment

      • SterLo

        #4
        Re: Getting two variables from a PHP loop

        I am not exactly following your logic.

        So if you check the Enrolled check box it should store in the database
        the date that it was checked...
        Do you want the date to appear automatically next to the check box
        once it is checked?
        If so maybe you should look into Javascript.
        If you want to access database information without reloading the page
        then use AJAX.

        I hope I am not too far off.

        Comment

        • Floortje

          #5
          Re: Getting two variables from a PHP loop

          Thanks for the reply but what I want is like this (hypothetically ):
          >
          Student name [Peter] Enrolled? [] Date Enrolled [ ]
          Student name [John ] Enrolled? [] Date Enrolled [ ]
          Iirc this works. didn't test it cause im lazy :-)

          <input type="text" name="person[1][enrolled]">
          <input type="text" name="person[1][date]">
          <input type="text" name="person[2][enrolled]">
          <input type="text" name="person[2][date]">

          foreach ($_POST['person'] as $id=>$person)
          {
          $sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
          date='".$person['date']."' WHERE id = '$person'";
          // etc
          }

          --
          Arjen
          www.arjenkarel.nl -- my crappy excuse for a website (just gathering dust)

          Comment

          • Floortje

            #6
            Re: Getting two variables from a PHP loop

            Floortje wrote:

            Im an idiot :-)
            $sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
            date='".$person['date']."' WHERE id = '$id'";
            --
            Arjen
            www.arjenkarel.nl -- my crappy excuse for a website (just gathering dust)

            Comment

            • chimambo@googlemail.com

              #7
              Re: Getting two variables from a PHP loop

              On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
              Floortje wrote:
              >
              Im an idiot :-)
              >
              $sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
              date='".$person['date']."' WHERE id = '$id'";
              >
              --
              Arjenwww.arjenk arel.nl-- my crappy excuse for a website (just gathering dust)
              Still not inserting the values. Maybe let me get an exact extract from
              my code:


              <?php require_once('./Connections/irdb.php'); ?>
              <?php
              $hos_no = $_GET["h_no"];

              $currentPage = $_SERVER["PHP_SELF"];

              $maxRows_zz = 10;
              $pageNum_zz = 0;
              if (isset($_GET['pageNum_zz']))
              {
              $pageNum_zz = $_GET['pageNum_zz'];
              }
              $startRow_zz = $pageNum_zz * $maxRows_zz;

              mysql_select_db ($database_irdb , $irdb);
              $query_sl = "SELECT fname, sname FROM irdb_patients WHERE
              irdb_patients.h ospital_number ='$hos_no'";
              $sl = mysql_query($qu ery_sl, $irdb) or die(mysql_error ());
              $row_sl = mysql_fetch_ass oc($sl);

              ?>

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
              www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
              <html xmlns="http://www.w3.org/1999/xhtml">

              <head>
              <meta http-equiv="Content-Type" content="text/html;
              charset=windows-1252">
              <title>Medicati ons</title>

              </head>
              <body>
              <table align='left'>
              <font face="Verdana" size="5"><b>Oth er Medications
              </b></font>
              </table>

              <br>
              <br>
              <br>
              <table border="0" width="100%" cellspacing="1" >
              <tr>
              <td width="594"><b> <font face="Verdana"> Patient Name:&nbsp;<?ph p
              echo $row_sl['fname'] ?>&nbsp;&nbsp;& nbsp;&nbsp;<?ph p echo
              $row_sl['sname'] ?>
              </font </b </td>
              </tr>
              </table>


              <?php
              $today = date('Y-m-d');
              echo "Date: ";
              echo date('d-m-Y', strtotime($toda y));


              while (list($key, $val) = @each ($box))
              {
              echo "$val";


              while (list($key, $v_val) = @each ($dose))
              {

              echo "$v_val";
              }


              }

              $mysql_query="i nsert into x_tbl (a, b, c) "."values ('$v_val, $v_val,
              $today) ";
              mysql_query($my sql_query) or die ('Error Insertion into Other Co
              Morbidities Failed');


              }


              echo "<form method=post action=''>";
              echo "<table border='0' cellspacing='0' style='border-collapse:
              collapse' width='100' >

              <tr bgcolor='white' >
              <td width='25%'><in put type=checkbox name=box[] value='Adalimam ub'></
              td>
              <td width='13%'>&nb sp;Adalimamub</td>
              <td width='12%'><in put type=text name=dose[] size=8></td>
              <td width='25%'><in put type=checkbox name=box[] value='Azathiop rine'></
              td>
              <td width='13%'>&nb sp;Azathioprine </td>
              <td width='12%'><in put type=text name=dose[] size=8></td>
              <td width='25%'><in put type=checkbox name=box[]
              value='Bendrofl uazide'></td>
              <td width='13%'>&nb sp;Bendrofluazi de</td>
              <td width='12%'><in put type=text name=dose[] size=8></td>
              <td width='25%'><in put type=checkbox name=box[] value='Calcium' ></td>
              <td width='13%'>&nb sp;Calcium</td>
              <td width='12%'><in put type=text name=dose[] size=8></td>

              </table>";

              ?><body bgcolor="white" >

              <b><font face="Verdana" size="2" color="#FF0000" >**Please ensure
              that the medications are correct before saving otherwise go back to
              previous page.</font></b>

              </body>

              </html>

              Comment

              • Floortje

                #8
                Re: Getting two variables from a PHP loop

                chimambo@google mail.com schreef:
                On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
                >Floortje wrote:
                >>
                >Im an idiot :-)
                >>
                >>$sql="UPDAT E $db SET enrolled='".$pe rson['enrolled']."',
                >>date='".$pers on['date']."' WHERE id = '$id'";
                >--
                >Arjenwww.arjen karel.nl-- my crappy excuse for a website (just gathering dust)
                >
                Still not inserting the values. Maybe let me get an exact extract from
                my code:
                The code shoudl work after a liitle modification.

                Try var_dump($_POST ) and see what you get

                --
                Arjen
                HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

                Comment

                • chimambo@googlemail.com

                  #9
                  Re: Getting two variables from a PHP loop

                  On 19 May, 10:21, Floortje <l...@zingmaarm etmijmee.enelwr ote:
                  chima...@google mail.com schreef:On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
                  Floortje wrote:
                  >
                  Im an idiot :-)
                  >
                  >$sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
                  >date='".$perso n['date']."' WHERE id = '$id'";
                  --
                  Arjenwww.arjenk arel.nl--my crappy excuse for a website (just gathering dust)
                  >
                  Still not inserting the values. Maybe let me get an exact extract from
                  my code:
                  >
                  The code shoudl work after a liitle modification.
                  >
                  Try var_dump($_POST ) and see what you get
                  >
                  --
                  Arjenhttp://www.hondenpage. com

                  I have tried but to no avail. I am able to get the values of the text
                  box. But what I am not sure is how to handle the checkbox values. You
                  suggested:

                  <input type="text" name="person[1][enrolled]">
                  <input type="text" name="person[1][date]">
                  <input type="text" name="person[2][enrolled]">
                  <input type="text" name="person[2][date]">

                  But what I actually need is:

                  <input type=checkbox name=box[] value="X"-- Supposed to insert "X"
                  if checked
                  <input type="text" name="person[1][date]"-- Deals with variable
                  dates per corresponding checkbox
                  <input type=checkbox name=box[] value="Y"-- Supposed to insert "Y"
                  if checked
                  <input type="text" name="person[2][date]"-- Deals with variable
                  dates per corresponding checkbox


                  Comment

                  Working...