FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » foreach in reverse
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
foreach in reverse [message #174180] Wed, 25 May 2011 00:43 Go to next message
Evolution is currently offline  Evolution
Messages: 14
Registered: April 2011
Karma: 0
Junior Member
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";
}
?>

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.
Re: foreach in reverse [message #174181 is a reply to message #174180] Wed, 25 May 2011 01:40 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/24/2011 8:43 PM, 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";
> }
> ?>
>
> 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.
>

http://us3.php.net/manual/en/function.array-reverse.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: foreach in reverse [message #174182 is a reply to message #174180] Wed, 25 May 2011 01:44 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/24/2011 8:43 PM, Evolution wrote:


Hit send too fast...

Also - session_register() has been deprecated and shouldn't be used.
However, session_start() is still alive and well, and needed if you use
sessions (unless you have PHP set to auto-start sessions, of course).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: foreach in reverse [message #174183 is a reply to message #174180] Wed, 25 May 2011 02:01 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
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
Re: foreach in reverse [message #174184 is a reply to message #174181] Wed, 25 May 2011 05:11 Go to previous messageGo to next message
Evolution is currently offline  Evolution
Messages: 14
Registered: April 2011
Karma: 0
Junior Member
On May 24, 6:40 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 5/24/2011 8:43 PM, 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";
>>              }
>>        ?>
>
>> 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.
>
> http://us3.php.net/manual/en/function.array-reverse.php
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Thank you! It worked perfectly!
Re: foreach in reverse [message #174185 is a reply to message #174183] Wed, 25 May 2011 06:05 Go to previous messageGo to next message
Helmut Chang is currently offline  Helmut Chang
Messages: 22
Registered: September 2010
Karma: 0
Junior Member
Am 25.05.2011 04:01, schrieb Peter H. Coffin:
> On Tue, 24 May 2011 17:43:32 -0700 (PDT), Evolution wrote:
>> Also, I read somewhere that session_register() and session_start() are
>> depricated and should not be used.
> [...]
> Whatever thing gave you the idea that they were deprecated was.. uhm..
> wrong.

Partly. session_register() is deprecated since 5.3. And it's usage is
discouraged since 4.1. Of course, session_start() is not deprecated.

Helmut
Re: foreach in reverse [message #174186 is a reply to message #174180] Wed, 25 May 2011 06:19 Go to previous messageGo to next message
Helmut Chang is currently offline  Helmut Chang
Messages: 22
Registered: September 2010
Karma: 0
Junior Member
Am 25.05.2011 02:43, schrieb Evolution:

> Also, I read somewhere that session_register() and session_start() are
> depricated and should not be used.

You should always consult the manual first for such questions:

<http://www.php.net/manual/en/function.session-register.php>

It's indeed deprecated since 5.3. It's the very, very "old" and
unsecure(?) way to register a variable in the session.

> However, when removed them, some
> code seems to have stopped working.

Yes, because on the other hand, session_start() is not deprecated, but
necessary to (aas the name indicates) start the session.

Again: consult the manual:

<http://www.php.net/manual/en/function.session-start.php>

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

First: you should turn on error reporting. The above code should give at
least one warning, because session_register() expects at least one
parameter.

Second: trying to register a variable (what you don't in your usage of
session_register(), because you don't pass the name of a variable to
register) before you start the session...?

On the other hand, session_register() implicitly starts the session, if
none has been started already. So the following call of session_start()
would raise a notice, that the session already had been started.

And last: I wouldn't use the variable $html_title, if it's not needed
elsewhere in the code. It'sjust a tiny issue. But in this way, you
create a variable and in the next line, you create a copy of it in
assigning it to the session, while the original variable is not used
anymore.

So my opinion of a proper code here would be:

// Start the session:
session_start();

// Write data to the session:
$_SESSION['html_title'] = 'UCSB Earth Science : Home';

HTH, Helmut
Re: foreach in reverse [message #174188 is a reply to message #174180] Wed, 25 May 2011 15:07 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 24 May 2011 17:43:32 -0700, 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.

Your array appears to be sortable in (reverse) date order.

Try the following in the php cli:

<?php

$newsbrief[] = "110101_oldest";
$newsbrief[] = "110303_newest";
$newsbrief[] = "110202_middle";

rsort($newsbrief);

foreach ($newsbrief as $filename)
{
echo "$filename\n";
}
?>

> Also, I read somewhere that session_register() and session_start() are
> depricated and should not be used.

<http://uk3.php.net/manual/en/function.session-start.php>

Nothing there about session_start being deprecated

session_register is deprecated, see:

<http://uk3.php.net/manual/en/function.session-register.php>

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

Just remove the "session_register();" line. I don't think it does what
you think it is doing here. session_register() is used to register
session global variables.

As far as I can tell, session_register() called _with_no_parameters_
*before* session_start() will just call session_start(), and
session_register() called _with_no_parameters_ *after* session_start()
will do nothing at all.

Use the superglobal $_SESSION[] array instead of registering variables
with session_register().

Rgds

Denis McMahon
Re: foreach in reverse [message #174193 is a reply to message #174184] Thu, 26 May 2011 11:59 Go to previous message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 5/25/2011 1:11 AM, Evolution wrote:
> On May 24, 6:40 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 5/24/2011 8:43 PM, 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";
>>> }
>>> ?>
>>
>>> 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.
>>
>> http://us3.php.net/manual/en/function.array-reverse.php
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Thank you! It worked perfectly!

Another thing to note is that _if_ the elements of the array are being
filled from a SQL call, you could simply have an "ORDER BY ID DESC" at
the end of the call where ID is the autoincrement index that is created
when you added each element to the table. Then your foreach would work
right away.

--
Shelly
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: problem saving date fields
Next Topic: furniture
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 12:38:34 GMT 2024

Total time taken to generate the page: 0.04621 seconds