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 9 of 9
  1. #1
    CPD
    CPD is offline
    New Coder
    Join Date
    Jul 2012
    Location
    Ontario Canada
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Newbe....Can not find an explanation for one line of code

    Can someone please explain this line of code, in particular the syntax, quotes, dots slashes, ?, etc.
    [
    PHP Code:
        <select name="venue" onChange="autoSubmit();">        
            <option value="null">Select Venue</option>
            
    <?php
            
            
    //POPULATE DROP DOWN MENU WITH VENUES FROM A GIVEN REGION
            
            
    $sql1 "SELECT vid, vname FROM venue WHERE re_id = $region";
            
    $venues mysql_query($sql1,$conn);
      
            while(
    $row mysql_fetch_array($venues))

            {      
                echo (
    "<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" "") . ">$row[vname]</option>");    
            }    

    ?>   
        </select>
       
    <?php
        
        
    }
        echo 
    $row["vname"];    
    ?>
    ]

    It is this line that I am having problems with
    PHP Code:
    echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" "") . ">$row[vname]</option>"); 
    it works but I need to extract the "$row[vname]" into the $_GET to use in the next page, if someone could explain this line of code it would be great
    thanks
    Last edited by CPD; 07-11-2012 at 09:35 PM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    It's shorthand for an if statement, if $venue == vid then the option will be the default selected one
    You just need the name of the select it belongs to

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    PHP Code:
    echo $var == 'value' 
    Is exactly equivalent to

    PHP Code:
    if ($var == 'value') echo 1;
    else echo 
    0
    So

    PHP Code:
    $venue == $row["vid"] ? " selected" "" 
    is the same as

    PHP Code:
    if ($venue == $row['vid']) echo ' selected'
    The nice thing is you can do it inline, as you see, without needing to break. So you can store stuff into a string based on other variables, without having to do multiline concatenation.

  • Users who have thanked Keleth for this post:

    CPD (07-11-2012)

  • #4
    CPD
    CPD is offline
    New Coder
    Join Date
    Jul 2012
    Location
    Ontario Canada
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So how would I extract the selected "$row[vname]" for use on the next page?
    PHP Code:
    echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" "") . ">$row[vname]</option>"); 
    The selection is made by the "$row[vid]", which is required elsewhere in the code, and is required by the next Drop Down, I also need to $_GET the "$row[vname]"for use in the post to data base

    And what does the slashes do in the following
    PHP Code:
    echo ("<option value=\"$row[vid]\" " . ($venue ........ 
    Thanks
    Last edited by CPD; 07-11-2012 at 10:26 PM.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The term you are looking for is "ternary operator" "?:". As was mentioned it is shorthand way of writing if/else statements and is not always easily readable or easy to follow.

    http://www.php.net/manual/en/languag...comparison.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    your select name is 'venue' , so $_POST['venue'] should be available on the next page (or $_GET['venue'] if you use GET method in your form)

    the slashes 'escape' the quotes e.g.

    echo "hello from "me" ";
    will issue a parse error but
    echo "hello from \"me\" ";
    does not ... see strings in the manual

    also ...
    PHP Code:
    echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" "") . ">$row[vname]</option>"); 
    should really be....

    PHP Code:
    echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] ? " selected" "") . ">{$row['vname']}</option>"); 
    $row[vid] will work but issue a warning
    $row['vid'] will work as expected
    {braces} further tell PHP to expect a more complex variable within, say an array value or object property
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    CPD
    CPD is offline
    New Coder
    Join Date
    Jul 2012
    Location
    Ontario Canada
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well I must say that I am learning, I have another question?

    I need to extract a second column from the selected

    Origional code
    PHP Code:
    echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] ? " selected" "") . ">{$row['vname']}</option>"); 
    I need to do something like this...but it will not work no matter how I configure the code, see the "&& $ven....."
    PHP Code:
    echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] && $ven == $row['vname'] ? " selected" "") . ">{$row['vname']}</option>"); 
    Is there any way to get both the "$row['vid']" and the "$row['vname']" that is selected to further process, the select name "venue" is stored in "$_GET [venue]", and works fine, and is needed for the next drop down, I also need the "$row['vname']" that is selected.
    Thanks
    Last edited by CPD; 07-12-2012 at 04:27 PM.

  • #8
    CPD
    CPD is offline
    New Coder
    Join Date
    Jul 2012
    Location
    Ontario Canada
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Anyone have any suggestions ??

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Perhaps you mean to use an || instead of an &&? The ternary is otherwise correct, the first condition must match in its entirety to execute the true condition, which is why I suspect you are looking for OR and not AND.

  • Users who have thanked Fou-Lu for this post:

    CPD (07-21-2012)


  •  

    Tags for this Thread

    Posting Permissions

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