4

Im a newbie to this forum and have just started coding in php. Need some help. I have the following code

<?php error_reporting(-1);

require_once('/mysql/sql_connect.php');

$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
    $r = @mysqli_query ($dbc, $q);

    $results_array = array();
    while($row = mysqli_fetch_assoc($r)) {
        array_push($results_array, $row);
    echo "<pre>"; print_r($results_array); echo "</pre>"; }

?>

<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>  
   <select name="pty_select" > 
   <?php foreach($results_array as $key => $value){ ?>
                    <option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option> 
    <?php } ?>
        </select>
    <input type="submit" name="Submit" />
</form>

<?php 

if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?> 

The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.

If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.

I hope I've made sense. Thanks in advance.

5 Answers 5

5

It will obviously echo the key, as you assigned the value of options as $key if you need the options in the $_POST['pty_select'] use this:

 <select name="pty_select" > 
<?php foreach($results_array as $key => $value){ ?>
                <option value="<?php echo $value['profile'];?>"><?php echo $value['profile'];     ?></option> 
<?php } ?>
    </select>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that worked. Just waiting on a response from another poster before closing
3

You mean you need the value what you have used to display it. Then, Change to :

<option value="<?php echo $value['profile']; ?>">
    <?php echo $value['profile']; ?>
</option> 

1 Comment

thanks that worked. Just waiting on a response from another poster before closing
2

Give

<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option> 

instead of

<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option> 

4 Comments

thanks that worked. Just waiting on a response from another poster before closing
@DanielGarcia please close once you are done. Cheers and Happy Coding. :)
Hi, how do I close again? Sorry I am new to this site
@DanielGarcia their is a tick mark you would see below the vote symbol near answer just tick it. Then it will be marked as solved.
2

And now let's go to ideal world :)

Build data pairs database_id => name for options:

$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users 
      WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);

$values = array();
while($r = mysqli_fetch_row($r)) {
    $values[$r[0]] = $r[1];
}

Never use @ when working with database, why do you want to suppress errors instead of preventing/handling them?

Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:

function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
    // Header
    echo '<select';
    foreach( $attributes as $key => $val){
        echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
    }
    echo '>';

    // Values
    foreach( $values as $key => $val){
        echo '<option value="' . htmlspecialchars( $key) .'"';
        if( $key === $selected_value){
            echo ' selected="selected"';
        }
        echo '>' . htmlspecialchars( $val) . '</option>';
    }
    echo '</select>';
}

And now usage :)

<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>  
    <?php selectbox( $values, array( 'name' => 'pty_select')); ?>
    <input type="submit" name="Submit" />
</form>

And what to do with it then?

$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
    $name = $values[$id];
}

3 Comments

Thanks guys I will try these suggestions tonight and let you know how I get on. Cheers
Hi Vyktor, the code that the others gave me worked, however, I am trying to figure out how to get your code working as I want to learn new techiques. What does $values[$r[0]] = $r[1]; do? Why would the 2nd value in an array need to be assigned to the first value?
@DanielGarcia idea is you'll get pairs (id,name) pairs like (1, Emerton) or (2,Foo). Note those are real database ids. And you'll build associative array $values = array( 1=>"Emerton", 2=>"Foo") and you can use it as $values[1] to get string. IDs are universal identifiers which are effective and therefore I prefer to use them :)
1
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?> 

Change it to something like

if(isset($_POST['Submit'])) {
    echo $results_array[$_POST['pty_select']]['profile'];
}

Or alternatively use profile option value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.