Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP] compare arrays problem

0 views
Skip to first unread message

Onaje Johnston

unread,
Feb 17, 2001, 5:45:34 PM2/17/01
to
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
*/

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

foreach($pgids as $pid => $pagename){
print $pid.' => '.$pagename.'<br>';
}

/* 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
*/

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

$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

Richard Lynch

unread,
Feb 19, 2001, 3:14:54 AM2/19/01
to
> $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...@lists.php.net
> For additional commands, e-mail: php-gene...@lists.php.net
> To contact the list administrators, e-mail: php-lis...@lists.php.net


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general...@lists.php.net
For additional commands, e-mail: php-gene...@lists.php.net
To contact the list administrators, e-mail: php-lis...@lists.php.net

0 new messages