SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select/options tag in form and php

    I'm doing a form that will have a select/options html tag. The menu will have a listing of 50 states, i.e. Alabama, Alaska etc. I need to be able to recall the states for categorizing the links a user will submit.

    Is there an easy way to do this with php or do I need to write out all 50 option states in the form?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Give this a quick whirl...
    PHP Code:
    <?php
    $aStates 
    = array(
        
    'az'    =>    'Arizona',
        
    'tx'    =>    'Texas'
    );

    echo 
    '<select name="states">';
    foreach(
    $aStates as $sCode => $sName)
    {
        echo 
    sprintf('<option value="%s">%s</option>'$sCode$sName);
    }
    echo 
    '</select>';
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're still gonna need to type them all at some point, whether it's into an array that PHP iterates to create the form fields, or directly into your form markup.

    Of course if you need the list in multiple places you'll want to reference the same list to avoid any retyping.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm learning php as I go, so bear with me. Is the %s some kind of shortcut to the variable storing the state?

    This appears on the screen with a %s box right after the echo.
    'Arizona', 'tx' => 'Texas' ); echo ''; ?>

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Have you saved your file with the PHP extension? Are you calling the file which contains this code from a web server, i.e. http://.... and not a local path?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm running Wamp and calling it from within localhost www folder with php extension.

    The folder was listed wrong in the address folder. Your script is working now.
    Last edited by trevor2; Jan 8, 2009 at 16:31. Reason: I'm an idiot.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting this error on screen. Can I not use the mylink function within the mysql_query like this? I tried variable variable and it just makes another error.


    Connected successfully.
    Database Selected.
    Table query sent.

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test\mylink.php on line 34
    Invalid query:
    ------------------------------------------------------------------

    <?php

    //establish connection to sql databse server

    function mylink($host, $user, $pass = ""){
    $link = mysql_connect($host, $user, $pass);
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully.<br />';
    }
    ?>

    <?php

    $mylink = mylink('localhost', 'root', "");
    //$mylink = mysql_connect("localhost", "root", "") or die ("Could not connect");

    //select database
    $db = mysql_select_db('db_form');
    if (!$db) {
    die('Could not select db_form: ' . mysql_error());
    } else { echo 'Database Selected.<br />';
    }

    //query the table
    $sql = "SELECT * FROM `states`";
    if (!$sql) {
    die('Could not query the table: ' . mysql_error());
    } else { echo 'Table query sent.<br />';
    }

    // execute the query
    $result = mysql_query($sql,$mylink); <<---------------- Line 34

    if (!$result) die('Invalid query: ' . mysql_error());
    echo "sid, sCode, sName<br>";

    //fetches each row of result set as an array
    while($result = mysql_fetch_row($result)){

    //display the values using numeric array index
    echo "$result[0], $result[1], $result[2]<br>";
    }

    //echo mysql_result($result, 2,2,);
    ?>

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No you cannot connect with a function, as it loses 'scope' once the function has completed.

    You could however have the function return the connection for you.

    PHP Code:
    <?php
    function myLink()
    {
        
    $rConn mysql_connect('localhost''username''password');
        if (
    is_resource($rConn)) {
            
    mysql_select_db('database');
            if (!
    mysql_error($rConn)){
                return 
    $rConn;
            }
        }
        
    trigger_error('Cannot connect to database:- ' mysql_error(), E_USER_ERROR);
        return 
    false;
    }

    $rLink myLink();
    ?>
    Additionally, the following section of code is not going to work as expected. $sql is always going to be true because you have assigned a value to the variable, you should be checking if the query is executed successfully.

    PHP Code:
    <?php

    $sql 
    "SELECT * FROM `states`";
    if (!
    $sql) {
    die(
    'Could not query the table: ' mysql_error());
    } else { echo 
    'Table query sent.<br />';
    }

    #should be
    $rResult mysql_query('SELECT * FROM `states`');
    if (!
    is_resource($rResult))
    {
        die(
    'Could not query the table: ' mysql_error());
    }
    else
    {
        echo 
    'Table query sent.<br />';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    It works!

    I was having some trouble with the html tags parsing and also displaying all 59 entries on the drop menu. Realized that the code was in the while loop and displaying for each row in the database.

    Now, I just need to make the drop menu into a function and find a way to get the user selection back to a different table.

    Code PHP:
    <?php
    //connect to database
    $mylink = mysql_connect("localhost", "root", "") or die ("Could not connect");
     
    //select database
    $db = mysql_select_db('db_form');
    if (!$db) {
    		die('Could not select db_form: ' . mysql_error());
    	} else { echo 'Database Selected.<br />';
    	}
     
    // execute the query
    $rResult = mysql_query("SELECT `sid`, `sCode`, `sName` FROM `states`");
    	if (!is_resource($rResult))
    	{
    		die('Could not query the table: ' . mysql_error());
    	}
    	else
    	{
    		echo 'Table query sent.<br />';
    	}
     
    // dropdown menu starts here
    	echo "<select name = 'state'/>";
     
    	while ($row = mysql_fetch_array($rResult)) 
    	{
    		echo "<option value = '" . $row['sCode'] . "'>". $row['sName'] . "</option>";		
    	}
    	echo "</select>";
     
    ?>
    <?php mysql_close($mylink);?>

  10. #10
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    html table or mysql table ?

    if its html table, try somehting like this

    Code PHP:
       <table>
        while ($row = mysql_fetch_array($rResult))
        {
            echo '<tr>';
                echo '<td>' . $row['sCode'].'</td>';
                echo  '<td>' .$row['sName'] . '</td>';     
            echo '</tr>'; 
        }
       </table>


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
  •