RE: Populate Array During Loop With in_array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



OK, so here's the solution I came up with. A little harder to read here than
in an editor, but it's as clean and tight as I could make it. I use it as
the body of a function that returns $displayThese to a script uses the
newsID values to display those news items that have not expired AND the user
has not marked as read. Your mileage may vary ;^)

		$sql_getNews = "SELECT newsID FROM news WHERE CURDATE()
BETWEEN startDate AND endDate";

		$result_getNews = mysql_query($sql_getNews)
			or die(mysql_error());

		$sql_getReadItems = "SELECT newsID FROM newsReadBy WHERE
userID='".$creatorID."'";

		$result_getReadItems = mysql_query($sql_getReadItems)
			or die(mysql_error());

		$readList = mysql_fetch_array($result_getReadItems);//turn
Resource ID into an assoc. array

		//check to see if a valid newsID ($newsList["newsID"]) has
already been read (appears in $readList)
		while($newsList = mysql_fetch_array($result_getNews)){
			if($readList){
				if (!in_array($newsList["newsID"],
$readList)){
					$displayThese[] =
$newsList["newsID"];
				}
			}
			else{
				$displayThese[] = $newsList["newsID"];
			}
		}

Rich

> -----Original Message-----
> From: Jeffrey_N_Dyke@Keane.com [mailto:Jeffrey_N_Dyke@Keane.com]
> Sent: Thursday, March 06, 2003 11:36 AM
> To: Hutchins, Richard
> Cc: php-db@lists.php.net
> Subject: RE:  Populate Array During Loop With in_array
> 
> 
> 
> personally, i think solutions are always worthwhile, no 
> matter how trivial.
> that way when someone is searching the archives, they will find the
> solution in the thread.
> 
> just my $.02
> Jeff
> 
> 
>                                                               
>                                                                
>                     "Hutchins, Richard"                       
>                                                                
>                     <Richard.Hutchins@Getin       To:     
> php-db@lists.php.net                                               
>                     geusa.com>                    cc:         
>                                                                
>                                                   Subject:    
>  RE:  Populate Array During Loop With in_array         
>                     03/06/2003 11:33 AM                       
>                                                                
>                                                               
>                                                                
>                                                               
>                                                                
> 
> 
> 
> 
> Rich,
> 
> Thanks! You pointed me in the right direction and I now have the code
> working and returning the expected results. If anybody's 
> interested, just
> post back to the list and I'll post the working code. Don't 
> want to bog the
> list down if nobody cares.
> 
> Rich
> 
> > -----Original Message-----
> > From: Rich Gray [mailto:rich@f1central.net]
> > Sent: Thursday, March 06, 2003 10:05 AM
> > To: Hutchins, Richard; php-db@lists.php.net
> > Subject: RE:  Populate Array During Loop With in_array
> >
> >
> > > Can somebody please tell me why the $displayThese variable
> > in the code
> > > snippet below only gets populated with the last value in the
> > > $newsList array
> > > even when $newsList has more than one row of data?
> > >
> > >       while($newsList = mysql_fetch_array($result_getNews)){
> > >            if($convReadList != ""){
> > >                 for($i = 0; $i < $countNewsItems; $i++){
> > >                      if (!in_array($newsList[$i],
> > > $convReadList)){
> > >                           //echo
> > $newsList[$i]." was
> > > found\n";
> > >                           $displayThese[$i] =
> > > $newsList[$i];
> > >                      }
> > >                 }
> > >            }
> > >            else{
> > >                 echo "<p>readList is empty</p>";
> > >            }
> > >       }
> > >       print_r($displayThese);
> > >
> > >
> >
> > You're resetting $i back to 0 for each row returned from the
> > database. So if
> > there are multiple rows (which you say there are) then the
> > array entries
> > will get overwritten each time...
> >
> > HTH
> > Rich
> >
> 
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 
> 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux