Storing array in a row

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

    #16
    Re: Storing array in a row

    Jerry Stuckle wrote:
    [color=blue]
    > Well, you can also get the name of all values from the $_POST array, i.e.
    >
    > foreach ($_POST as $key->$value) {[/color]

    Ahh, thanks again. One last question:

    When I post the final values to my database, I need to split them up.
    Say I enter 3 authors and 3 individual scores and reviews, I need to
    save them concurrently. What I've just done is coded a query to insert
    the data that applies to all the multiple-author reviews (eg, band
    name, record label, etc) into the main reviews table. I also assigned
    that row a "review_typ e" of '3' which tells me it's a multiple review.
    What I wanna do to display a review is test for type, if "3", then
    search in table multiple_review s for all occurences of whatever $id
    review I'm displaying.

    After my code to insert these common variables into the db, I then pull
    out the most recent $id entered into the reviews table, and store that
    as the $id for the new row being saved in multiple_review s, with the
    intention of storing like so:

    table: reviews

    id: 1234
    artist_name: The Beatles
    album_name: Let It Be
    record_label: Apple
    author_id: (left blank)
    review: (left blank)
    score: (left blank)
    review_type: 3

    table: multiple reviews

    id: 1234
    author_id: 34
    score: 7
    review: blah blah

    id: 1234
    author_id: 67
    score: 8
    review: blah blah BLAH

    etc. I hope that wasn't too insulting to your intelligence..

    Anyway, my issue is, for the INSERT to get that info into the second
    table, how can I grab each element of the array? As you've probably
    gathered, I'm new to php (I could probably do this in Java, though..)
    and all I've got so far is:

    foreach ($author as $value) {
    echo "$value";
    }

    which just prints out each element of the array. How can I make an
    insert query that'll populate the tables as outlined above (with the
    last three rows mentioned all being arrays)?

    Comment

    • Jerry Stuckle

      #17
      Re: Storing array in a row

      guitarromantic@ gmail.com wrote:[color=blue]
      > Jerry Stuckle wrote:
      >
      >[color=green]
      >>Well, you can also get the name of all values from the $_POST array, i.e.
      >>
      >> foreach ($_POST as $key->$value) {[/color]
      >
      >
      > Ahh, thanks again. One last question:
      >
      > When I post the final values to my database, I need to split them up.
      > Say I enter 3 authors and 3 individual scores and reviews, I need to
      > save them concurrently. What I've just done is coded a query to insert
      > the data that applies to all the multiple-author reviews (eg, band
      > name, record label, etc) into the main reviews table. I also assigned
      > that row a "review_typ e" of '3' which tells me it's a multiple review.
      > What I wanna do to display a review is test for type, if "3", then
      > search in table multiple_review s for all occurences of whatever $id
      > review I'm displaying.
      >
      > After my code to insert these common variables into the db, I then pull
      > out the most recent $id entered into the reviews table, and store that
      > as the $id for the new row being saved in multiple_review s, with the
      > intention of storing like so:
      >
      > table: reviews
      >
      > id: 1234
      > artist_name: The Beatles
      > album_name: Let It Be
      > record_label: Apple
      > author_id: (left blank)
      > review: (left blank)
      > score: (left blank)
      > review_type: 3
      >
      > table: multiple reviews
      >
      > id: 1234
      > author_id: 34
      > score: 7
      > review: blah blah
      >
      > id: 1234
      > author_id: 67
      > score: 8
      > review: blah blah BLAH
      >
      > etc. I hope that wasn't too insulting to your intelligence..
      >
      > Anyway, my issue is, for the INSERT to get that info into the second
      > table, how can I grab each element of the array? As you've probably
      > gathered, I'm new to php (I could probably do this in Java, though..)
      > and all I've got so far is:
      >
      > foreach ($author as $value) {
      > echo "$value";
      > }
      >
      > which just prints out each element of the array. How can I make an
      > insert query that'll populate the tables as outlined above (with the
      > last three rows mentioned all being arrays)?
      >[/color]

      First of all, don't store review_type. You can find out if it has multiple
      reviews by a simple

      SELECT COUNT(*) FROM multip_reviews WHERE id='1234'

      If the count is > 1, you have multiple reviews. Otherwise you need to ensure
      you keep this in sync. For instance, if you later add a review, you need to
      check to see what the current review_type is (it might be zero or 1 review) then
      update it.

      As for inserting multiple values, it's pretty simple. You need to build the
      query in your foreach loop. First you need to get all of your variables for a
      single review into an array, i.e. assuming you have your form fields as author1,
      author2, score1, score2 and review1, review2, etc.: (warning - not tested)

      $data = array();
      foreach($_POST as $key->$value) {
      if (substr($key, 0, 6) == 'author')
      $data[int(substr($key ,6))]['author'] = $value;
      elseif (substr($key, 0, 5) == 'score')
      $data[int(substr($key ,5))]['score'] = $value;
      elseif (substr($key, 0, 6) == 'review')
      $data[int(substr($key ,5))]['review'] = $value;
      }

      Your results will now be in $data[1]['author'], $data[1]['score'],
      $data[1]['review'], etc.

      Now that the data is sorted, you can build an insert VALUES clause ($id has the
      review id)

      $insert = '';
      foreach($data as $value) {
      if ($insert != '') // add a comma if not the first one
      $insert .= ', ';
      $insert .= "($id, $value['author'], $value['score'], '$value['review'])";
      }

      And finally you can insert into the table:

      $query = "INSERT INTO review_table(id , author_id, score, review) VALUES $insert;
      $result = mysql_query($qu ery);

      Not perfect, and I didn't do basic error checking (i.e. ensure all the values
      are filled in), but hopefully you get the idea.


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

      Comment

      • guitarromantic@gmail.com

        #18
        Re: Storing array in a row

        Jerry Stuckle wrote:
        [color=blue]
        > First of all, don't store review_type. You can find out if it has multiple
        > reviews by a simple
        >
        > SELECT COUNT(*) FROM multip_reviews WHERE id='1234'[/color]

        My reason for using review_type is that we already have a secondary
        review type (in this case it uses the same database structure, only the
        review body is part of our "200 words or less" feature, and I like to
        mark that visually on the review index).

        [color=blue]
        > As for inserting multiple values, it's pretty simple. You need to build the
        > query in your foreach loop. First you need to get all of your variables for a
        > single review into an array, i.e. assuming you have your form fields as author1,
        > author2, score1, score2 and review1, review2, etc.: (warning - not tested)[/color]

        Apologies if this is a stupid question, but would this method only
        allow 2 authors to be used when adding a review? Occasionally we use 3,
        there's no single number. Just so I can get my head around this, what
        are the numbers 5 and 6 doing in that code?
        [color=blue]
        > Now that the data is sorted, you can build an insert VALUES clause ($id has the
        > review id)
        >
        > $insert = '';
        > foreach($data as $value) {
        > if ($insert != '') // add a comma if not the first one
        > $insert .= ', ';
        > $insert .= "($id, $value['author'], $value['score'], '$value['review'])";[/color]

        Using this code I get a "Parse error: syntax error, unexpected
        T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or T_VARIABLE or
        T_NUM_STRING" on the final line of the above code. I took out the ' by
        $value, same result.

        Thanks again for all the help thus far, I'll have to credit you
        somewhere on the site (scenepointblan k.com) when this eventually goes
        live!

        Matt

        Comment

        • Jerry Stuckle

          #19
          Re: Storing array in a row

          guitarromantic@ gmail.com wrote:[color=blue]
          > Jerry Stuckle wrote:
          >
          >[color=green]
          >>First of all, don't store review_type. You can find out if it has multiple
          >>reviews by a simple
          >>
          >> SELECT COUNT(*) FROM multip_reviews WHERE id='1234'[/color]
          >
          >
          > My reason for using review_type is that we already have a secondary
          > review type (in this case it uses the same database structure, only the
          > review body is part of our "200 words or less" feature, and I like to
          > mark that visually on the review index).
          >[/color]

          Hmmm, it still isn't a good idea to duplicate information. What happens if for
          some reason it gets out of synchronization ? It does happen, for one reason or
          another, like someone modifying the code and not realizing what needs to be
          changed. Then you'll have other problems. And you can still mark it visually
          in the review index.
          [color=blue]
          >
          >[color=green]
          >>As for inserting multiple values, it's pretty simple. You need to build the
          >>query in your foreach loop. First you need to get all of your variables for a
          >>single review into an array, i.e. assuming you have your form fields as author1,
          >>author2, score1, score2 and review1, review2, etc.: (warning - not tested)[/color]
          >
          >
          > Apologies if this is a stupid question, but would this method only
          > allow 2 authors to be used when adding a review? Occasionally we use 3,
          > there's no single number. Just so I can get my head around this, what
          > are the numbers 5 and 6 doing in that code?
          >[/color]

          No, you just continue your numbering mechanism as many as you want. However, an
          alternative is to enter one review per page, submit it and come back to the same
          page for the next review. Then you don't have to worry about how many reviews
          to enter. But if you do submit several per page you'll need to validate the
          input (which you really need to do anyway).
          [color=blue]
          >[color=green]
          >>Now that the data is sorted, you can build an insert VALUES clause ($id has the
          >>review id)
          >>
          >> $insert = '';
          >> foreach($data as $value) {
          >> if ($insert != '') // add a comma if not the first one
          >> $insert .= ', ';
          >> $insert .= "($id, $value['author'], $value['score'], '$value['review'])";[/color]
          >
          >
          > Using this code I get a "Parse error: syntax error, unexpected
          > T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or T_VARIABLE or
          > T_NUM_STRING" on the final line of the above code. I took out the ' by
          > $value, same result.
          >[/color]

          As I said - untested and not guaranteed. And I don't guarantee its valid. But
          the problem is there should be another single quote after the last right square
          bracket.
          [color=blue]
          > Thanks again for all the help thus far, I'll have to credit you
          > somewhere on the site (scenepointblan k.com) when this eventually goes
          > live!
          >
          > Matt
          >[/color]


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

          Comment

          • guitarromantic@gmail.com

            #20
            Re: Storing array in a row

            Jerry Stuckle wrote:[color=blue]
            > As I said - untested and not guaranteed. And I don't guarantee its valid. But
            > the problem is there should be another single quote after the last right square
            > bracket.[/color]

            Sorry, I don't mean to sound like I expect everything to work 100%.
            That said, adding the missing ' didn't change the error message.

            Comment

            • Jerry Stuckle

              #21
              Re: Storing array in a row

              guitarromantic@ gmail.com wrote:[color=blue]
              > Jerry Stuckle wrote:
              >[color=green]
              >>As I said - untested and not guaranteed. And I don't guarantee its valid. But
              >>the problem is there should be another single quote after the last right square
              >>bracket.[/color]
              >
              >
              > Sorry, I don't mean to sound like I expect everything to work 100%.
              > That said, adding the missing ' didn't change the error message.
              >[/color]

              Well, I never was good at matching quotes and parens. And I shouldn't have
              tried to get fancy. The following should work:

              $insert .= "($id, " . $value['author'] . ', ' .
              $value['score'] . ", '" . $value['review'] . "')";

              And BTW you'll need a double quote (") before the semicolon on the $query= line.

              Sorry.

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

              Comment

              • guitarromantic@gmail.com

                #22
                Re: Storing array in a row

                Jerry Stuckle wrote:[color=blue]
                > Well, I never was good at matching quotes and parens. And I shouldn't have
                > tried to get fancy. The following should work:[/color]

                Yeah, it's messing with my head just trying to look at that line..

                [color=blue]
                > And BTW you'll need a double quote (") before the semicolon on the $query= line.[/color]

                Added this, but the row isn't inserted. I changed it to:
                $result = mysql_query($qu ery) or die ("error"); and it printed the
                message, so there's still something wrong there. I took a look but
                can't make head or tail of it, haha.

                Comment

                • Jerry Stuckle

                  #23
                  Re: Storing array in a row

                  guitarromantic@ gmail.com wrote:[color=blue]
                  > Jerry Stuckle wrote:
                  >[color=green]
                  >>Well, I never was good at matching quotes and parens. And I shouldn't have
                  >>tried to get fancy. The following should work:[/color]
                  >
                  >
                  > Yeah, it's messing with my head just trying to look at that line..
                  >
                  >
                  >[color=green]
                  >>And BTW you'll need a double quote (") before the semicolon on the $query= line.[/color]
                  >
                  >
                  > Added this, but the row isn't inserted. I changed it to:
                  > $result = mysql_query($qu ery) or die ("error"); and it printed the
                  > message, so there's still something wrong there. I took a look but
                  > can't make head or tail of it, haha.
                  >[/color]

                  Well, I don't have your tables; this was representative statements only. They
                  are meant to give you an idea how they work, not be your final code. You will
                  need to modify them as necessary to fit your circumstances.

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

                  Comment

                  Working...