break apart a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Auddog

    break apart a variable

    I would like to know how I can break apart a variable. I'm currently
    passing the variable $id-hours and I would like to break it down. I would
    like to break down to something similar to:

    $eid = id (id part of $id-hours)
    $ehour = hours (hours part of $id-hours)

    Is this possible? I'm new to programming and just trying to get thru it
    all. Thanks for any help that you may be able to provide.

    A


  • Rik

    #2
    Re: break apart a variable

    Auddog <will_k@hotmail .comwrote:
    I would like to know how I can break apart a variable. I'm currently
    passing the variable $id-hours and I would like to break it down. I
    would
    like to break down to something similar to:
    >
    $eid = id (id part of $id-hours)
    $ehour = hours (hours part of $id-hours)
    How is $id-hours formatted?
    Is this possible? I'm new to programming and just trying to get thru it
    all. Thanks for any help that you may be able to provide.
    Several functions come to mind. You might want to use one of these:
    - explode()
    - fscanf()
    - split()
    - preg_match()

    Not much else I can say without knowing the format.

    Also, why do you have this in 1 variable? More logical would be:

    $var = array('id'='you r_id','hours' ='your_hours', 'foo' ='bar');
    --
    Rik Wasmus

    Comment

    • Jerry Stuckle

      #3
      Re: break apart a variable

      Auddog wrote:
      I would like to know how I can break apart a variable. I'm currently
      passing the variable $id-hours and I would like to break it down. I would
      like to break down to something similar to:
      >
      $eid = id (id part of $id-hours)
      $ehour = hours (hours part of $id-hours)
      >
      Is this possible? I'm new to programming and just trying to get thru it
      all. Thanks for any help that you may be able to provide.
      >
      A
      >
      >
      Not easily. When you pass a variable to a function the name of the
      variable changes.

      But why would you need to, anyway? Variable names are just there for
      human readability; they should be meaningful to the programmer, but the
      computer doesn't care.

      And BTW - $id-hours is an invalid variable name. The hyphen isn't valid
      in a variable name.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Auddog

        #4
        Re: break apart a variable

        My form is created by querying my database for id, fname, lname of employees
        and then I add an input box for hours worked. I want everyone that works on
        the production lines listed. I then would like to be able to insert the
        form information into a database. I just don't know how to do this. I'm
        grasping at straws right now.

        Here is my form code:
        <?php

        //create query
        $query = "SELECT id, fname, lname FROM prod_employee where active = 'yes'";

        //excute query
        $result = mysqli_query($c onnection, $query) or die ("Error in query: $query.
        ".mysqli_error( ));

        // see if any rows were returned
        if (mysqli_fetch_a rray($result) 0) {
        // yes
        // print them one after another
        while (list($id, $fname, $lname) = mysqli_fetch_ro w($result))
        {
        echo " <tr>
        <td><div align=center>$i d</div></td>
        <td>$fname</td>
        <td>$lname</td>
        <td><input name=$id-hours type=text size=4 maxlength=4 />
        </tr>";
        }
        echo "</table>";
        }
        else {
        // no
        // print status message
        echo "No rows found!";
        }

        Again thanks for any help that you can provide.

        A


        "Rik" <luiheidsgoeroe @hotmail.comwro te in message
        news:op.tndy6vf 5qnv3q9@misant. kabel.utwente.n l...
        Auddog <will_k@hotmail .comwrote:
        I would like to know how I can break apart a variable. I'm currently
        passing the variable $id-hours and I would like to break it down. I
        would
        like to break down to something similar to:
        >
        $eid = id (id part of $id-hours)
        $ehour = hours (hours part of $id-hours)
        How is $id-hours formatted?
        Is this possible? I'm new to programming and just trying to get thru it
        all. Thanks for any help that you may be able to provide.
        Several functions come to mind. You might want to use one of these:
        - explode()
        - fscanf()
        - split()
        - preg_match()

        Not much else I can say without knowing the format.

        Also, why do you have this in 1 variable? More logical would be:

        $var = array('id'='you r_id','hours' ='your_hours', 'foo' ='bar');
        --
        Rik Wasmus


        Comment

        • Rik

          #5
          Re: break apart a variable

          Auddog <will_k@hotmail .comwrote:
          My form is created by querying my database for id, fname, lname of
          employees
          and then I add an input box for hours worked. I want everyone that
          works on
          the production lines listed. I then would like to be able to insert the
          form information into a database. I just don't know how to do this. I'm
          grasping at straws right now.
          >
          Here is my form code:
          <?php
          >
          // see if any rows were returned
          if (mysqli_fetch_a rray($result) 0) {
          This would mean you always discard the first row, use
          mysqli_num_rows ($result) instead.

          while (list($id, $fname, $lname) = mysqli_fetch_ro w($result))
          {
          echo " <tr>
          <td><input name=$id-hours type=text size=4 maxlength=4 />
          </tr>";
          }
          echo "</table>";
          }
          I'd do this:

          echo '<input name="hours['.$id.']" type="text" size="4" maxlength="4" >';

          And upon receiving, you'll have an array in $_POST (or $_GET, but don't do
          that) named 'hours' containing the id as key and the value as the input.
          Check this with print_r($_POST['hours']);

          To keep using the current form, you'd have to do something like this:

          $hours = array();
          foreach($_POST as $key =$value){
          if(preg_match('/^[0-9]+-hours$/i',$key){
          $keyarray = explode('-',$key);
          $hours[$keyarray[0]] = $value;
          }
          }
          print_r($hours) ;
          --
          Rik Wasmus

          Comment

          • Auddog

            #6
            Re: break apart a variable

            Rik,

            Thanks for the feed back and here is what I got from it. By placing the [ ]
            around the $id (what's the periods for???) it will create an array for
            $hours. What is the key? I see you assign the id as it's value, is that
            html or php? Lastly, when I execute the code all I get on the screen is
            Array ( ). I can place all my code here if it would help. Thanks again for
            all your help, it is greatly appreciated.

            A

            Sorry for all the questions, but I want to understand what each line is
            doing so that I can get this figured out.


            "Rik" <luiheidsgoeroe @hotmail.comwro te in message
            news:op.tnd3qml wqnv3q9@misant. kabel.utwente.n l...
            Auddog <will_k@hotmail .comwrote:
            My form is created by querying my database for id, fname, lname of
            employees
            and then I add an input box for hours worked. I want everyone that works
            on
            the production lines listed. I then would like to be able to insert the
            form information into a database. I just don't know how to do this. I'm
            grasping at straws right now.
            >
            Here is my form code:
            <?php
            >
            // see if any rows were returned
            if (mysqli_fetch_a rray($result) 0) {
            This would mean you always discard the first row, use
            mysqli_num_rows ($result) instead.

            while (list($id, $fname, $lname) = mysqli_fetch_ro w($result))
            {
            echo " <tr>
            <td><input name=$id-hours type=text size=4 maxlength=4 />
            </tr>";
            }
            echo "</table>";
            }
            I'd do this:

            echo '<input name="hours['.$id.']" type="text" size="4" maxlength="4" >';

            And upon receiving, you'll have an array in $_POST (or $_GET, but don't do
            that) named 'hours' containing the id as key and the value as the input.
            Check this with print_r($_POST['hours']);

            To keep using the current form, you'd have to do something like this:

            $hours = array();
            foreach($_POST as $key =$value){
            if(preg_match('/^[0-9]+-hours$/i',$key){
            $keyarray = explode('-',$key);
            $hours[$keyarray[0]] = $value;
            }
            }
            print_r($hours) ;
            --
            Rik Wasmus


            Comment

            • Rik

              #7
              Re: break apart a variable

              Auddog <will_k@hotmail .comwrote:
              Rik,
              >
              Thanks for the feed back and here is what I got from it. By placing the
              [ ]
              around the $id (what's the periods for???) it will create an array for
              $hours.
              Nonono.

              By creating the _string_ with the id in it, you html output with for
              instance id = 12 would looke like:

              <input name="hours[12]" value="somevalu e">

              Which on return will be interpreted by PHP as:

              _POST Array
              (
              'hours' =Array
              (
              12 ='somevalue'
              )
              )

              --
              Rik Wasmus

              Comment

              • Auddog

                #8
                Re: break apart a variable

                Here is what my input looks like from my browser (view page source):

                <input name=hours['.3.'] type=text size=4 maxlength=4 >

                Is that the same? I'm sorry for being slow at getting this.

                A


                "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                news:op.tnd8n4z xqnv3q9@misant. kabel.utwente.n l...
                Auddog <will_k@hotmail .comwrote:
                Rik,
                >
                Thanks for the feed back and here is what I got from it. By placing the
                [ ]
                around the $id (what's the periods for???) it will create an array for
                $hours.
                Nonono.

                By creating the _string_ with the id in it, you html output with for
                instance id = 12 would looke like:

                <input name="hours[12]" value="somevalu e">

                Which on return will be interpreted by PHP as:

                _POST Array
                (
                'hours' =Array
                (
                12 ='somevalue'
                )
                )

                --
                Rik Wasmus


                Comment

                • Rik

                  #9
                  Re: break apart a variable

                  Auddog <will_k@hotmail .comwrote:
                  Here is what my input looks like from my browser (view page source):
                  >
                  <input name=hours['.3.'] type=text size=4 maxlength=4 >
                  >
                  Is that the same? I'm sorry for being slow at getting this.
                  No, it is not. I gave you the code. Copy/paste it if need be.

                  --
                  Rik Wasmus

                  Comment

                  • Auddog

                    #10
                    Re: break apart a variable

                    OK, I see the errors of my way. I now get :

                    <input name="hours[20]" type="text" size="4" maxlength="4">

                    I cut and pasted you code into the top part of my page - here is the code:

                    <?php

                    if (isset($_POST['submitted'])) {

                    $hours = array();
                    foreach($_POST as $key =$value){
                    if(preg_match('/^[0-9]+-hours$/i',$key)){
                    $keyarray = explode('-',$key);
                    $hours[$keyarray[0]] = $value;
                    }
                    }
                    print_r($hours) ;


                    //if - else statement isset
                    } else {

                    ?>

                    When I run this all I get is array ( ) - is there something else I'm
                    missing.



                    "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                    news:op.tnd93ac 9qnv3q9@misant. kabel.utwente.n l...
                    Auddog <will_k@hotmail .comwrote:
                    Here is what my input looks like from my browser (view page source):
                    >
                    <input name=hours['.3.'] type=text size=4 maxlength=4 >
                    >
                    Is that the same? I'm sorry for being slow at getting this.
                    No, it is not. I gave you the code. Copy/paste it if need be.

                    --
                    Rik Wasmus


                    Comment

                    • Rik

                      #11
                      Re: break apart a variable

                      Auddog <will_k@hotmail .comwrote:
                      When I run this all I get is array ( ) - is there something else I'm
                      missing.
                      Yes, before that piece of code I said:"To keep using the current form".
                      This , to me, would mean fully spelled out: "If you do not want to change
                      your form like this, and keep on using the form you allready have, then I
                      suggest the following approach.

                      In short: I gave to answers: one to alter the form and have an easier time
                      to process it, one how to process the current form you have now.

                      Please try to read answers more carefully, and when something doesn't
                      work, reread them to make sure you did not miss anything.
                      --
                      Rik Wasmus

                      Comment

                      • Curtis

                        #12
                        Re: break apart a variable

                        Auddog wrote:
                        "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                        news:op.tnd93ac 9qnv3q9@misant. kabel.utwente.n l...
                        >Auddog <will_k@hotmail .comwrote:
                        >>
                        >Here is what my input looks like from my browser (view page source):
                        >>
                        ><input name=hours['.3.'] type=text size=4 maxlength=4 >
                        >>
                        >Is that the same? I'm sorry for being slow at getting this.
                        >>
                        >No, it is not. I gave you the code. Copy/paste it if need be.
                        >>
                        >
                        OK, I see the errors of my way. I now get :
                        >
                        <input name="hours[20]" type="text" size="4" maxlength="4">
                        >
                        I cut and pasted you code into the top part of my page - here is the code:
                        >
                        <?php
                        >
                        if (isset($_POST['submitted'])) {
                        >
                        $hours = array();
                        foreach($_POST as $key =$value){
                        if(preg_match('/^[0-9]+-hours$/i',$key)){
                        $keyarray = explode('-',$key);
                        $hours[$keyarray[0]] = $value;
                        }
                        }
                        print_r($hours) ;
                        >
                        >
                        //if - else statement isset
                        } else {
                        >
                        ?>
                        >
                        When I run this all I get is array ( ) - is there something else I'm
                        missing.
                        Let's see what you get, alter Rik's code and post the output back to
                        us (if it repeats a lot, just post enough to get a look at what the
                        keys look like):

                        $hours = array();

                        echo '<pre>';
                        print_r($_POST) ; // dump all submitted POST data
                        echo '</pre><br>';

                        foreach($_POST as $key =$value){
                        if(preg_match('/^[0-9]+-hours$/i',$key)){
                        $keyarray = explode('-',$key);
                        $hours[$keyarray[0]] = $value;
                        }
                        }
                        print_r($hours) ;

                        Comment

                        • Rik

                          #13
                          Re: break apart a variable

                          Curtis <zer0dyer@veriz on.netwrote:
                          Let's see what you get, alter Rik's code and post the output back to
                          us (if it repeats a lot, just post enough to get a look at what the
                          keys look like):
                          >
                          $hours = array();
                          >
                          echo '<pre>';
                          print_r($_POST) ; // dump all submitted POST data
                          echo '</pre><br>';
                          >
                          foreach($_POST as $key =$value){
                          if(preg_match('/^[0-9]+-hours$/i',$key)){
                          The would only be usefull if the postfields were still in the 'id-hours'
                          format. It's now in 'hours[id]'.

                          So just print_r($_POST['hours']);
                          --
                          Rik Wasmus

                          Comment

                          • Auddog

                            #14
                            Re: break apart a variable

                            I went back and did as you told me. I was assuming the second option was
                            what was happening after you hit the submit button. I apologize for not
                            following the instructions,. I guess I'm biting off more than I know with
                            this script.

                            I was able to change my page and I did start getting the results: Array (
                            [1] =8 [2] =[3] =[4] =[5] =8 ETC. I'm still curious as to how the
                            =got into the mix, but I'll do some reading to figure that out. Now that
                            I'm getting the something, maybe I'll be able to get it into my database.
                            Thanks again for all your time and help.

                            A


                            "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                            news:op.tneon22 kqnv3q9@misant. kabel.utwente.n l...
                            Curtis <zer0dyer@veriz on.netwrote:
                            Let's see what you get, alter Rik's code and post the output back to
                            us (if it repeats a lot, just post enough to get a look at what the
                            keys look like):
                            >
                            $hours = array();
                            >
                            echo '<pre>';
                            print_r($_POST) ; // dump all submitted POST data
                            echo '</pre><br>';
                            >
                            foreach($_POST as $key =$value){
                            if(preg_match('/^[0-9]+-hours$/i',$key)){
                            The would only be usefull if the postfields were still in the 'id-hours'
                            format. It's now in 'hours[id]'.

                            So just print_r($_POST['hours']);
                            --
                            Rik Wasmus


                            Comment

                            Working...