can I make the select form get the chosen option from the database

Discussion in 'PHP' started by free-designer, Oct 12, 2009.

  1. #1
    youtube.com/watch?v=V3IDH5-6QyQ

    this is an video explains everything when i chose 'Show 3 thumbs' and click on submit query it will return to 'Show 0 thumbs' i want to make it stay as i picked .

    and this is the code

    <?php    if ( isset ( $_POST['submit'] ) )
            {
                #set variables
                $example_input = $_POST['thumbs_num'];
                $readmore_text = $_POST['readmore_text'];
                
                $query = mysql_query("UPDATE wp_villaarts_settings SET examble='$example_input' , readmore_text='$readmore_text'");
                
                if ( $query )
                {
                    #successful entering of data
                    "the value have been updated successfully. Now check the database.";
                }
                else
                {
                    #unsuccessful entering of data
                    "there a problem occurred while updateing your value in the database";
                }
                
            }
            
            
            function ss_thumbs_count(){
                $result = mysql_query("SELECT examble FROM wp_villaarts_settings WHERE id='1' ");
                if (!$result) {
                    die("Database query failed: " . mysql_error());
                }
        
                while ($row = mysql_fetch_array($result)) {
                        $show_value =  $row["examble"] ;
                        echo $show_value ;
                }
            }
            
            function readmore_text(){
                $result = mysql_query("SELECT readmore_text FROM wp_villaarts_settings WHERE id='1' ");
                if (!$result) {
                    die("Database query failed: " . mysql_error());
                }
        
                while ($row = mysql_fetch_array($result)) {
                        $show_value =  $row["readmore_text"] ;
                        echo $show_value ;
                }
            }
    
    ?>
        <form method="post" action="<?php echo $_SERVER['']; ?>">
            <label>How many thumbs shows beside the slideshow: 
            <select name="thumbs_num">
                <option value="1">Show One Thumb</option>
                <option value="2">Show Tow Tuhmbs</option>
                <option value="3">Show Three Thumbs</option>
            </select></label> <small style="color:#F00;">Default: <strong>3</strong></small><br /><br />
            
            <label>write the read more text the way u like: <input type="text" name="readmore_text" id="readmore_text" value="<?php readmore_text(); ?>"/></label><br /><br />
            <input type="submit" name="submit" id="submit" /> 
            <br /><br />
        </form>
    
    PHP:
     
    Last edited: Oct 12, 2009
    free-designer, Oct 12, 2009 IP
  2. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use the following code
    <select name="thumbs_num">
                <option value="1">Show One Thumb</option>
                <option value="2" selected>Show Tow Tuhmbs</option>
                <option value="3">Show Three Thumbs</option>
            </select>
    Code (markup):
     
    ThomasTwen, Oct 12, 2009 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i know what dose that code do
    i want it to select automatically what i have already chosed
     
    free-designer, Oct 12, 2009 IP
  4. IAreWhiteCat

    IAreWhiteCat Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <option value="1" <?php echo isset($_POST['thumbs_num']) && $_POST['thumbs_num'] == 1 ? "selected" : ""?>>Show One Thumb</option>
                <option value="2"  <?php echo isset($_POST['thumbs_num']) && $_POST['thumbs_num'] == 2 ? "selected" : ""?>>Show Tow Tuhmbs</option>
                <option value="3"  <?php echo isset($_POST['thumbs_num']) && $_POST['thumbs_num'] == 3 ? "selected" : ""?>>Show Three Thumbs</option>
    Code (markup):

    it prints out "selected" if the value for $_POST['thumbs'] is the same as one of the <option> values.

    When you start to creating huge options, you should start creating your tags through a php loop. If you ever need help with that just send a pm.

    Looking back at your video, I see the default option is 3. You should just reverse the order of the options if you want "three rows" to show first. That's the easiest way. Hope this helps.
     
    Last edited: Oct 12, 2009
    IAreWhiteCat, Oct 12, 2009 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    it fixed but there is a problem when i open the page in anther tab it will show the first value again

    are we going to use session or anything of this kind to save what we have chosed

    agh i been for a week trying to fix it and it's not ,dam it

    so what u think ? why it's going back to the same problem again when i open in other tab ?
     
    free-designer, Oct 12, 2009 IP
  6. IAreWhiteCat

    IAreWhiteCat Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, it involves sessions. I'll definitely fix that, give me a sec
     
    IAreWhiteCat, Oct 16, 2009 IP
  7. IAreWhiteCat

    IAreWhiteCat Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php session_start(); ?> 
    
    <?php
    if (isset($_POST['thumbs_num'])){
    
    $_SESSION['thumbs_num'] = $_POST['thumbs_num'];
    }
    ?>
    
    
    <form method="post">
    <select name="thumbs_num"><option value="1" <?php echo isset($_SESSION['thumbs_num']) && $_SESSION['thumbs_num'] == 1 ? "selected" : ""?>>Show One Thumb</option>
                <option value="2"  <?php echo isset($_SESSION['thumbs_num']) && $_SESSION['thumbs_num'] == 2 ? "selected" : ""?>>Show Tow Tuhmbs</option>
                <option value="3"  <?php echo isset($_SESSION['thumbs_num']) && $_SESSION['thumbs_num'] == 3 ? "selected" : ""?>>Show Three Thumbs</option></select>
    
    			<input type="submit" name="submit" value="submit" />
    			</form>
    Code (markup):
    There you go. Make sure you start the session at the TOP of the page, that's vital. Happy coding
     
    IAreWhiteCat, Oct 16, 2009 IP
  8. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it worked but there is a new problem

    first this is a plugin for customers to help them customize their pages

    let's say im customer number one and working on mozilla firefox the session is going to be saved on my browser okay what if im going to work in other computer my session ofcourse is going to be only in my own pc what if im going to use other pc so im going to open my plugin in wordpress so my all saved options is going to change to the fisrt value and it's not going to be saved

    what are we going to do now ??

    please help
     
    Last edited: Oct 17, 2009
    free-designer, Oct 17, 2009 IP
  9. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    part of your code is:
    <form method="post" action="<?php echo $_SERVER['']; ?>">
    Code (markup):
    replace it with:
    <form method="get" action="<?php echo $_SERVER['']; ?>">
    Code (markup):

    If you use GET instead of POST, also the data will be appended to the URL and therefore be transferred when you open the same URL on another computer.

    Using GET has one important downside: All the data is clearly visible in the URL, so you shouldn't use it for passwords.
     
    ThomasTwen, Oct 17, 2009 IP
  10. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    it's not going to work
    because im posting on the database not getting from the database so i can't use it

    cuz my form posting not getting

    sorry

    so any other idea
     
    free-designer, Oct 17, 2009 IP
  11. IAreWhiteCat

    IAreWhiteCat Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Lol... Okay then, give me a second. I gotta think about this
     
    IAreWhiteCat, Oct 20, 2009 IP
  12. IAreWhiteCat

    IAreWhiteCat Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Alright, so you gotta tell me a bit more details.

    Are you using a database to get the info from? Is this for wordpress?

    I just need to know how you plan on using this. I'm assuming now you want a customer to access his option from any computer. So you have something set up where he logs in?
     
    IAreWhiteCat, Oct 20, 2009 IP
  13. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    you know wordpress? of course you do okay let's talk

    now in your blog you are showing to people 10 posts okay let's say the owner of the blog don't know how to work with codes and php and all this stuff okay ...

    i made a table on my wordpress database witch's call "wp_villaarts_settings" and have on it 1 column call "posts_counter" okay this column have the number "10" i made my plugin have a function witch's get this number and i matched the function have a variable witch's only get the value witch's in the database and in the page that call index you have the code witch's let you write how many posts to show i added the variable except writing "10"

    sooo the variable is getting the number from the database witch's is 10 so it's going to read the 10 number there is 10 posts is going to show to people

    okay the idea of the plugin is to have a form let you write the number and the form is going to update automatically the database let's say the database have the number 10 let's say Im going to write on the form 5 so it's going to update it so the function that I made is going to read the database and the variable is going to read 5 so it's going to automatically show 5 posts

    this is the whole idea about it

    but my customer want a slideshow in his blog and the slideshow have 5 thumbs im just going to make the select form give him 5 options let say he want to show just 3 tumbs insted of showing 5 so the select form is going to give him the ability to control it very easy but my problem is

    let's say he chosed 3 thumbs so the fucking select form is going to go back to the first option as we all know that

    I WANT HIM TO CHOSE 5 AND WHEN HE CLICK ON UPDATE AND GOING TO LOGOUT AND LOG BACK IN OTHER TIME THE SELECT STAY ON FIIIIIVE

    aghh

    if you need any other question just ask me okay

    cuz we going to solve this out okay! YOU WILL :mad:
     
    free-designer, Oct 20, 2009 IP