Functions

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Functions

Post by Epiales »

Hey all, been working on an admin area and created a function that will display a drop down, and allow you to select a user to edit. I've gotten as far as the function but don't know how to display the database information when the user is selected. Here is my function:

Code: Select all


function user_dropdown($ddname = "username", $selected = -1)
{

$ret = "<select name='$ddname' type='dropdown'>";
    
$q = "SELECT * FROM users ORDER BY username ASC";
$user_query = mysqli_query($db_conx, $q);
$numrows = mysqli_num_rows($user_query);

    if ($selected == -1)
    {
        $first = 0;
    }
    else
    {
        $first = 1;
    }

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    
        $ret .= "\n<option value='{$row['username']}'";
        if ($selected == $row['username'] || $first == 0)
        {
            $ret .= " selected='selected'";
            $first = 1;
        }
        $ret .= ">{$row['username']}</option>";
    }
    $ret .= "\n</select>";
    return $ret;
}
 
Okay, now I know when you use a form method, you can call the name of the form to select database information. I'm not sure how to do it with the function. I need a function, as I will be using this code many times in the admin section. I can work it out with the form method, but like it this way better.

The name of the select is $ddname, but it's not a form. Any idea how to get it to recognize the $ddname name to write out the database information?

I've tried using the iffset:

Code: Select all

if(isset($_POST['$ddname ']))
    {
Thanks
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Functions

Post by KyleMassacre »

Looks like good ol' MCCodes V1.

When you use the function in a form like so:

Code: Select all

<form action="?action=editUser" method="post">
<?php echo user_dropdown("username"); ?>
</form>
You can then use it by doing something like this:

Code: Select all

<?php
if(isset($_POST['username']) && $_POST['username'] != ''){
    //do all your stuff here
}
else {
    //it failed here
}
?>
The parameter $ddname will be you post key. And also what I would do is change the function to something like this:

Code: Select all

function user_dropdown($ddname = "username", $selected = -1)
{

$ret = "<select name='$ddname' type='dropdown'>";
    
$q = "SELECT * FROM users ORDER BY username ASC";
$user_query = mysqli_query($db_conx, $q);
$numrows = mysqli_num_rows($user_query);

    if ($selected == -1)
    {
        $first = 0;
    }
    else
    {
        $first = 1;
    }

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    
        $ret .= "\n<option value='{$row['userid']}'";
        if ($selected == $row['userid'] || $first == 0)
        {
            $ret .= " selected='selected'";
            $first = 1;
        }
        $ret .= ">{$row['username']}</option>";
    }
    $ret .= "\n</select>";
    return $ret;
}
That way it uses the users id as the the option value if this is MC Codes which is what it does look like to me
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Functions

Post by Epiales »

KyleMassacre wrote:Looks like good ol' MCCodes V1.

When you use the function in a form like so:

Code: Select all

<form action="?action=editUser" method="post">
<?php echo user_dropdown("username"); ?>
</form>
You can then use it by doing something like this:

Code: Select all

<?php
if(isset($_POST['username']) && $_POST['username'] != ''){
    //do all your stuff here
}
else {
    //it failed here
}
?>
The parameter $ddname will be you post key. And also what I would do is change the function to something like this:

Code: Select all

function user_dropdown($ddname = "username", $selected = -1)
{

$ret = "<select name='$ddname' type='dropdown'>";
    
$q = "SELECT * FROM users ORDER BY username ASC";
$user_query = mysqli_query($db_conx, $q);
$numrows = mysqli_num_rows($user_query);

    if ($selected == -1)
    {
        $first = 0;
    }
    else
    {
        $first = 1;
    }

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    
        $ret .= "\n<option value='{$row['userid']}'";
        if ($selected == $row['userid'] || $first == 0)
        {
            $ret .= " selected='selected'";
            $first = 1;
        }
        $ret .= ">{$row['username']}</option>";
    }
    $ret .= "\n</select>";
    return $ret;
}
That way it uses the users id as the the option value if this is MC Codes which is what it does look like to me
I have it doing what you have above. It's getting it to write out the users database information that I cannot figure out. I have tried:

if(isset($_POST['$ddname'])

To no avail, I can't get it working. Thank you for replying so fast :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Functions

Post by Epiales »

Okay, I have put this together so far, but can't get it to display the selected username's information:

Code: Select all

<?php
function user_dropdown($ddname = "username", $selected = -1)
{


$ret = "<select name='$ddname' type='dropdown'>";
    
$q = "SELECT * FROM users ORDER BY username ASC";
$user_query = mysqli_query($db_lqls, $q);
$numrows = mysqli_num_rows($user_query);

    if ($selected == -1)
    {
        $first = 0;
    }
    else
    {
        $first = 1;
    }

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    
        $ret .= "\n<option value='{$row['ID']}'";
        if ($selected == $row['ID'] || $first == 0)
        {
            $ret .= " selected='selected'";
            $first = 1;
        }
        $ret .= ">{$row['username']}</option>";
    }
    $ret .= "\n</select>";
    return $ret;
}   
    
echo user_dropdown();

$q = "SELECT * FROM users ORDER BY username ASC WHERE username = '" . $ddname . "'";
$fetch = mysqli_fetch_array($db_conx, $q);

 ?>   
    
  <tr>

        <td width="10"><div2>User </td>
        <td width="25"><span class="div2">
          <?php echo $fetch['username'] ?>
        </span></td>
      </tr>
      <tr>

        <td>ID </td>
        <td><span class="div2">
          <?php echo $fetch['id'] ?>
        </span></td>
      </tr>
      <tr>

        <td>Email </td>
        <td><span class="div2">
          <?php echo $fetch['email'] ?>
        </span></td>
      </tr>
      <tr>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Functions

Post by Epiales »

No worries, I just went ahead and did it using the form method and all is well. Thanks! :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Advanced Help and Support”