Jump to content

Advice please.


Phpfr3ak

Recommended Posts

In short i want to use the following code below, when someone selects there option and submits it, it would bring up details from the database on this user from the selected table, can you explain what it would be called doing this so i can look it up, Sorry to be a pain, Cheers.

 

<select name="target2" id="target2">

    <option value=""></option>

  <?php

  $sql = "SELECT player_id, friend_id, name, is_active FROM contacts as c JOIN players as p ON c.friend_id = p.id WHERE c.player_id = $playerID AND is_active = 1 ORDER BY name ASC";

  $que = mysql_query($sql) or die(mysql_error());

  while($list = mysql_fetch_array($que)) {

  ?>

  <option value="<?php echo $list['friend_id'] ?>"><?php echo $list['name'] ?></option>

  <?php } ?>

  </select>

Link to comment
Share on other sites

Um, a database-driven web page? What you are asking is not a single function or process. It is several functions/processes to achieve the desired goal. If you have a form and can post the form to another (or the same page). You just need to check for the value passed in the form. In this case yor script would check for the value of $_POST['target2'] (or $_GET['target2'] depending on the method you set for the form). From there, you would want to validate/sanitize the value and then run a select query for the information you want to display. Then create the logic to output the data in the manner you want it.

 

Also, some advice on the code above:

 

If you are only using 'friend_id' and 'name', then you only need to include those in the SELECT query. Plus, I always suggest separating the logic of your code from the presentation. So, I would put the logic to generate the options at the top of the script and then output the options in the display portion of the code at the bottom of the script (or even in a separate file). Lastly, if you need to generate multiple select lists using database queries it would be advantageous to create a function that you can pass a query to.

 

My take

<?php

$sql = "SELECT friend_id, name
        FROM contacts AS c
        JOIN players AS p ON c.friend_id = p.id
        WHERE c.player_id = $playerID
          AND is_active = 1
        ORDER BY name ASC";
$que = mysql_query($sql) or die(mysql_error());

$target2Options = '';
while($list = mysql_fetch_array($que))
{
    $target2Options .= "<option value='{$list['friend_id']}'>{$list['name']}</option>\n";

}

?>

<select name="target2" id="target2">
    <option value=""></option>
    <?php echo $target2Options; ?>
</select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.