Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #1832 > unrolled thread

foreach in reverse

Started byEvolution <cryptozoan@gmail.com>
First post2011-05-24 17:43 -0700
Last post2011-05-25 15:07 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  foreach in reverse Evolution <cryptozoan@gmail.com> - 2011-05-24 17:43 -0700
    Re: foreach in reverse Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-24 21:40 -0400
      Re: foreach in reverse Evolution <cryptozoan@gmail.com> - 2011-05-24 22:11 -0700
        Re: foreach in reverse sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 07:59 -0400
    Re: foreach in reverse Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-24 21:44 -0400
    Re: foreach in reverse "Peter H. Coffin" <hellsop@ninehells.com> - 2011-05-24 21:01 -0500
      Re: foreach in reverse Helmut Chang <usenet@helmutchang.at> - 2011-05-25 08:05 +0200
    Re: foreach in reverse Helmut Chang <usenet@helmutchang.at> - 2011-05-25 08:19 +0200
    Re: foreach in reverse Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-05-25 15:07 +0000

#1832 — foreach in reverse

FromEvolution <cryptozoan@gmail.com>
Date2011-05-24 17:43 -0700
Subjectforeach in reverse
Message-ID<54289e0b-89c3-4bc5-9077-77a2568b66fe@q12g2000prb.googlegroups.com>
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.

[toc] | [next] | [standalone]


#1833

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-05-24 21:40 -0400
Message-ID<irhmn8$6qt$1@dont-email.me>
In reply to#1832
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#1836

FromEvolution <cryptozoan@gmail.com>
Date2011-05-24 22:11 -0700
Message-ID<bb509717-28a8-4a78-a81d-e9b73de5caa2@x38g2000pri.googlegroups.com>
In reply to#1833
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!

[toc] | [prev] | [next] | [standalone]


#1846

Fromsheldonlg <sheldonlg@thevillages.net>
Date2011-05-26 07:59 -0400
Message-ID<irlfbk$vhr$1@dont-email.me>
In reply to#1836
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

[toc] | [prev] | [next] | [standalone]


#1834

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-05-24 21:44 -0400
Message-ID<irhmtm$6qt$2@dont-email.me>
In reply to#1832
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#1835

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2011-05-24 21:01 -0500
Message-ID<slrnitooo7.6hm.hellsop@nibelheim.ninehells.com>
In reply to#1832
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

[toc] | [prev] | [next] | [standalone]


#1837

FromHelmut Chang <usenet@helmutchang.at>
Date2011-05-25 08:05 +0200
Message-ID<iri67v$g1j$1@dont-email.me>
In reply to#1835
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

[toc] | [prev] | [next] | [standalone]


#1838

FromHelmut Chang <usenet@helmutchang.at>
Date2011-05-25 08:19 +0200
Message-ID<iri72o$k1o$1@dont-email.me>
In reply to#1832
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

[toc] | [prev] | [next] | [standalone]


#1841

FromDenis McMahon <denis.m.f.mcmahon@gmail.com>
Date2011-05-25 15:07 +0000
Message-ID<4ddd1b37$0$8752$bed64819@gradwell.net>
In reply to#1832
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web