How to get the value out of this

How to get the value out of this

whitbaconwhitbacon Posts: 40Questions: 2Answers: 0
edited June 2013 in Editor
[code]
if ( $_POST['action'] === 'edit' ) {
$checkdup = $db ->select( 'users', 'member_id', array( 'usr' => $val ) ) ->count();
if($checkdup == 1 and $checkdup.member_id[0] <> $_POST['id']){
return "Duplicate username.";
}
}

[/code]


I am trying to make a datatable for users and on edit I want to check the value of the member id and if it is not the same as being posted. clealy my problem is here:

$checkdup.member_id[0]

I have also tried

db.member_id

How do I get the id of the returned result and compare it to the posted id?

Thanks

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The `$checkdup` variable is being assigned as an integer there.

    I think what you probably want is something like this:

    [code]
    if ( $_POST['action'] === 'edit' ) {
    $checkdup = $db ->select( 'users', 'member_id', array( 'usr' => $val ) ) ->fetchAll();
    if( count($checkdup) == 1 and $checkdup[0]['member_id'] <> $_POST['id']){
    return "Duplicate username.";
    }
    }
    [/code]

    Note the use of `fetchAll` rather than `count` . It then simply uses the PHP count function and the array will have one item in it with information about the row selected. If the count() fails, the second part of the condition won't execute since PHP short cuts conditions like that.

    Allan
  • whitbaconwhitbacon Posts: 40Questions: 2Answers: 0
    edited June 2013
    Allan,

    Thank you so much! That almost worked perfectly.

    the only thing I needed to change was add "row_"

    as follows:

    [code]
    if ( $_POST['action'] === 'edit' ) {
    $checkdup = $db ->select( 'users', 'member_id', array( 'usr' => $val ) ) ->fetchAll();
    if( count($checkdup) == 1 and "row_".$checkdup[0]['member_id'] <> $_POST['id']){
    return "Duplicate username in system, please try something else!"

    }
    }
    [/code]
This discussion has been closed.