SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Zealot gregorious's Avatar
    Join Date
    Aug 2006
    Location
    Honolulu, HI
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get MYSQL value to show in DROP MENU

    I am building a CMS for a real estate site, with COMMERCIAL and RESIDENTIAL page listings. The CMS has ADD/SELECT/EDIT/DELETE pages for both sections. The ADD page is a basic form page, with a coulple of DROP MENUs; when submitted the data is recored in a MYSQL database.

    The EDIT page is a mirror of the ADD page; the data is retrieved from the database and placed into the form fields for editing by the user; the form works well - except for the DROP DOWN MENUs. The drop menus default to the first value in the form but I can't seem to get the DROP MENUs on the EDIT page to show the value that is in the database.

    The code below does not show the database value.

    Code:
    <select name="restype" class="">
    <option selected value="<?php echo $restype; ?>">  
    <option value="SINGLE FAMILY HOME">SINGLE FAMILY HOME
    <option value="DUPLEX FAMILY HOME">DUPLEX FAMILY HOME
    <option value="MULTI FAMILY PROPERTY">MULTI FAMILY PROPERTY
    <option value="CONDOMINIUM">CONDOMINIUM
    <option value="LAND (RESIDENTIAL)">LAND (RESIDENTIAL)
    </select>
    Been working with PHP MYSQL for less than a year, so my apolgy if this is "no brainer".

    Thanks in advance,
    Greg
    G. Armento - Design, Multimedia, Illustration, Photography
    -------------------------------------------------------
    www.communications-by-design.com

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* DisplaySelect function                                                  */
    /* Author : Byron Thomas                                                   */
    /* Date : 07 / 07 / 2007                                                   */
    /* In parameters : $Opt_r is an array of option selections                 */
    /*               : $Name is the name of the field in POST / GET array      */
    /*      Optional : $Selected is the value the list will be set to          */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    function DisplaySelect$Opt_r$Name$Selected "" ) {
        echo 
    "<select name='" .$Name"'>\n";
        foreach ( 
    $Opt_r as $Val ) {
            if ( 
    $Val == $Selected ) {
                echo 
    "<option selected='selected'>" .$Val"</option>\n";
            } else {
                echo 
    "<option>" .$Val"</option>\n";
            }
        }
        echo 
    "</select>\n";

    You only need the value attribute if you want something other than the option text to be submitted as the POST / GET value. If you ned a variation of this code that will allow you to specify a value let me know.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    If you're going to go so far to create fancy documentation like that, you might as well do it in phpdoc format, so that phpdoc parsers and PHP5's own reflection classes can pull it out

  4. #4
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        <select name="Select1" style="height: 22px">
        <?php
        $query 
    mysql_query("SELECT * FROM table ORDER BY id DESC");
        while(
    $row mysql_fetch_array($query)) {
        
    $restype $row['restype'];
        
    ?>
        <option value="<?php echo $restype?>"><?php echo $restype?></option>
        <?php
        
    }
        
    ?>
        </select>
    I am sure you can change restype around to make it fit your application

    If you need any more help feel free to contact me.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Should be:
    PHP Code:
    $restype $row['restype']; 
    Without it, restype would need to be a defined constant, otherwise the syntax is incorrect and will generate a notice.

  6. #6
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Should be:
    PHP Code:
    $restype $row['restype']; 
    Without it, restype would need to be a defined constant, otherwise the syntax is incorrect and will generate a notice.
    Sorry about that, mistake. I am used to Zend Studio doing it for me, just reformatted and trying out Microsoft Expression Web.

    Thanks

  7. #7
    SitePoint Zealot gregorious's Avatar
    Join Date
    Aug 2006
    Location
    Honolulu, HI
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I was in the ballpark. but just a little off base.

    Code:
    <select name="restype" class="listing01">
    <option selected value="<?php echo $restype; ?>"><?php echo $restype; ?></option>
    <option value="SINGLE FAMILY HOME">SINGLE FAMILY HOME?</option>
    <option value="DUPLEX FAMILY HOME">DUPLEX FAMILY HOME?</option>
    <option value="MULTI FAMILY PROPERTY">MULTI FAMILY PROPERTY</option>
    <option value="CONDOMINIUM">CONDOMINIUM</option>
    <option value="LAND (RESIDENTIAL)">LAND (RESIDENTIAL)</option>
    </select>
    Last edited by gregorious; Jul 10, 2007 at 10:12. Reason: added closing </option> tags
    G. Armento - Design, Multimedia, Illustration, Photography
    -------------------------------------------------------
    www.communications-by-design.com

  8. #8
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, hope that helped!
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  9. #9
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isn't the correct html
    Code:
    selected="selected"
    ? It probably works either way... and where is the closing </option> tags on the above output?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  10. #10
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope its <option selected I believe.
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  11. #11
    SitePoint Addict einSTein's Avatar
    Join Date
    Aug 2006
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can make it like this one
    Code PHP:
    <?php
    	include 'connect.php';
    	$kk=$session['user1'];
    	$C=0;
    	$result2 = mysql_query("SELECT * FROM j_cat ORDER BY name");
    	echo '<select size="1" name="jc">';
    	while($row = mysql_fetch_array($result2))
    		{
    		echo '<option value="'.$row['value'].'">'.$row['name'].'</option>';
    		}
    echo '</select>';
    ?>

  12. #12
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you want a session?
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  13. #13
    SitePoint Zealot gregorious's Avatar
    Join Date
    Aug 2006
    Location
    Honolulu, HI
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code below is an abreveated version of the full page, but there seems to be a discussion here about the correct usage/method/syntax so I have included the variables and mysql calls.

    The solution above seems to work, but the $restype variable does appear twice. Once as the selected data [from the database] and again as a regular menu choice.

    This could be confusing to my client who is not very computer literate, so if there is another way to acheive the same results without the duplication - or - any other suggestions I would be interested in hearing them.

    Learning keeps me young.
    Greg

    PHP Code:
    <?php
    if (isset($_POST['resorder'])): // POST an UPDATE data in form       
            
    $resorder $HTTP_POST_VARS['resorder']; 
            
    $resstreet01 $HTTP_POST_VARS['resstreet01'];
            
    $rescity $HTTP_POST_VARS['rescity'];
            
    $resstate $HTTP_POST_VARS['resstate'];
            
    $reszip $HTTP_POST_VARS['reszip'];
            
    $restype $HTTP_POST_VARS['restype'];
            
    $id $HTTP_POST_VARS['id'];
         
    $sql "UPDATE residential SET            
        res_order='
    $resorder',
        res_street01='
    $resstreet01',
        res_city='
    $rescity',
        res_state='
    $resstate',
        res_zip='
    $reszip',                        
        res_type='
    $restype'   //no ****** comma here
                 WHERE res_id='
    $id'";

    if (@
    mysql_query($sql)) {
    echo 
    '<blockquote><p>Residential Property UPDATED.</p>' .
    } else {
    echo 
    '<p>Error UPDATING Residential Property: ' .
    mysql_error() . '</p>';
    }
    ?>

    <?php
    else: // GET and ECHO data in form
        
    $id $_GET['id'];
    $textarray = @mysql_query("SELECT * FROM residential WHERE res_id='$id'");
    if (!
    $textarray) {
    exit(
    '<p>Error GETTING data from database: ' .
    mysql_error() . '</p>');
    }

    $textarray mysql_fetch_array($textarray);
        
    $id $textarray['res_id'];
        
    $resorder $textarray['res_order'];
        
    $resstreet01 $textarray['res_street01'];
        
    $rescity $textarray['res_city'];
        
    $resstate $textarray['res_state'];
        
    $reszip $textarray['res_zip'];
        
    $restype $textarray['res_type'];                

        
    $resorder htmlspecialchars($resorder);
        
    $resstreet01 htmlspecialchars($resstreet01);
        
    $rescity htmlspecialchars($rescity);
        
    $resstate htmlspecialchars($resstate);
        
    $reszip htmlspecialchars($reszip);                    $restype htmlspecialchars($restype);
    ?>

    <form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post" class="listing01">

    <tr>
    <td width="70%" align="left" valign="bottom">
    <input name="resorder" class="body01" size="3" maxlength="3" value="<?php echo $resorder?>" />
    <span class="caption_cap">&nbsp;Display In This Order</span></td>

    <td width="30%" align="left" valign="bottom">
    <span class="caption_cap">Record Id&nbsp;&nbsp; <?php echo $id?></span></td>
    </tr>
    </table>


    <span class="caption_cap">Residence Type</span><br>
    <select name="restype" class="listing01">
    <option selected value="<?php echo $restype?>"><?php echo $restype?></option>

    <option value="SINGLE FAMILY HOME">SINGLE FAMILY HOME</option>
    <option value="MOBILE HOME">MOBILE HOME</option>                
    <option value="DUPLEX FAMILY">MULTI FAMILY</option>
    <option value="TOWNHOUSE">TOWNHOUSE</option>
    <option value="CONDOMINIUM">CONDOMINIUM</option>
    <option value="LAND (RESIDENTIAL)">LAND (RESIDENTIAL)</option>
    </select>
    <br>

    <span class="caption_cap">Street Address</span><br>
    <input name="resstreet01" class="body01" size="60" maxlength="50" value="<?php echo $resstreet01?>" />
    <br>

    <span class="caption_cap">City</span><br>
    <input name="rescity" class="body01" size="60" maxlength="50" value="<?php echo $rescity?>" />
    <br>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>

    <td width="49%" align="left" valign="top" class="listing01"><span class="caption_cap">State</span><br>
    <input name="resstate" class="body01" size="25" maxlength="20" value="<?php echo $resstate?>" /></td>
                
    <td width="51%" align="left" valign="top" class="listing01"><span class="caption_cap">Zip</span><br>
    <input name="reszip" class="body01" size="25" maxlength="20" value="<?php echo $reszip?>" /></td>
    </tr>
    </table>


    <input name="id" type="hidden" value="<?php echo $id?>" />
    <p align="center">
    <input name="submit" type="submit" class="body01" value="SUBMIT">
    </p>
    </form>
    G. Armento - Design, Multimedia, Illustration, Photography
    -------------------------------------------------------
    www.communications-by-design.com

  14. #14
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I handed you a function in the second post in this thread to take care of that little duplication problem. Would you like me to show you how to implement it in your code ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  15. #15
    SitePoint Zealot gregorious's Avatar
    Join Date
    Aug 2006
    Location
    Honolulu, HI
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FUNCTIONS is where the sport really becomes an ART

    Quote Originally Posted by byron3@earthlink View Post
    I handed you a function in the second post in this thread to take care of that little duplication problem. Would you like me to show you how to implement it in your code ?
    Thanks for the support, I love these forums for that reason.

    Your solution uses a FUNCTION and I am a PHP white belt; I realize that FUNCTIONS is where the sport really becomes an ART. The issue I raised was a tiny one and I had larger issues to combat. Thanks for the offer, when I get a breather I would like to learn more about functions.

    I once got my mind set on implemeting a feature into a CMS that would allow a user to upload a photo and the place it into the page by putting [IMG-L]name[/IMG] for a flush left image, and vice-versa for flush right image. That was two weeks I will never get back; but I learned about REGULAR EXPRESSIONS and acheived my goal.

    So I have learned to prioritize my issues and their solutions against my skill set an deadlines. It seems the more I learn about PHP/MYSQL; I realize how little I really know.

    Best Regards,
    Greg
    G. Armento - Design, Multimedia, Illustration, Photography
    -------------------------------------------------------
    www.communications-by-design.com

  16. #16
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    functions are not too daunting, for me OOP is alot more confusing. Here is an example of a function
    PHP Code:
    function num($value)
    {
        if (
    is_numeric($value)) 
        {
            return 
    true
        } else {
            return 
    false;
        }
    }

    $numbers = array(1,2,5,433,'boobs','551a');

    foreach (
    $numbers as $k)
    {
        if (
    num($k))
        {
            echo 
    "$k is a number <br />";
        } else {
            echo 
    "$k is not a number <br />";
        }

    some rules are:
    - don't forget to declare a function before trying to use it
    - you can't use reserved words as function names

    the rest is basically trial and error
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  17. #17
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    - don't forget to declare a function before trying to use it
    Not a rule for PHP

  18. #18
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perhaps I used the wrong terminology. If the function does not exist in your included code, this happens:
    Code:
    Fatal error: Call to undefined function num() in /home/username/public_html/sandbox/index.php on line 17
    which leads me to believe the word I was looking for is "define", not "declare"
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  19. #19
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes the function must exist, but where it's declared doesn't have to be before you use it

  20. #20
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I discovered this just recently when I called functions within functions that occurred lower down in the functions file.... presumably php (parser?) creates an index of all functions before executing code?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


Bookmarks

Posting Permissions

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