Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts

    Php if statement

    I am looking for a php if statement that is basicly like this:

    Like if the value of a row in a column table is 1 then show this, if the value is 2 then show this.

    Can someone help me out and make one for me.

    I think its simple and not many lines.

    Code:
    +---------+
    | P_style |
    +---------+
    |       2 |
    +---------+
    Like if its that value then echo something.
    If the value is 1 for P_style then display something else.
    Last edited by bucket; 10-22-2009 at 11:02 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    What you are looking for is the switch statement.
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Can you show me how to make that with connecting to a mysql database and getting it from a table.

    Thanks for figuring out what I mean.

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    You can connnect to mysql via this script(also look at the switch statement):

    PHP Code:

    mysql_connect
    ("localhost""user""pass") or die(mysql_error());
    mysql_select_db("my_db") or die(mysql_error());

    $result mysql_query("SELECT col1 FROM mytable"
    or die(
    mysql_error());  

    $row mysql_fetch_row($result);
    $col $row[0];


    switch(
    $col){

    case 
    1: echo'The value of the column is 1 ! '; break;
    case 
    2: echo'The value of the column is 2 ! '; break;




    This should work, it is untested though.

  • Users who have thanked BinaryX for this post:

    geoserv (10-22-2009)

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Thanks.

    So I edited to fit my style.

    PHP Code:
    <?php 
    include("inc/config.php");
    $result mysql_query("SELECT style FROM website"
    or die(
    mysql_error());  

    $row mysql_fetch_row($result);
    $col $row[0];

    switch(
    $col)
    {
    case 
    1: echo "value1";
    case 
    2: echo "value2"

    ?>
    Now If someone can make a dropdown for me that edits the value on it.

    Like this:
    PHP Code:
    <select>
    <
    option value="1">First</option>
    <
    option value="2">second</option>
    <
    option value="3">third</option>
    </
    select
    and the value that you pic will be updated in the database. So if you pick third and click submit, the value that will be in the database would be 3.

    Can someone help me out with that?

  • #6
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Bump.
    If someone can help me out.

  • #7
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts
    PHP Code:
    <?php 
    include("inc/config.php");

    if(isset(
    $_POST['choice']))
    {
    $choice $_POST['choice'];
    $result mysql_query("SELECT style FROM website WHERE column = '$choice'"
    or die(
    mysql_error());  

    $row mysql_fetch_row($result);
    $col $row[0];

    switch(
    $col)
    {
    case 
    1: echo "value1";
    case 
    2: echo "value2"
    }
    }
    ?>

    <form method="post" action="">
    <select name="choice">
    <option value="1">First</option>
    <option value="2">second</option>
    <option value="3">third</option>
    </select>
    <input type="submit" value="submit" />
    </form>
    Last edited by karlosio; 10-23-2009 at 01:50 PM. Reason: No code
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here

  • #8
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    I coded up something like this:

    Code:
    <form action="...">
    <select name="value">
    <option value="1">First</option>
    <option value="2">second</option>
    <option value="3">third</option>
    </select>
    <input type="submit" name="submit">
    </form>
    And some php code to handle it
    PHP Code:
    $allowed = array(123);
    $default 1;
    if (isset(
    $_POST['submit'])) {
       
    $value in_array($_POST['value'], $allowed) ? $_POST['value'] : $default;
       
    $query "UPDATE your_table SET style='$value' LIMIT 1";
       
    mysql_query($query) or die(mysql_error());

    Is that correct?

  • #9
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Okay I have used your idea:

    PHP Code:
    <?php 
    include("inc/config.php");

    if(isset(
    $_POST['choice']))
    {
    $choice $_POST['choice'];
    $result mysql_query("SELECT style FROM website WHERE style = '$choice'"
    or die(
    mysql_error());  

    $row mysql_fetch_row($result);
    $col $row[0];

    switch(
    $col)
    {
    case 
    1: echo "value1";
    case 
    2: echo "value2"
    case 
    3: echo "value3"
    }
    }
    ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="choice">
    <option value="1">First</option>
    <option value="2">second</option>
    <option value="3">third</option>
    </select>
    <input type="submit" value="submit" />
    </form>
    The thing is I want more than 2 cases. How would I do that?

  • #10
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Also It is not changing the value the the row to what i selected it to be in the dropdown and clicked submit.

  • #11
    Regular Coder
    Join Date
    Sep 2008
    Posts
    707
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Fixed!

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors''1');

    include(
    "inc/config.php");
    $allowed = array(123);
    $default 1;
    if (isset(
    $_POST['submit'])) {
       
    $value in_array($_POST['value'], $allowed) ? $_POST['value'] : $default;
       
    $query "UPDATE website SET style='$value' LIMIT 1";
       
    mysql_query($query) or die(mysql_error());
    }  
    ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="value">
    <option disabled selected value="0">Choose a style</option>
    <option value="1">first</option>
    <option value="2">second</option>
    <option value="3">third</option>
    </select>
    <input type="submit" name="submit">
    </form>
    And the one to show what it picked:
    PHP Code:
    <?php 
    include("inc/config.php");
    $result mysql_query("SELECT style FROM website"
    or die(
    mysql_error());  

    $row mysql_fetch_row($result);
    $col $row[0];

    switch(
    $col)
    {
         case 
    1
              echo 
    "value1 selected";
              break;
         case 
    2
              echo 
    "value2 selected"
              break;
         case 
    3
              echo 
    "value3 selected";
              break;
    }  
    ?>
    Thanks for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •