Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Peter H. Coffin" Newsgroups: comp.lang.php Subject: Re: foreach in reverse Date: Tue, 24 May 2011 21:01:43 -0500 Organization: A noiseless patient Spider Lines: 66 Message-ID: References: <54289e0b-89c3-4bc5-9077-77a2568b66fe@q12g2000prb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="le/2G4+BNpieVhQI6uJ2Lw"; logging-data="20772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+G9K3uJkvimcVnCS+hW0VI" User-Agent: slrn/0.9.9p1 (OpenBSD) Cancel-Lock: sha1:355Z9/nCkgAjK+JG4B4r//zyaVA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.php:1835 On Tue, 24 May 2011 17:43:32 -0700 (PDT), Evolution wrote: > I wrote the following code to include a succession of 7 files on my > news page and it works fine. However, if I create a new news brief, > it should be indexed 7 and appear at the top of the page. I am > guessing that I would probably have to use a for loop and not foreach > so that I start with the highest index and decrement down (where > 110106_Valentine (below) would be indexed 0), is that the only way? > > session_register(); > session_start(); > $docRoot = getenv("DOCUMENT_ROOT"); > > $newsbrief[0] = "110422_Mendes"; > $newsbrief[1] = "110411_Livsey"; > $newsbrief[2] = "110404_Archuleta"; > $newsbrief[3] = "110315_Tanimoto"; > $newsbrief[4] = "110312_Ji"; > $newsbrief[5] = "110215_Lea"; > $newsbrief[6] = "110106_Valentine"; > > foreach ( $newsbrief as $filename ) > { > print "
\n"; > require_once $docRoot . '/news/' . $filename . '.php'; > print "
\n\n"; > } > ?> It's not the only way. You could, for example, use foreach and build your array by doing something like $my_array = array_splice($my_array, 0, 0, "new item goes here"); which tacks the new thing onto the FRONT of the array instead of the end, and pushes all the previously existing ones further up the index order. > Also, I read somewhere that session_register() and session_start() are > depricated and should not be used. However, when removed them, some > code seems to have stopped working. Below is an example of how I've > been using session_register() and session_start(). What code would > be proper here? > > session_register(); > session_start(); > $html_title="UCSB Earth Science : Home"; > $_SESSION['html_title'] = $html_title; > > Thanks a bunch. Whatever thing gave you the idea that they were deprecated was.. uhm.. wrong. You *may*, however, wish to start paying attention to where you put session_start(); calls, as doing so tends to lead to things like preventing you from sending additional headers, and causing complications if you're trying to use named sessions and different parts of code end up expecting different names to be the important and active session. A common though sloppy practice is to sprinkle them around at the front of every chunk of code just to make really really really sure there's always an active session handler, and eventually there may be actual consequences for doing that. -- I got told by a friend's ex-girlfriend that she could tell I was a Linux geek from the way I *walked*. -- Skud