SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: compare arrays

  1. #1
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need some help figuring out how to compare the values in two arrays to each other.

    (data from db for first query)
    cluster_id page_id pagename clustername
    ----------------------------------------------
    1 1 breakingnews newspages
    1 3 weather newspages

    //put page ids associated with a cluster into an array

    for($i = 0; $i < $numrows; $i++) {
    $pid= mysql_result($result,$i, page_id);
    $pagename =mysql_result($result, $i, pagename);

    $pgids= array("$pid" => "$pagename");


    (data from db for second query)
    pid pagename
    ---------------------
    1 breakingnews
    2 pastnews
    3 weather

    //get all page ids, print checkboxes, preselect checkbox if page id associated with a cluster

    while ($x<$numrows){

    $mpid=mysql_result($query2,$x, pid);
    $mpagename=mysql_result($query2,$x, pagename);

    $mpgids= array("$mpid" => "$mpagename");

    while (list($mpid, $mpagename) = each ($mpgids)){
    print '<input type=checkbox name=page_ids[] value="'.$mpid;

    //this where it's not working - compare the values in the arrays to each other
    I don't know what to do here, I tried $pgids[$mpid] == $mpgids[$mpid]- none of the checkboxes were automatically checked, but there are two page ids that are in both arrays.

    if ($pgids == $mpgids){ print " checked"; }

    print '">'.$mpagename;
    }

    $x++;
    }

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($pgids == $mpgids){ print " checked"; }

    In this you haven't closed the open quote for value="
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After fixing my print statement, only the third checkbox is checked. It should be the first and the third. I'm still not doing something right.

    Any help appreciated.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you are creating an array you need to use the array key

    $pgids[$pid] == $mpgids[$mpid]


    But I really think there has got to be a much easier way to do what you are trying to do. Could you explain a little more about how it supposed to work?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy -> more explanation: There are three tables - clusrertbl, paagetbl and cluster_pagetbl. The cluser table has the cluster id and name, page table has the page id and name, the cluster page table has cluster id and page id (which page is associated with which cluster i.e. cluster 1- page 1 or cluster 1- page 3)

    I have an insert script that's working already, that inserts the cluster id and page id into the cluster page table like so:

    for ($index=0; $index< count($page_ids); $index++){
    $clstentry =mysql_query("INSERT INTO cluster_pagetbl (
    cluster_id,page_id) VALUES ('$cid','$page_ids[$index]')");
    } //end for

    The modify/update script is the problem:
    The first query gets the cluser-page associations from the cluster_pagetbl, the second query gets all the pages from the page table and prints checkboxes for all of them. What I want to do is automatically have a checkbox be checked if the page id is accociated with a cluster in the cluster page table.

    When I submit the page for updates I'm thinking that I would either delete all rows from the cluster page table with a specific cluster id, then insert everything from scratch. Or do a search to see if a cluster id-page id combination already exsists, and only insert for a combination that isn't in the table already.

    I am going to try your suggestion in the meantime.

  6. #6
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Since you are creating an array you need to use the array key $pgids[$pid] == $mpgids[$mpid] "

    Using this results in none of the checkboxes being checked.
    Here's the how I'm getting all the page ids from the page table then trying to compare them to the page ids in the cluster page table:

    $query2=mysql_query("select page_id as pid, pagename FROM
    pagetbl order by page_id");

    if (mysql_Numrows($query2)>0) {
    $numrows=mysql_NumRows($query2);
    $x=0;

    while ($x<$numrows){

    $mpid=mysql_result($query2,$x, pid);
    $mpagename=mysql_result($query2,$x, pagename);

    $mpgids= array("$mpid" => "$mpagename");

    foreach ($mpgids as $mpid => $mpagename){
    print '<input type=checkbox name=page_ids[] value="'.$mpid.'"';
    if ($pgids[$pid] == $mpgids[$mpid]){ print " checked"; }
    print '>'.$mpagename;
    }

    $x++;
    }
    }


Bookmarks

Posting Permissions

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