[PHP] Re: compare arrays problem

2001-02-19 Thread Onaje Johnston

Thank you Tim and Richard. With your help I was able to figure out how to
get this script working as I intended.
Here's the code, in case anyone wants to do something similar in the future.
:-))

$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");

$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[$pid] = $pagename;

$i++;
}
}

//prints what is in the pgid array. 
foreach($pgids as $pid => $pagename){
 print $pid.' => '.$pagename.'';
}

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

$mpgids= array();

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[$mpid] = $mpagename;   
  
$x++;
}
}

//prints the checkboxes, compares pgids and mgids array
foreach ($mpgids as $mpid => $mpagename){
print ''.$mpagename;
}

//double check to see what values are in both arrays, then print it.
$diff = array_intersect($mpgids,$pgids);

foreach ($diff as $element){
print ''.$element.'';}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]


[PHP] RE: compare arrays problem

2001-02-19 Thread Tim Ward

your problem is where you are creating the array, see my notes

> -Original Message-
> From: Onaje Johnston [mailto:[EMAIL PROTECTED]]
> Sent: 17 February 2001 22:47
> To: [EMAIL PROTECTED]
> 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.'';
> }
> 
> /* 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 ' 
> 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 ''.$element.'';} 
>  
> //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
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]