Page 1 of 1

Struggling with Drop Down List and Results Display

Posted: Wed Apr 04, 2007 8:56 pm
by strugglestreet
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm new to php but have been progressing using books and the Internet as well as reading  this and other forums but am stuck.

I am putting together a website. I've created a mysql/php database. In a database called 'councils' I have a table called 'councildata'. 

In councildata there are 14 columns including the 'ID' field.

I've managed to populate a drop down selection box derived from the database which shows a list of every council. There are currently 60 councils entered. 

[b]The result I want now is: [/b]When a site visitor selects a council (or councils if multiple) using the drop-down box and selects 'submit', they see a page with the information they were looking for, relevant to that council. 

[b]For example: [/b]

User selects Council 'A' and submits form.

[i][b]Page appears with:[/b][/i]
[b]Council: [/b]Council 'A'
[b]Address:[/b] Address pulled from database
[b]Town or City: [/b]Town pulled from database
[b]Postcode: [/b]Postcode pulled from database

etc., etc. 

[b]I've managed to populate the drop down list with this code (below) [/b]- which may or may not be ideal and might be the reason I can't get the next page to display:

Code: Select all

<?php
$username="xxxxxxx";
$password="xdxxxxx";
$database="xxxxxxxxx";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM councildata";
$result=mysql_query($query);
function generate_box() {
    $sql = "SELECT * FROM councildata";
    $result = mysql_query($sql);
    $entries = mysql_num_rows($result);
    for($n = 0; $n <= $entries; $n++) {
        $sql = "SELECT * FROM councildata WHERE ID = $n";
        $result = mysql_query($sql);
        while ($info = mysql_fetch_array($result)) {
            foreach( $info AS $key => $val ) {
                $$key = stripslashes( $val );
            }
            $options .= "<option value=\"-1\">$council</option>";
            $options .= "<option value=\"-1\"></option>";
          }
    }
    return $options;
}
$options = generate_box();
?>
<form method="get" name="form_select" action="stupidity.php"><table cellspacing="0" cellpadding="0" border="0">
	<tr>
                <td><span>Search By: 
<select name="by">
<? echo "$options"; ?>


Now at least this populates the drop down list with the councils.

So now if I create a file called 'stupidity.php'. What's needed? A connection to the database, a table with rows along the lines of: td>Council</td>
<td><? echo $council; ?></td>?

What else?


I'm missing something REALLY basic here aren't I? And yet my mind is now mush after searching for the answer and alas I have hope in the goodness of this forum!!

Any help?

Any help appreciated!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Apr 05, 2007 8:38 pm
by califdon
Yes indeed, you are missing something very basic. Your second script does need to connect to the database, then you need to query the table in the database to return the specific record (*) you want to display, then display the data from that record. This requires that you know how to query a database. You must also know how to obtain the form data from your first script. Your code uses the "GET" method for the form (I would have used "POST", which is retrieved from an array instead of being sent as part of the URL), so you would have to set a variable to the value of $_GET['by'] and put that into your SQL query. Which reminds me, you don't need to go through all those repeated queries to get your list in the first place. Something like the following is a more usual syntax:

Code: Select all

echo "<select name='by'>";
    $sql = "SELECT * FROM councildata";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    while ($row) {
        extract($row);
        echo "<option value='$ID'>$council</option>";
        $row = mysql_fetch_assoc($result);
    }
    echo "</select>";
(*) You can only derive one value from a dropdown ("<select>") box. To permit multiple choices, you would have to use some other kind of control (such as a "<input type='checkbox'>").

I know it sounds harsh to say this, but it's true: it's not realistic to expect to ask people in a forum to teach you PHP (or anything else) from the ground up. Some of us might even be willing to try, but it's just not possible. You have to gain enough understanding of the basics so that you can ask for help for specific questions.

Posted: Fri Apr 06, 2007 11:51 am
by guitarlvr
Califdon:

how do websites such as dice.com allow multiple choices in their drop down? you can select one, hold the ctrl key and select another. Is that not possible with PHP?

Posted: Fri Apr 06, 2007 2:22 pm
by feyd
The <select> must be set to allow "multiple" choices. Your code also needs to be aware of how to read those choices. The easiest way is to name the <select> such that PHP will create an array of the values chosen for you automatically such as "foo[]"

Posted: Fri Apr 06, 2007 3:08 pm
by guitarlvr
Thanks feyd. I thought it was possible but never tried it.

Posted: Fri Apr 06, 2007 3:20 pm
by RobertGonzalez
Try this and see if does what you are looking for (I am in a coding mood today ;) ):

Code: Select all

<?php
// General help me out kind of things
$debug = true; //Reset this to false to turn off debugging
$councils = array();
$councils_count = 0;
$council = null;

// DB details, though if this is not distributed these 
// can go straight into your function calls
$username="xxxxxxx";
$password="xdxxxxx";
$database="xxxxxxxxx";

if (!$con = mysql_connect(localhost,$username,$password))
{
    $msg = 'Could not connect to the database server';
    if ($debug)
    {
        $msg .= ': ' . mysql_error();
    }
    die($msg);
}

if (!mysql_select_db($database))
{
    $msg = 'Unable to select database';
    if ($debug)
    {
        $msg .= ': ' . mysql_error();
    }
    die($msg);
}

$sql = "SELECT * 
        FROM councildata 
        ORDER BY ID";
if (!$result = mysql_query($sql))
{
    $msg = 'Could not get the council information';
    if ($debug)
    {
        $msg .= ': ' . mysql_error();
    }
    die($msg);
}

while ($row = mysql_fetch_array($result))
{
    $councils[$row['ID']] = $row;
}
$councils_count = count($councils);

if (isset($_POST['by']))
{
    $id = intval($_POST['by']);
    
    if (array_key_exists($id, $councils)
    {
        $council = $councils[$id];

        // Now you have all of your specific council data in a nice little array
        // that can show data based on $council['db_field_name'] for that council
        echo 'Council is: ' . $council['ID'];
    }
    else
    {
        echo 'There were no matches to your selected council.';
    }
}
else
{
?>
<form method="post" name="form_select" action="<?php echo basename(__FILE__); ?>">
<table cellspacing="0" cellpadding="0" border="0">
<?php if ($councils_count > 0): ?> 
    <tr>
        <td>Search By:
        <select name="by">
        <?php for($i = 0; $i < $councils_count; ++$i): ?>
            <option value="<?php echo $councils[$i]['ID']; ?>"><?php echo $councils[$i]['CouncilName']; // or whatever field ?></option>
        <?php endfor; ?>
        </select>
        </td>
    </tr>
<?php else: ?>
    <tr>
        <td>There were no councils to choose from.</td>
    </tr>
<?php endif; ?>
</table>
</form>
<?php
}
?>

Thanks for input

Posted: Fri Apr 06, 2007 7:03 pm
by strugglestreet
Everah,

Thanks for that... I've tried it out and getting an error at line 58, unexpected '{' - but leave it with me. I'll work it out. Your help is really appreciated.

Califdon I'm taking a crash course in php/mySQL and am not expecting forums to teach me from the ground up. I have spent hours going through two books I've bought and spend countless more reading tutorials on the internet - in truth I agree I'm probably skipping ahead of myself - and in fact have tried to learn too much too soon. I also know that once I get a handle on the basics properly I'll look back and wonder what all the fuss was about. But I thought as a last resort - not as a first resort 'cop out' - to pose my question on a forum. It'd be great if I had someone I know to just ring up and 'ask' but I don't. Besides - when more questions are posed, in their many different forms - with valuable answers and input - it leads to a more successful - (and more search optimised!) forum, don't you think?

Thanks all for your replies.

Posted: Fri Apr 06, 2007 7:08 pm
by RobertGonzalez
Replace this line:

Code: Select all

if (array_key_exists($id, $councils)
with this line:

Code: Select all

if (array_key_exists($id, $councils))
Once again my fat fingers have proven to be evil instead of good.