Re: foreach in reverse [message #174183 is a reply to message #174180] |
Wed, 25 May 2011 02:01 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma:
|
Senior Member |
|
|
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?
>
> <?php
> 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 " <div class='news'>\n";
> require_once $docRoot . '/news/' . $filename . '.php';
> print " </div>\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
|
|
|