Re: navigation include not functioning (RESOLVED)

Re: navigation include not functioning (RESOLVED)

am 05.08.2009 23:11:27 von Allen McCabe

--00163646c2e242bab104706b72df
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

I just wanted to let know I figured out my last issue (last as in, for now).

In my navigation.php include file, I had if ($page = about) echo href....
I changed it to if ($page == about) echo.... and it suddenly worked! Imagine
that...

Thanks all for you help, you are celebrities in my book now.

On Wed, Aug 5, 2009 at 1:44 PM, Martin Scotta wrote:

> You are using a value-filled array as a key-filled. Try this snippet
> and look the results...
>
> $pages = array('about' , 'services', 'portfolio', 'contact');
> $page = array_key_exists( 'page', $_GET ) ? $_GET[ 'page' ] : 'about';
> /*if*/ false === array_search( $page, $pages, true ) && (
> $page = 'about'
> );
>
> # note the sintax used to avoid if-statement
> # this has the same behaviour, but with less performance
> $pages = array('about' , 'services', 'portfolio', 'contact');
> $page = array_key_exists( 'page', $_GET ) ? $_GET[ 'page' ] : 'about';
> if( false === array_search( $page, $pages, true ))
> {
> $page = 'about';
> }
>
>
>
> On Wed, Aug 5, 2009 at 5:36 PM, Allen McCabe wrote:
> > Okay, I see how href="?page=contact" would work, and I do indeed have
> only
> > one file (which loads includes), but it still is not working. Clicking a
> > link, be href=?page=contact, href=?page=services, whatever, it still
> loads
> > the page with the ?page=whatever attached to the URL, but it is not
> > subsituting the $page variable within the include snippets, and it just
> > loads the 'about' versions of all includes
> (/phpincludes/about_content.php
> > as opposed to /phpincludes/services_content.php or whichever).
> >
> > This is really stumping me and try as I might I cannot see why it will
> not
> > work. Here is some of my code as it is currently:
> >
> > On default.php:
> >
> > [code=default.php]
> >
> >
> >
> > > >
> > $pages = array(
> > // list of includes:
> > 'about' , 'services', 'portfolio', 'contact'
> > );
> > $page = isset($_GET['page']) && isset($pages[$_GET['page']]) ?
> > $_GET['page'] : 'about';
> > // about is default page here
> >
> > ?>
> > <br /> > ><br /> > > [/code]<br /> > ><br /> > > then in the body tags<br /> > ><br /> > > [code=default.php]<br /> > ><br /> > > <td><?php include('phpincludes/' . $page . '_centerbar.php'); ?></td><br /> > > </tr><br /> > > <?php include('phpincludes/nav2.php'); ?><br /> > > <tr><br /> > ><br /> > > [/code]<br /> > > [code=nav2.php]<br /> > ><br /> > > <br /> > ><br /> > > [/code]<br /> > ><br /> > > It is surprisingly little code and I am starting to wonder if any php<br /> > > settings on my server are inhibiting this. What do you think?<br /> > ><br /> > ><br /> > ><br /> > > 2009/8/5 ollisso <ollisso@fromru.com><br /> > ><br /> > >> On Wed, 05 Aug 2009 22:08:30 +0300, Allen McCabe <allenmccabe@gmail.com<br /> > ><br /> > >> wrote:<br /> > >><br /> > >> You can do something like that:<br /> > >><br /> > >> links:<br /> > >> <br /> > >><br /> > >> This will work if you have only one file, all the time and it is default<br /> > >> one for current folder. (normally that is index.php, might be<br /> > default.php in<br /> > >> your case)<br /> > >><br /> > >> Second option is to use more harder approach:<br /> > >><br /> > >> $pos =<br /> > >><br /> > min(strpos($_SERVER['QUERY_STRING'],'&'),strpos($_SERVER['QU ERY_STRING'],'='));<br /> > >> $act = ($pos!==false) ? substr($_SERVER['QUERY_STRING'], 0, $pos) :<br /> > >> $_SERVER['QUERY_STRING'];<br /> > >> $page = strtolower($act);<br /> > >><br /> > >> then you can use links like:<br /> > >> href='?contact'<br /> > >> or if you need:<br /> > >> href='?contact=1' (in case of GET forms)<br /> > >><br /> > >><br /> > >> Third option is to use mod_rewrite, but this is slightly harder :)<br /> > >><br /> > >> But then you will be able to use links like:<br /> > >> www.domain.com/contact/<br /> > >> (which will work like: index.php?page=contact internally)<br /> > >><br /> > >> About checking what is included:<br /> > >> Imagine following scenario:<br /> > >><br /> > >> $page = isset($_GET['page']) ? $_GET['page'] : 'about';<br /> > >><br /> > >> include 'modules/'.$page.'.php';<br /> > >><br /> > >> Problem here is that you can include ANY file.<br /> > >> For example:<br /> > >> ?page=../index<br /> > >> will work as:<br /> > >> include 'modules/../index.php';<br /> > >><br /> > >> Which is crearly not what is intended.<br /> > >><br /> > >> There is also much more dangerous scenarios of this.<br /> > >><br /> > >> I hope this explains something :)<br /> > >><br /> > >><br /> > >><br /> > >> Excellent, your snippet is working nicely. Thank you!<br /> > >>><br /> > >>> Unfortunately, when I click a link ( <a href="<br /> > >>> http://uplinkdesign.hostzi.com/default.php?page=contact"> ),<br /> > >>> deafult.php?contact shows in the browser, but the default (about)<br /> > content<br /> > >>> is<br /> > >>> loading.<br /> > >>><br /> > >>> Also, I don't know what you mean by checking what is included.<br /> > >>><br /> > >>> 2009/8/5 ollisso <ollisso@fromru.com><br /> > >>><br /> > >>> On Wed, 05 Aug 2009 20:19:00 +0300, Allen McCabe <<br /> > allenmccabe@gmail.com><br /> > >>>> wrote:<br /> > >>>><br /> > >>>> Sure.<br /> > >>>><br /> > >>>>><br /> > >>>>> When I load my site, default.php loads ( displaying:<br /> > >>>>> http://uplinkdesign.hostzi.com/ in the browser as expected).<br /> > $thisPage<br /> > >>>>> is<br /> > >>>>> set to "about" via:<br /> > >>>>><br /> > >>>>> <?php<br /> > >>>>> if (!isset($thisPage)) {<br /> > >>>>> $thisPage="about";<br /> > >>>>> } else {<br /> > >>>>> $thisPage = addslashes($_GET['page']);<br /> > >>>>> }<br /> > >>>>> ?><br /> > >>>>><br /> > >>>>> in the <head> tags.<br /> > >>>>><br /> > >>>>><br /> > >>>>> I am seeing this:<br /> > >>>>><br /> > >>>>> The first 2 includes work just fine, loading to proper middle section<br /> > >>>>> and<br /> > >>>>> proper right-hand-side page content, the navigation include also<br /> > loads,<br /> > >>>>> but<br /> > >>>>> all the of "tabs" (background images) are the currentpage image, as<br /> > >>>>> opposed<br /> > >>>>> to not, as they should be (the the exception of the About Us<br /> > background<br /> > >>>>> image).<br /> > >>>>><br /> > >>>>> It seems that $thisPage is equal to all four values, so all the if<br /> > >>>>> statements within navigation tell PHP to load the current page image<br /> > for<br /> > >>>>> all<br /> > >>>>> 4 links.<br /> > >>>>><br /> > >>>>> Does this help?<br /> > >>>>><br /> > >>>>><br /> > >>>> Looks like you need something like that:<br /> > >>>> $pages = array(<br /> > >>>> // list of modules you have, for example:<br /> > >>>> 'about' , 'help', etc<br /> > >>>> );<br /> > >>>> $page = isset($_GET['page']) && isset($pages[$_GET['page']]) ?<br /> > >>>> $_GET['page'] : 'about';<br /> > >>>> // about is default page here<br /> > >>>><br /> > >>>> then just:<br /> > >>>> include 'modules/'.$page.'.php';<br /> > >>>><br /> > >>>> Always remember that you have to check what is included.<br /> > >>>> Best approach(if possible) to have a predefined list of all modules<br /> > which<br /> > >>>> can be included.<br /> > >>>><br /> > >>>> Else, there is some nasty things like:<br /> > >>>> ?page=../index.php<br /> > >>>> (infinity recurssion)<br /> > >>>><br /> > >>>> ?page=http://otherhost.com/hacker.<br /> > >>>> (inclusion of malicious script)<br /> > >>>> and so on.<br /> > >>>><br /> > >>>><br /> > >>>><br /> > >>>><br /> > >>>> On Wed, Aug 5, 2009 at 10:10 AM, Jerry Wilborn <<br /> > jerrywilborn@gmail.com<br /> > >>>><br /> > >>>>> >wrote:<br /> > >>>>><br /> > >>>>> Look<br /> > >>>>><br /> > >>>><br /> > >>>><br /> > >>>> I'm having trouble understanding your description of the problem.<br /> > Can<br /> > >>>><br /> > >>>>> you<br /> > >>>>>> tell us what you're seeing and what you expect to see?<br /> > >>>>>> Jerry Wilborn<br /> > >>>>>> jerrywilborn@gmail.com<br /> > >>>>>><br /> > >>>>>><br /> > >>>>>><br /> > >>>>>> On Wed, Aug 5, 2009 at 12:00 PM, Allen McCabe <<br /> > allenmccabe@gmail.com<br /> > >>>>>> >wrote:<br /> > >>>>>><br /> > >>>>>> I am trying to generate pages by importing content in includes, and<br /> > >>>>>> using<br /> > >>>>>><br /> > >>>>>>> my<br /> > >>>>>>> navigation include to tell PHP to replace a $thisPage variable<br /> > which<br /> > >>>>>>> all<br /> > >>>>>>> the<br /> > >>>>>>> includes use <?php include('phpincludes/' . $thisPage . '.php') ?><br /> > >>>>>>><br /> > >>>>>>> The idea behind it (I know tons of people do it, but I'm new to<br /> > this<br /> > >>>>>>> concept), is to have a 'layout' page where only a variable changes<br /> > >>>>>>> using<br /> > >>>>>>> $_GET on an href (index.php?page=services or index.php?page=about)<br /> > to<br /> > >>>>>>> load<br /> > >>>>>>> the new 'pages'.<br /> > >>>>>>><br /> > >>>>>>> PROBLEM:<br /> > >>>>>>> All my links are displaying the current page state, and links are<br /> > not<br /> > >>>>>>> building around the link text (hrefs are built conditionally with<br /> > if<br /> > >>>>>>> $thisPage != services then build the link, otherwise leave it as<br /> > >>>>>>> normal<br /> > >>>>>>> text). Same thing with the background image behind the link text<br /> > (to<br /> > >>>>>>> indicate a page's current position). If the condition is not true,<br /> > all<br /> > >>>>>>> the<br /> > >>>>>>> links (except the true current 'page') are supposed reload<br /> > index.php<br /> > >>>>>>> and<br /> > >>>>>>> pass a variable to itself to place into $thisPage using<br /> > >>>>>>> href="index.php?page=" (after which I have a variable which stores<br /> > >>>>>>> "about"<br /> > >>>>>>> or "services" within the if statement near the link text.<br /> > >>>>>>><br /> > >>>>>>> If this sounds like something you are familiar with (former issues<br /> > and<br /> > >>>>>>> whatnot) please let me know what I'm doing wrong. I would be happy<br /> > to<br /> > >>>>>>> give<br /> > >>>>>>> you any code you want to look at (index.php or navigation.php,<br /> > >>>>>>> whatever).<br /> > >>>>>>><br /> > >>>>>>> Thanks again for your help PHP gurus!<br /> > >>>>>>><br /> > >>>>>>><br /> > >>>>>>><br /> > >>>>>><br /> > >>>>>><br /> > >>>>><br /> > >>>>><br /> > >>>><br /> > >>>><br /> > >>><br /> > >>><br /> > >><br /> > >><br /> > ><br /> ><br /> ><br /> ><br /> > --<br /> > Martin Scotta<br /> ><br /> <br /> --00163646c2e242bab104706b72df--</p> </article> <article> <h2>Re: navigation include not functioning (RESOLVED)</h2><span>am 05.08.2009 23:55:07 von Ben Dunlap</span> <p>> In my navigation.php include file, I had if ($page = about) echo href....<br /> > I changed it to if ($page == about) echo.... and it suddenly worked! Imagine<br /> > that...<br /> <br /> Another good case for putting the variable on the right side of "==":<br /> <br /> if ("about" == $page)<br /> <br /> Then if you mis-type "==" as "=", PHP will fail immediately with a parse error.<br /> <br /> It feels a little weird but if it saves a lot of head-desk moments it's<br /> probably worth it. Now if only I could get into the habit myself...<br /> <br /> Ben<br /> <br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <article> <h2>Re: navigation include not functioning (RESOLVED)</h2><span>am 06.08.2009 00:04:38 von Martin Scotta</span> <p>Yes, that's a golden rule for a bug free code.<br /> The rule shoudl be something like this...<br /> <br /> Whenever you have a constant value evaluated to a non-constant value<br /> put them in the left side of the expression.<br /> <br /> if( 'constant' == $non_constant )<br /> echo ' variables has that name for anything ';<br /> <br /> if( false !== ( $pos =3D strpos( 'php', 'I love php sintax' )))<br /> echo ' we all love it ';<br /> <br /> Also note that is easy to count the closing parenthesis<br /> (if your editor does not helps try with SciTE)<br /> <br /> On Wed, Aug 5, 2009 at 6:55 PM, Ben Dunlap<bdunlap@agentintellect.com> wrot=<br /> e:<br /> >> In my navigation.php include file, I had if ($page =3D about) echo href.=<br /> ....<br /> >> I changed it to if ($page == about) echo.... and it suddenly worked!=<br /> Imagine<br /> >> that...<br /> ><br /> > Another good case for putting the variable on the right side of "==":<br /> ><br /> > =A0 if ("about" == $page)<br /> ><br /> > Then if you mis-type "==" as "=3D", PHP will fail immediately with a =<br /> parse error.<br /> ><br /> > It feels a little weird but if it saves a lot of head-desk moments it's<br /> > probably worth it. Now if only I could get into the habit myself...<br /> ><br /> > Ben<br /> ><br /> ><br /> > --<br /> > PHP General Mailing List (http://www.php.net/)<br /> > To unsubscribe, visit: http://www.php.net/unsub.php<br /> ><br /> ><br /> <br /> <br /> <br /> --=20<br /> Martin Scotta<br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <article> <h2>Re: navigation include not functioning (RESOLVED)</h2><span>am 06.08.2009 04:14:01 von Paul M Foster</span> <p>On Wed, Aug 05, 2009 at 02:55:07PM -0700, Ben Dunlap wrote:<br /> <br /> > > In my navigation.php include file, I had if ($page = about) echo href....<br /> > > I changed it to if ($page == about) echo.... and it suddenly<br /> > worked! Imagine<br /> > > that...<br /> > <br /> > Another good case for putting the variable on the right side of "==":<br /> > <br /> > if ("about" == $page)<br /> > <br /> > Then if you mis-type "==" as "=", PHP will fail immediately with a parse<br /> > error.<br /> > <br /> > It feels a little weird but if it saves a lot of head-desk moments it's<br /> > probably worth it. Now if only I could get into the habit myself...<br /> <br /> This is common practice for a lot of C programmers for exactly this<br /> reason.<br /> <br /> Paul<br /> <br /> -- <br /> Paul M. Foster<br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <footer> <a href="/">Index</a> | <a href="/impressum.php">Impressum</a> | <a href="/datenschutz.php">Datenschutz</a> | <a href="https://www.xodox.de/">XODOX</a> </footer> </main> </body> </html>