#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Compare two arrays, execute checkbox list..


    I'm trying to match any array element (id) from query one and two below but when I run script I get nothing checked but know I have them in db.

    I have two tables
    esu_checks - Fields: vidID, id, groupID
    esu_videotypes - Fields: id, CategoryName

    PHP Code:

    $query
    ="SELECT id, CategoryName FROM esu_videotypes"
    $result=mysql_query($query);
    //there are three rows

    $query "SELECT esu_checks.id, esu_checks.vidID, esu_checks.groupID, esu_videotypes.id, esu_videotypes.CategoryName FROM esu_videotypes, esu_checks WHERE esu_checks.id = esu_videotypes.id AND esu_checks.vidID = ".$_GET['editarchive']." AND esu_checks.groupID = '1'";
    $rescardsr=mysql_query($query); 
    //there are two rows

    $int= array(); // 
    while($int[] = mysql_fetch_array($rescardsr))
    {  
    }

    //List all 3 options from first query, with the two matches outputed in second, and have them 'checked'...

    while($arraymysql_fetch_array($result)) 

        if (
    in_array($array[id], $int)){
        echo 
    "<input type='checkbox' name='vidtype[]' value='$array[id]' checked>$array[CategoryName]<br>\n"
        } else {
        echo 
    "<input type='checkbox' name='vidtype[]' value='$array[id]'>$array[CategoryName]<br>\n"
        }

  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Any help out there?
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Please don't bump your threats, especially when your first post was at 4:30am.

    Are you doing any sort of debugging here? Have you printed the values you're getting back from either of these calls? Have you handled any MySQL errors? Do you have error_reporting turned on?

    All I can tell from reading your code is that you're a new user who isn't using array indicies properly (which means your error_reporting is definitely not turned on). If you turn on errors you'll see that PHP is doing automated critiques of your code, and may be pointing out where your problem is already, without you having to rely on strangers to read your code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    I have different levels of error reporting on my server, I do have them turned on, but not sure about the specificity as it has different types.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    PHP Code:
    error_reporting(E_ALL); 
    That will turn on errors in this script.

    You still need to be doing manual error-handling and debugging here. Print the lists, echo the values in the loops, make sure you know what's actually happening.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Actually, I figured it out already.

    Aside from the misuse of array index values (they should be quoted) and the inefficiencies of your queries:

    $int contains an array of arrays. Every element of $int is a row from esu_videotypes. So your in_array is looking at the top-level array and not finding the ID. You need to make $int an array of just the ID (I assume) of the items in that table.

    You could do ALL of this in a single query, however, using LEFT JOIN.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    Actually, I figured it out already.

    Aside from the misuse of array index values (they should be quoted) and the inefficiencies of your queries:

    $int contains an array of arrays. Every element of $int is a row from esu_videotypes. So your in_array is looking at the top-level array and not finding the ID. You need to make $int an array of just the ID (I assume) of the items in that table.

    You could do ALL of this in a single query, however, using LEFT JOIN.
    Maniac Dan,

    I'm a bit confused, make $int an array of which table? Could you provide an example?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all, you have some huge security holes in your code. If you upload this code (which I hope you haven't yet), anybody with Internet access can mess with your database or even take over the whole server. See The 6 worst sins of security.

    Secondly, you missed the most important information: You want a join. Associating tables is what SQL is made for. That's what it's good at. You can abuse PHP for "emulating" a join, but then you'll end up writing a lot of unnecessary, inefficient and unreliable code.

    What you're trying to do is a classical case for a LEFT JOIN (as has been mentioned already). When you left-join a table A with a table B, then the rows of A are associated with the rows of B according to the join conditions. However, if a row in A has no matching row in B, it will be associated with a row filled with NULLs. So every row in A is guaranteed to appear in the result set.

    sql Code:
    SELECT
    	esu_videotypes.id
    	, esu_videotypes.categoryName
    	, esu_checks.id IS NOT NULL AS has_check	-- if esu_checks.id is NULL, that means MySQL didn't find a corresponding check and filled the row with NULLs
    FROM
    	esu_videotypes
    	LEFT JOIN esu_checks ON esu_videotypes.id = esu_checks.id
    -- ... and the WHERE clause
    ;
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    first of all, you have some huge security holes in your code. If you upload this code (which I hope you haven't yet), anybody with Internet access can mess with your database or even take over the whole server.

    Secondly, you missed the most important information: You want a join. Associating tables is what SQL is made for. That's what it's good at. You can abuse PHP for "emulating" a join, but then you'll end up writing a lot of unnecessary, inefficient and unreliable code.

    What you're trying to do is a classical case for a LEFT JOIN (as has been mentioned already). When you left-join a table A with a table B, then the rows of A are associated with the rows of B according to the join conditions. However, if a row in A has no matching row in B, it will be associated with a row filled with NULLs. So every row in A is guaranteed to appear in the result set.

    sql Code:
    SELECT
    	esu_videotypes.id
    	, esu_videotypes.categoryName
    	, esu_checks.id IS NOT NULL AS has_check	-- if esu_checks.id is NULL, that means MySQL didn't find a corresponding check and filled the row with NULLs
    FROM
    	esu_videotypes
    	LEFT JOIN esu_checks ON esu_videotypes.id = esu_checks.id
    -- ... and the WHERE clause
    ;

    Thanks that worked marvelously, as it's outputting two checked boxes with it's associated category name in correlation to the db, but I have a remaining issue.

    My target table, videotypes contains 3 total rows that always need to be displayed. How would I approach adding the 3rd check box (that I'm assuming would be null and whose row/value isn't in my db) as displayed unchecked?

    This is what I came up with below.

    PHP Code:

    $query 
    "SELECT esu_videotypes.id, esu_videotypes.CategoryName, esu_checks.id IS NOT NULL AS has_check    
    FROM esu_videotypes LEFT JOIN esu_checks ON esu_videotypes.id = esu_checks.id
    WHERE esu_checks.vidID = "
    .$_GET['editarchive']."";

    $result mysql_query($query) or die(mysql_error());

    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {
            echo 
    "<input type=\"checkbox\" name=\"vidtype[]\" value=\"$row[id]\"";
            if (
    $row['has_check'] != NULL){
                echo 
    " checked";
            } 
                echo 
    "> $row[CategoryName]<br>";
            }  

    //outputs 2
    //<input type="checkbox" name="vidtype[]" value="2" checked> // Primary Care<br><input type="checkbox" name="vidtype[]" //value="3" checked> Collaborative
    //but need 3rd here unchecked...
    //<input type="checkbox" name="vidtype[]" value="1"> //name.. 
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    SECURITY

    There's absolutely no point in fumbling with details as long as the whole code needs a rewrite. First fix the database code and the security holes. Then worry about the rest.

    I guess I shouldn't have posted the query, because as soon as people get something to work, they stop listening.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo