Re: compare arrays problem

From: Date: Mon, 19 Feb 2001 07:03:44 +0000
Subject: Re: compare arrays problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40528@lists.php.net to get a copy of this message
> $pages=mysql_query("SELECT CP.page_id, pagename FROM cluster_pagetbl as CP, > pagetbl WHERE > CP.cluster_id = '$id' AND CP.page_id=pagetbl.page_id order by page_id"); > > /* > SQL Result for $id=1: > page_id pagename > 1 breakingnews > 3 weather > */ > > if (mysql_Numrows($pages)>0) { > > $prows=mysql_NumRows($pages); > $i=0; > while ($i<$prows){ > > //figure out how to get page ids into array > $pid= mysql_result($pages,$i, page_id); > $pagename =mysql_result($pages, $i, pagename); > > $pgids= array("$pid" => "$pagename"); This is *rebuilding* the array from scratch on each "page". > foreach($pgids as $pid => $pagename){ > print $pid.' => '.$pagename.'<br>'; > } This is *inside* the loop, so shows you the first array, with page 1, and then the second array, with page 3. But you never really had an array with *both* pages in it at once. Change the $pgids = array($pid => $pagename); line to just: $pgids[$pid] = $pagename; Now, since you probably want $pgids to be an array even if there are *no* pages matching the criteria, insert a line *before* the loop: $pgids = array(); This will guarantee that $pgids is always an array. > /* prints: > 1 => breakingnews > 3 => weather > > At this point the correct number of values appear to be in the pgids array > */ > > $i++; > } > } > > $query2=mysql_query("select page_id, pagename FROM > pagetbl order by page_id"); > > /* > SQL Result: > page_id pagename > 1 breakingnews > 2 pastnews > 3 weather > */ > You'll want to reset($pgids) before you use it again. > if (mysql_Numrows($query2)>0) { > > $numrows=mysql_NumRows($query2); > $x=0; > while ($x<$numrows){ > > $mpid=mysql_result($query2,$x, page_id); > $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 == $mpgids){ print " checked"; } You need to keep track of which $pgid[] you are on -- Keep a running counter or something so that after you have $pgids[1] and $mpgids[1], you'll know that you are on $pgids[3] and $mpgids[2], and then $pgids[3] (still) and $mpgids[3]. In other words, you need two distinct iterators going in parallel. > print '>'.$mpagename; > } > > // prints out three checkboxes, since that's the total number of pages > present > //but only the third checkbox (weather) gets checked, the first one should > be checked also > > $x++; > } > } > > file://I used the array intersect function to doublecheck what is going on and it > finds only weather also > file://What is happening to the first value? > > $diff = array_intersect($mpgids,$pgids); > > foreach ($diff as $element){ > print '<p>'.$element.'<br>';} > > file://prints: weather > > ---------------------------------------------------------------------------- ---- > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#40528) next »