Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Linking Functions to Data Displayed on Page

    Ok, this is probably an easy question, but since I'm new to PHP it is slowly deteriorating the mass of my brain.

    Scenario
    I have a text input box, in which you can enter a user ID. You click 'find user' and this populates a table with this users details. So far pretty straight forward.

    What I want, is a set of functions relating to this user, such as a simple button: 'Delete User' (the functionality of which is self explanatory).

    Question
    I'm having a very difficult time relating these functions to the user displayed in the table. It shouldn't be difficult, but being new to PHP I don't know if I should be calling PHP from a seperate page, where to put my variables, wondering if there's a way to do this without refreshing the page, or if I should be using cookies...there seems to be so many options and my head's full of junk. I'm finding it tough to even get on the right path to the question, never mind the answer.

    How would you do it? How do I delete this specific user (displayed on screen) from the database without having to enter their user ID again?

    In english, please

    Any help welcome.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    You didn't mention how you're getting the data (user details) for the user.

    Are you reading the data from a database?
    ... or I should ask, what kind of database?

    This might affect the answer to your question.

    Also ... how far have you gotten? Do you have the portion
    where it displays the user details? If you've gotten that far,
    adding the portion to delete them won't be too hard to do.

    a link to what you have so far might be nice to see.


    .
    Last edited by mlseim; 11-20-2006 at 05:38 PM.

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, the data in the table (user data) is coming directly from the database.

    When you enter a User ID and click 'Find User', it fires up a mySQL SELECT in a seperate php page and stores the values for that user in variables. In the page that is being displayed, I have an include so I can access the code and use the variables. The variables are then placed within the table.

    This can all be changed of course, I'm really not sure what the best approach is...



    Edit: I do have the data displayed currently. This is all working, yes.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Few ways you could approach it, depending on what you want to do.

    You could add a cell in your table that has a link in it that says "Delete username". This would point to a page called deleteUser.php?id=(for example). The id passed in the querystring would be the userid from your database. You can then run a query to delete that userid from your table.

    Another approach maybe you have a column in your table that has a checkbox, then somewhere after the table you have a delete button, which would cycle through all of the checkboxes and delete those users(this would be the case if you wanted to delete multiple users at once).

    Roughly, that's an idea.

    Good luck;

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    So I'm envisioning something like this ...

    In the script that displays the table, you then have something at
    the end of the table that has a link like this:

    <a href="myscript.php?id=123123&action=confirm">Delete This ID</a>

    It then runs a script that looks for the two variables:
    $id=$_REQUEST['id'];
    $action=$_REQUEST['id'];

    the logic would flow something like this ... the script would have two
    functions, one to confirm the deletion, and the other to actually delete it.

    if action = confirm, then display a confirmation to delete, yes or no.

    This is your data for id: $id ... blah blah blah ..
    <a href="myscript.php?id=123123&action=delete">Yes, Delete it.</a>

    if action = delete, then actually delete it from the database.

    otherwise, just go back.

    That delete script could be a PHP script all by itself to handle the
    confirmation Y or N, and the actual deletion.

    Keep in mind that once you define variables, when you go back to another
    script, those variables are still defined (they are global). So in my example,
    I used $id and $action. Make sure you're not using those anywhere else.

    Sort of a rough scenerio, but it's hard to give actual code without seeing
    what you already have. But it might get you started in the right direction.

  • #6
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much.

    I did write out a long list of questions...but in reality, all I need to know is this:

    Quote Originally Posted by brandoe
    ]You could add a cell in your table that has a link in it that says "Delete username". This would point to a page called deleteUser.php?id=(for example). The id passed in the querystring would be the userid from your database.
    How do I get the User ID into the url? Obviously this can't be hardcoded. I mean I may not be creating a the next mySpace, but the whole point of having a member based system is to have more than one member
    Last edited by php_leg_end; 11-20-2006 at 07:23 PM.

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yes, you generate the link with the userid from your table, e.g when you are displaying all the other info in the table it's merely taking the row variable from the database and tacking it onto the querystirng.

    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
       
    // building a table in your loop? use the userid from here and put it at the end of your querystring

    Posting your code where you are building the table will allow us to show you in example.

    Good luck;

  • #8
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again.

    I don't have any loops. I'm only displaying the one user on the page, based on a search for the user id.
    Last edited by php_leg_end; 11-20-2006 at 08:16 PM.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    yea ... if it's displaying the ID in the table, then it must
    already be a variable ...

    something like this:

    <a href="myscript.php?id=<?=$id?>&action=confirm">Delete This ID</a>

  • #10
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Right, loop or no loop the variable is there.
    mlseim has it.

  • #11
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha..thank you

    I did try this, but had no '=' in my <?=$id?> which meant no results.

    All working now....time to kick some data's ***.

    Much appreciated.

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    great ...

    if you get stuck on the confirmation/delete code, post some of it
    and we'll see what we can do. I just think it's important to at least
    give a confirmation of YES or NO before you delete anything.

  • #13
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still having a struggle with this...mainly with my if statements...

    I'm currently saying something like this:

    if($_REQUEST['userID'])
    {
    then find the user and display user on screen
    }
    but then, if I do this:

    if($_REQUEST['delete'])
    {
    grab the user id and delete that user
    }
    ...it has a tendency to also run the code in the first if too (request userID). I don't believe I'm using a feasible method here...any help? What should I be using to say that the user has clicked a specific button then run the code on it?

    Or should I have the code for each function in a seperate page (Surely that's not necesarry)?

  • #14
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    On the page, you might have the link to delete user something like this ...

    <a href="myscript.php?userID=<?=$userID?>&action=confirm">Delete This ID</a>



    Then, in your script, you grab the variables ($userID and $action) ...

    $userID=$_REQUEST['userID'];
    $action=$_REQUEST['action'];

    if($action=="confirm"){
    //confirm deletion
    }
    if($action=="delete"){
    //delete user
    }

  • #15
    New Coder
    Join Date
    Nov 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I think I'm beginning to understand it better now. 'Beginning' being the most appropriate word...

    I've been out and bought myself a book too, so maybe I'll actually get somewhere.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •