RE: compare arrays problem

From: Date: Mon, 19 Feb 2001 10:15:22 +0000
Subject: RE: compare arrays problem
Groups: php.general 
Request: Send a blank email to php-general+get-40551@lists.php.net to get a copy of this message
your problem is where you are creating the array, see my notes > -----Original Message----- > From: Onaje Johnston [mailto:onaje@worldnet.att.net] > Sent: 17 February 2001 22:47 > To: php-general@lists.php.net > Subject: compare arrays problem > > > I am hoping that someone really understands arrays reads this > and can tell > me why the following isn't working as I expect it to. I've > been trying for > days to get it to work. > > $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 > */ declare the array here: $pgids= array(); > > 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 reinitialises the array for each row of the answer to your query, what you should do here is: $pgids["$pid"] = "$pagename"; > > foreach($pgids as $pid => $pagename){ > print $pid.' => '.$pagename.'<br>'; > } > > /* prints: > 1 => breakingnews > 3 => weather prints each row but at no point are both in the array. on each pass only the current row is in your array. > > At this point the correct number of values appear to be in > the pgids array > */ no they are not, only one is in at a time > > $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 > */ > > 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"; } > 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 that's because your array only contains the last row ... > > $x++; > } > } > > //I used the array intersect function to doublecheck what is > going on and it > finds only weather also > //What is happening to the first value? > > $diff = array_intersect($mpgids,$pgids); > > foreach ($diff as $element){ > print '<p>'.$element.'<br>';} > > //prints: weather > Tim Ward Senior Systems Engineer Please refer to the following disclaimer in respect of this message: http://www.stivesdirect.com/e-mail-disclaimer.html

« previous php.general (#40551) next »