Using foreach and "radio" fields

Discussion in 'PHP' started by romic, Jan 30, 2007.

  1. #1
    Hi all,

    I have a table in the database with info about product photos. The table has the following fields:
    - idphoto
    - idproduct
    - photo
    - thumbnail
    - showinsearch (this can have value of 1(photo shown in search results) or 0(not shown in search))

    only one of the photos can be shown in search that's why I think I should be using radio fields (so that the user can only select one of the photos for search result).

    Please suggest how should I do this to correctly update the data in the BD. If I didn't explain something clear enough please ask and I'll try to reformulate.

    Thanks.
     
    romic, Jan 30, 2007 IP
  2. Dan Nilsson

    Dan Nilsson Peon

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sorry, I didn't quite get what you're trying to do, please explain further. :)
     
    Dan Nilsson, Jan 30, 2007 IP
  3. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As I said, the table has the following fields:
    - idphoto - unique ID of the photo
    - idproduct - ID of the product that the photo belongs to
    - photo - name of the big photo
    - thumbnail - name of the thumbnail
    - showinsearch (this can have value of 1(photo shown in search results) or 0(not shown in search))

    A product can have up to 6 photos. The user can open a page and see all the photos (thumbnails) of a given product, and on that page he can choose which of the photos to show in search results (selecting the "radio" button/field). After that he/she clicks button "Update" and the table data is updated having the "showinsearch" set to "1" for the selected photo and "0" for all the others.
     
    romic, Jan 30, 2007 IP
  4. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    since there can only be one photo (thumbnail) in search results I thought it should be done using "radio" input fields.

    Please let me know your ideas about this, if any. Thanks.
     
    romic, Jan 30, 2007 IP
  5. Dan Nilsson

    Dan Nilsson Peon

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, try something like this:

    
    <form method="post">
    <input type="radio" name="pic" value="pic1">
    <input type="radio" name="pic" value="pic2">
    etc...
    <input type="hidden" name="idproduct" value="prod1">
    <input type="submit" value="Update">
    </form>
    
    HTML:
    And the PHP code:
    
    <?php
    mysql_query("UPDATE tablename SET showinsearch=1 WHERE idphoto='".$_POST['pic']."'");
    mysql_query("UPDATE tablename SET showinsearch=0 WHERE idphoto!='".$_POST['pic']."' AND idproduct='".$_POST['idproduct']."'");
    ?>
    
    PHP:
    ...or something like that.
     
    Dan Nilsson, Jan 30, 2007 IP
  6. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for this ideia
     
    romic, Jan 30, 2007 IP