1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

If statement to check $id with row id problem

Discussion in 'Programming' started by Warwick Lyons, Feb 28, 2013.

  1. #1
    Hi I am trying to check the page user $id against the id of a row from the wordpress database and if they equal print first_name and family-name information.

    The code I am trying to use is but I cannot seem to get it to work.

    `global $wpdb;

    $result = $wpdb->get_results( "SELECT * FROM wp_lb_person");


    if (id == $id) {

    echo $row->first_name." ".$row->family_name;

    }
    else
    {
    echo 'Not Working';
    }`

    Any advise or assistance would be appreciate.

    This code is added to the page via shortcode

    Regards

    Warwick
     
    Warwick Lyons, Feb 28, 2013 IP
  2. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    165
    #2
    if (id == $id) {

    what is "id" in this statement ?

    Also, this
    $result = $wpdb->get_results( "SELECT * FROM wp_lb_person");
    Returns a list of all your persons, yet you don't loop through them ?
     
    kajakske, Feb 28, 2013 IP
  3. Warwick Lyons

    Warwick Lyons Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks for the prompt reply.

    id is the row id

    ie if the row id is equal to the user id variable.

    If I have left out any code can you point me in the right direction
     
    Warwick Lyons, Feb 28, 2013 IP
  4. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    165
    #4
    Untested code:

    $result = $wpdb->get_results( "SELECT * FROM wp_lb_person");
    foreach ($result as $person) {
    if ($person['id'] == $id) { // Note if this is an object you should use $person->id
    echo $row->first_name." ".$row->family_name;
    }
    }
     
    kajakske, Feb 28, 2013 IP
  5. Warwick Lyons

    Warwick Lyons Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Hi


    Thanks . You are a jet!

    That code appears to have fixed the problem.

    Much appreciated!
     
    Warwick Lyons, Feb 28, 2013 IP
  6. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    165
    #6
    Obviously
    echo $row->first_name." ".$row->family_name;
    should be
    echo $person->first_name." ".$person->family_name;
     
    kajakske, Feb 28, 2013 IP
  7. Warwick Lyons

    Warwick Lyons Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    To be honest, I did miss that at first but picked it up on the 3rd attempt.

    Thanks again!!
     
    Warwick Lyons, Feb 28, 2013 IP