Storing array in a row

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

    #1

    Storing array in a row

    Hey everyone,

    I run a site with staff-submitted reviews, and most of them are written
    by one author. However, we also do "multiple" reviews. Up until now I
    just had a userid for a 'Multiple' account and submitted them under
    that, but this makes it harder to print lists of all the reviews by one
    person, so ideally I wanna make a multiple select form so we can just
    select all the contributors and have the values saved in the database -
    in one row (something like "56,34,21" etc).

    I've coded a multiple select form with all the authors of articles for
    my site on, and I want to store the results of this in one row so I can
    grab them as an array later - how can I go about doing this? I just
    googled and came across using <select name="author[]"> in my HTML, and
    then using serialize() to process it, but it fails. When I run the
    following code, the value for 'multiplearray' remains at 0 whether I
    select single OR multiple values on the form:

    <?php
    $dbhost = ###
    $dbuser = ###
    $dbpass = ###
    $dbname = ###

    $conn = mysql_connect($ dbhost, $dbuser, $dbpass) or die ('Error
    connecting to mysql');
    mysql_select_db ($dbname);

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

    $author = serialize($_POS T['author']);

    $query = "INSERT INTO review_test (multiplearray) VALUES
    ('$author')";
    mysql_query($qu ery) or die('Error ,query failed');
    echo "Query successful!";
    }

    echo "<form name='test' method='post' action=''>";

    $query2 = "SELECT Staff_id, displayname FROM users WHERE
    userlevel !=0
    ORDER BY username";
    $result2 = mysql_query($qu ery2) or die('Error : ' .
    mysql_error());

    echo "<select name='author[]' id='author[]' multiple='multi ple'
    size='10'>";
    while($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
    {
    list($Staff_id, $displayname) = $row2;
    echo "<option value='$Staff_i d'>$displayname </option>";
    }

    echo "</select>";
    echo "<br><br><i nput name='save' type='submit' id='save' value='Post
    Review'>";
    echo "</form>";

    ?>


    -----

    Matt

  • Rik

    #2
    Re: Storing array in a row

    guitarromantic@ gmail.com wrote:[color=blue]
    > I wanna make a multiple select form
    > so we can just select all the contributors and have the values saved
    > in the database - in one row (something like "56,34,21" etc).[/color]

    $author = implode(',',$_P OST['author']);

    And if you'd need to extract it all again:

    $authors = explode(',',$au thor);
    (assuming there is no ',' in the $Staff_id)

    Allthough, if it becomes a common habit, maybe an extra table will be
    preferred. It would make handling data on the database side somewhat easier.

    If this doesn't work:
    1. Did you deribately set action='' in your form?
    2. print_r($_POST) , does your $_POST['author'] contain the values?

    Grtz,
    --
    Rik Wasmus


    Comment

    • guitarromantic@gmail.com

      #3
      Re: Storing array in a row

      Rik wrote:
      [color=blue]
      > $author = implode(',',$_P OST['author']);
      >
      > And if you'd need to extract it all again:
      >
      > $authors = explode(',',$au thor);
      > (assuming there is no ',' in the $Staff_id)[/color]

      Thankyou for this, very helpful.
      [color=blue]
      > Allthough, if it becomes a common habit, maybe an extra table will be
      > preferred. It would make handling data on the database side somewhat easier.[/color]

      Out of a database of about 780 reviews, 65 are multiple authors. I
      think it'd be more of a headache to work the other way (plus I can't
      really get my head around how I'd have to restructure the database).
      [color=blue]
      > If this doesn't work:
      > 1. Did you deribately set action='' in your form?
      > 2. print_r($_POST) , does your $_POST['author'] contain the values?[/color]

      1. Nope, bad coding on my part. Removed.
      2. (It didn't work) Yeah, it's got the values, and the one that ends up
      in the db is author[0]. Is there anything special I need to do on the
      database field for this data? I just have it as a standard int
      currently.

      Matt

      Comment

      • guitarromantic@gmail.com

        #4
        Re: Storing array in a row

        Oops, just occured to me that I can't store a comma in an int field..
        changed to varchar, worked a charm, thanks!

        Do you think this approach is still viable for my site, though? If
        there's a more efficient/professional way, I'm interested.

        Comment

        • Jerry Stuckle

          #5
          Re: Storing array in a row

          guitarromantic@ gmail.com wrote:[color=blue]
          > Hey everyone,
          >
          > I run a site with staff-submitted reviews, and most of them are written
          > by one author. However, we also do "multiple" reviews. Up until now I
          > just had a userid for a 'Multiple' account and submitted them under
          > that, but this makes it harder to print lists of all the reviews by one
          > person, so ideally I wanna make a multiple select form so we can just
          > select all the contributors and have the values saved in the database -
          > in one row (something like "56,34,21" etc).
          >
          > I've coded a multiple select form with all the authors of articles for
          > my site on, and I want to store the results of this in one row so I can
          > grab them as an array later - how can I go about doing this? I just
          > googled and came across using <select name="author[]"> in my HTML, and
          > then using serialize() to process it, but it fails. When I run the
          > following code, the value for 'multiplearray' remains at 0 whether I
          > select single OR multiple values on the form:
          >
          > <?php
          > $dbhost = ###
          > $dbuser = ###
          > $dbpass = ###
          > $dbname = ###
          >
          > $conn = mysql_connect($ dbhost, $dbuser, $dbpass) or die ('Error
          > connecting to mysql');
          > mysql_select_db ($dbname);
          >
          > if(isset($_POST['save']))
          > {
          >
          > $author = serialize($_POS T['author']);
          >
          > $query = "INSERT INTO review_test (multiplearray) VALUES
          > ('$author')";
          > mysql_query($qu ery) or die('Error ,query failed');
          > echo "Query successful!";
          > }
          >
          > echo "<form name='test' method='post' action=''>";
          >
          > $query2 = "SELECT Staff_id, displayname FROM users WHERE
          > userlevel !=0
          > ORDER BY username";
          > $result2 = mysql_query($qu ery2) or die('Error : ' .
          > mysql_error());
          >
          > echo "<select name='author[]' id='author[]' multiple='multi ple'
          > size='10'>";
          > while($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
          > {
          > list($Staff_id, $displayname) = $row2;
          > echo "<option value='$Staff_i d'>$displayname </option>";
          > }
          >
          > echo "</select>";
          > echo "<br><br><i nput name='save' type='submit' id='save' value='Post
          > Review'>";
          > echo "</form>";
          >
          > ?>
          >
          >
          > -----
          >
          > Matt
          >[/color]

          Matt,

          Another table is definitely the way to go. Conistst of two columns - testid and
          reviewerid - plus anything else you might have specific to that reviewer on that
          test.

          Do some searches on "Database Normalization". It will definitely help you much
          more in the long run.


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

          Comment

          • Rik

            #6
            Re: Storing array in a row

            guitarromantic@ gmail.com wrote:[color=blue]
            > Oops, just occured to me that I can't store a comma in an int field..
            > changed to varchar, worked a charm, thanks!
            >
            > Do you think this approach is still viable for my site, though? If
            > there's a more efficient/professional way, I'm interested.[/color]

            An extra table would help to make the data more accessable.

            TABLE reviews
            review_id
            some_other_fiel ds

            TABLE staff
            staff_id
            some_other_fiel ds

            TABLE staff_reviews
            review_id
            staff_id

            That way cou can also keep the id's all integers, which would be a lot
            faster then searching a varchar for a number that may or may nog be between
            comma's.

            To keep integrity intact I'd advice using InnoDB tables:
            http://dev.mysql.com/doc/refman/5.0/...nstraints.html
            Unless there is a lot of adding and deleting, then using InnoDB might slow
            it down.

            Grtz,
            --
            Rik Wasmus


            Comment

            • guitarromantic@gmail.com

              #7
              Re: Storing array in a row

              Rik wrote:
              [color=blue]
              > An extra table would help to make the data more accessable.
              >
              > TABLE reviews
              > review_id
              > some_other_fiel ds
              >
              > TABLE staff
              > staff_id
              > some_other_fiel ds
              >
              > TABLE staff_reviews
              > review_id
              > staff_id[/color]

              Just to clarify - so when I display a review, would I have to then
              search staff_reviews for $foo (review id) and then select all the
              associated staff_ids to then display my links and stuff?

              I think this is a better method also because as well as each review
              having multiple authors, it has multiple scores too (currently we just
              write the individual scores in the body of the review and store the
              average score in the db - with this system I can store a score for each
              review in staff_reviews and then just use php to display the average,
              right?

              Comment

              • Good Man

                #8
                Re: Storing array in a row

                Jerry Stuckle <jstucklex@attg lobal.net> wrote in
                news:MdidneFRIe WFJv_ZRVn-pA@comcast.com:
                [color=blue]
                > guitarromantic@ gmail.com wrote:[color=green]
                >> Hey everyone,
                >>
                >> I run a site with staff-submitted reviews, and most of them are
                >> written by one author. However, we also do "multiple" reviews. Up
                >> until now I just had a userid for a 'Multiple' account and submitted
                >> them under that, but this makes it harder to print lists of all the
                >> reviews by one person, so ideally I wanna make a multiple select form
                >> so we can just select all the contributors and have the values saved
                >> in the database - in one row (something like "56,34,21" etc).[/color][/color]
                [color=blue]
                > Another table is definitely the way to go. Conistst of two columns -
                > testid and reviewerid - plus anything else you might have specific to
                > that reviewer on that test.[/color]

                Triple recommended. Basically, follow this rule:

                If the data you want to store is a list of values seperated by commas (or
                any other delimiter), 99% of the time it is ALWAYS better to create
                another table (a many-to-many table).

                Comment

                • guitarromantic@gmail.com

                  #9
                  Re: Storing array in a row

                  Good Man wrote:
                  [color=blue]
                  > Triple recommended. Basically, follow this rule:
                  >
                  > If the data you want to store is a list of values seperated by commas (or
                  > any other delimiter), 99% of the time it is ALWAYS better to create
                  > another table (a many-to-many table).[/color]

                  Thank you. So is my previous post a correct assumption?

                  Comment

                  • Rik

                    #10
                    Re: Storing array in a row

                    guitarromantic@ gmail.com wrote:[color=blue]
                    > Rik wrote:
                    >[color=green]
                    >> An extra table would help to make the data more accessable.
                    >>
                    >> TABLE reviews
                    >> review_id
                    >> some_other_fiel ds
                    >>
                    >> TABLE staff
                    >> staff_id
                    >> some_other_fiel ds
                    >>
                    >> TABLE staff_reviews
                    >> review_id
                    >> staff_id[/color]
                    >
                    > Just to clarify - so when I display a review, would I have to then
                    > search staff_reviews for $foo (review id) and then select all the
                    > associated staff_ids to then display my links and stuff?
                    >
                    > I think this is a better method also because as well as each review
                    > having multiple authors, it has multiple scores too (currently we just
                    > write the individual scores in the body of the review and store the
                    > average score in the db - with this system I can store a score for
                    > each review in staff_reviews and then just use php to display the
                    > average, right?[/color]

                    Yup, just add the review_score field to staff_revies. You could even let
                    MySQL take care of the average.

                    Grtz,
                    --
                    Rik Wasmus


                    Comment

                    • Jerry Stuckle

                      #11
                      Re: Storing array in a row

                      guitarromantic@ gmail.com wrote:[color=blue]
                      > Rik wrote:
                      >
                      >[color=green]
                      >> An extra table would help to make the data more accessable.
                      >>
                      >>TABLE reviews
                      >>review_id
                      >>some_other_fi elds
                      >>
                      >>TABLE staff
                      >>staff_id
                      >>some_other_fi elds
                      >>
                      >>TABLE staff_reviews
                      >>review_id
                      >>staff_id[/color]
                      >
                      >
                      > Just to clarify - so when I display a review, would I have to then
                      > search staff_reviews for $foo (review id) and then select all the
                      > associated staff_ids to then display my links and stuff?
                      >
                      > I think this is a better method also because as well as each review
                      > having multiple authors, it has multiple scores too (currently we just
                      > write the individual scores in the body of the review and store the
                      > average score in the db - with this system I can store a score for each
                      > review in staff_reviews and then just use php to display the average,
                      > right?
                      >[/color]

                      Yes, you would. But it will be much easier and more flexible than trying to
                      keep a list of comma separated values.

                      And think of the future. For instance, what if you want to add a staff member
                      to a review? In your method, you have to read the current comma separated list,
                      add the reviewer to the end and put it back. Or if you mistakenly added one and
                      need to delete it, you have to read, remove and rewrite. With a separate table
                      it's a matter of insert or delete, and that's all.

                      Also, what if you want to find a list of all articles reviewed by a certain
                      staff member? Much more difficult with your setup.

                      When you have a multi-multi link like this, it's virtually certain a separate
                      table is the way to go.


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

                      Comment

                      • guitarromantic@gmail.com

                        #12
                        Re: Storing array in a row

                        Thanks for the tips everyone, I've been experimenting.

                        Following this tutorial:


                        I've made a several-step form to simplify the submissions.

                        Page 1: User selects the relevant authors from a list box.
                        Page 2: A form is populated for each item in the author array, like so:

                        "Review for author $author"
                        [textarea for review]

                        I then pass these values to the final page (as the Zend tutorial
                        suggests, with the fieldforwarder class). My issue then is that I want
                        to use their code but I'm using an array within an array:

                        foreach ($author as $value) {
                        echo "Review for author $value<br>\n";
                        echo "<textarea name=\"review_$ value\" cols=\"32\"
                        rows=\"6\"></textarea><br>\n ";
                        }

                        The code the Zend tutorial gives for the final page is like this:

                        $review = htmlentities($_ POST['cust_name']);

                        How can I grab the values for the reviews placed in the textarea using
                        this method?

                        Comment

                        • Jerry Stuckle

                          #13
                          Re: Storing array in a row

                          guitarromantic@ gmail.com wrote:[color=blue]
                          > Thanks for the tips everyone, I've been experimenting.
                          >
                          > Following this tutorial:
                          > http://www.zend.com/zend/spotlight/c...lery-wade7.php
                          >
                          > I've made a several-step form to simplify the submissions.
                          >
                          > Page 1: User selects the relevant authors from a list box.
                          > Page 2: A form is populated for each item in the author array, like so:
                          >
                          > "Review for author $author"
                          > [textarea for review]
                          >
                          > I then pass these values to the final page (as the Zend tutorial
                          > suggests, with the fieldforwarder class). My issue then is that I want
                          > to use their code but I'm using an array within an array:
                          >
                          > foreach ($author as $value) {
                          > echo "Review for author $value<br>\n";
                          > echo "<textarea name=\"review_$ value\" cols=\"32\"
                          > rows=\"6\"></textarea><br>\n ";
                          > }
                          >
                          > The code the Zend tutorial gives for the final page is like this:
                          >
                          > $review = htmlentities($_ POST['cust_name']);
                          >
                          > How can I grab the values for the reviews placed in the textarea using
                          > this method?
                          >[/color]

                          Just like Zend tells you.

                          Whatever is the name of your text area becomes the key to the $_POST array. In
                          their case they had an input field named 'cust_name'.

                          Since you (probably) have multiple text areas, you can either give them unique
                          names such as 'comment_1', "comment_2' , etc. Or, a simpler approach would be to
                          name them all 'comment[]', in which case the $_POST['comment'] will be an array
                          with one element for each text area.


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

                          Comment

                          • guitarromantic@gmail.com

                            #14
                            Re: Storing array in a row

                            Jerry Stuckle wrote:
                            [color=blue]
                            > Since you (probably) have multiple text areas, you can either give them unique
                            > names such as 'comment_1', "comment_2' , etc. Or, a simpler approach would be to
                            > name them all 'comment[]', in which case the $_POST['comment'] will be an array
                            > with one element for each text area.[/color]

                            Ahh, I'd given them all unique names with the name of the author_id
                            attached, so I was wondering how to get that. I didn't think I could
                            give them all the same name, thanks for enlightening me!

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Storing array in a row

                              guitarromantic@ gmail.com wrote:[color=blue]
                              > Jerry Stuckle wrote:
                              >
                              >[color=green]
                              >>Since you (probably) have multiple text areas, you can either give them unique
                              >>names such as 'comment_1', "comment_2' , etc. Or, a simpler approach would be to
                              >>name them all 'comment[]', in which case the $_POST['comment'] will be an array
                              >>with one element for each text area.[/color]
                              >
                              >
                              > Ahh, I'd given them all unique names with the name of the author_id
                              > attached, so I was wondering how to get that. I didn't think I could
                              > give them all the same name, thanks for enlightening me!
                              >[/color]

                              Well, you can also get the name of all values from the $_POST array, i.e.

                              foreach ($_POST as $key->$value) {
                              ...

                              Or name then "text1", "text2", etc. and have another hidden variable with each
                              one such as "staff1", "staff2", etc. which contains the staff id.

                              Lots of ways to do it.

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

                              Comment

                              Working...