Page 1 of 1
Functions
Posted: Tue Jun 30, 2015 4:05 am
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:
Thanks
Re: Functions
Posted: Tue Jun 30, 2015 4:40 am
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
Re: Functions
Posted: Tue Jun 30, 2015 4:51 am
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
Re: Functions
Posted: Tue Jun 30, 2015 6:06 am
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>
Re: Functions
Posted: Tue Jun 30, 2015 7:58 am
by Epiales
No worries, I just went ahead and did it using the form method and all is well. Thanks!