Jump to content

Recommended Posts

Hi,

 

First of all, thanks for creating this site, lots of wonderful information here :)

 

Im totally new to PHP, and im trying to implement a little php script in a new website im building.

 

Basicly i have a table:

 

<div class="subnav">

<ul>

<tr><a href="page1.php">This is page 1</a></tr>

<tr><a href="page2.php">This is page 2</a></tr>

<tr><a href="page3.php">This is page 3</a></tr>

<tr><a href="page4.php">This is page 4</a></tr>

</ul>

</div>

 

 

When i click on a certain page, say Page 1, i wan't Page 1 to be underline and the rest of the links not.

 

Im really lost as how i would code this in PHP, but i figure it would go something like this:

 

 

If site = page1.php [

 

use class="boldnav" on <tr><a href="page1.php">This is page 1</a></tr>

 

else

 

use class="normal nav" on <tr><a href="page1.php">This is page 1</a></tr>

]

 

 

This obviously dosen't work, i need a "little" help here.

Edited by Togo
Link to comment
https://forums.phpfreaks.com/topic/274219-help-a-newbie-with-a-php-script/
Share on other sites

Hi Togo

 

What follows is a very simplistic demonstration to get you kick started.

 

 

You need to be able to grab the url. So if your site is www.mysite.com/page1.php you can do something along the lines of the following:

 

$pageLink =  $_SERVER['PHP_SELF'];

 

This will populate a variable with whatever follows the base domain. So in the example of www.mysite.com/page1.php $pageLink will now be equal to "page1.php"

 

You can then perform your if statement to find if you actually are on page1

 

 


<a href="page1.php" id="navLink" <?php if($pageLink == 'page1.php'){ echo 'class="UNDERLINE"'} ?> >page1</a>

 

 

In reality you wouldn't really get by with something quite as simplistic as the above because the url structure would be more complicated but it should get you thinking in the right direction.

 

 

Hi,

 

First of all, thanks for creating this site, lots of wonderful information here :)

 

Im totally new to PHP, and im trying to implement a little php script in a new website im building.

 

Basicly i have a table:

 

 

 

 

When i click on a certain page, say Page 1, i wan't Page 1 to be underline and the rest of the links not.

 

Im really lost as how i would code this in PHP, but i figure it would go something like this:

 

 

 

 

 

This obviously dosen't work, i need a "little" help here.

Thanks, i really appreciate the help.

I'm starting to see clear now, it's actually a funny nut to crack! :)

 

I think im really close, and with your help i altered the code like this.

 

<html>

<head>

 

<?php

$pageLink = $_SERVER['PHP_SELF'];

?>

 

</head>

<body>

<div>

<ul>

<tr><a href="page1.php">This is page 1</a></tr>

<tr><a href="page2.php">This is page 2</a></tr>

<tr><a href="page3.php">This is page 3</a></tr>

<tr><a href="page4.php">This is page 4</a></tr>

<tr><a href="page1.php" <?php if($pageLink == 'http://192.168.1.110/folder1/page5.php'){ echo 'class="boldnav"'}

else

{echo 'class="subnav"'}?> >page1</a></tr>

</ul>

</div>

 

</body>

</html>

 

If dosen't give me the wanted result, it gives me a blank page.

The PHP code in the header should store the exact url in the variable. And the statement says: If the url is exactly like this then use 'boldnav' class, else use 'boldnav' class.

 

In other words, if the statement was true the line would be like this:

<tr><a href="page1.php" class="boldnav">page1</a></tr>

 

and if false, it would be:

<tr><a href="page1.php" class="boldnav">page1</a></tr>

 

But me getting a blank page is evidence i wrote something wrong, i would assume i may have put the first part of the code in a wrong place, but it dosen't look like it makes a difference whereever i put it.

Edited by Togo

Use basename () on $_SERVER['PHP_SELF'] to get only the filename, should make the test a bit easier (especially if you change hosts/domains). ;)

 

To figure out what you did wrong, turn on error reporting in php.ini. A quick Google search will give you lots of resources on how to do it. ;)

Thanks Christian, ill try that as well!

 

I actually had error reporting enabled before. But it appears i have been too lazy to enable it again, so right now i am using phpcodechecker.com instead lol.

But it does look like i had a little progress. Im getting no errors now, but nothing is happening either.

 

Code now looks like this:

<html>

<head>

<?php

$PageLink = $_SERVER['PHP_SELF'];

?>

</head>

<body>

<div>

<ul>

<tr><a href="page1.php">This is page 1</a></tr>

<tr><a href="page2.php">This is page 2</a></tr>

<tr><a href="page3.php">This is page 3</a></tr>

<tr><a href="page4.php">This is page 4</a></tr>

<tr><a href="fysio.php" <?php if($PageLink == '/peter/fysio.php'){ echo 'class="subnav"';}

else {echo 'class="subnav"';}?> >page1</a></tr>

</ul>

</div>

</body>

</html>

 

Looks like i was missing 2 ";" after the echo's.

It wasen't actually your example i just posted, it was my results from the previous attempt.

And speaking of said example i tried to set

$PageLink = "monkey";

 

and

 

<tr><a href="fysio.php" <?php if($PageLink == 'monkey'){ echo 'class="subnav"';}

else {echo 'class="subnav"';}?> >page1</a></tr>

 

Still no result though :(

 

I also just now tried:

 

<?php

$PageLink = basename () on $_SERVER['PHP_SELF']

?>

It gives me an unexpected T_STRING in your code on line 6.

 

And now I just started to read the page you linked me.

 

Edit: Annd it might not even be the PHP code wrong anymore. I just tested what would happen if i simply made a row like this:

 

<tr><a href="page1.php" class="boldnav">Some text</a></tr>

 

Nothing happened, so it seems my search lies somewhere else now.

Edited by Togo

 

Basicly i have a table:

<div class="subnav">
<ul>
<tr><a href="page1.php">This is page 1</a></tr>
<tr><a href="page2.php">This is page 2</a></tr>
<tr><a href="page3.php">This is page 3</a></tr>
<tr><a href="page4.php">This is page 4</a></tr>
</ul>
</div>

 

No, you don't have a table. You're on the way to an unordered list but you haven't got that right either. Basically your HTML is a mess, neither one thing or another.

 

Your "if" condition appears to be

 

if (condition)
   do X
else
 do X

:confused:

Yes, and that is exactly what i want the script to do.

I want the link beeing clicked on underlined when the condition is met (beeing on the page the link refers too), and if not then don't underline it.

 

My html table may be a mess, but it looks fine to me. I did try building it the way i assume you wan't to, but it didn't give me a different result.

 

But this:

<tr><a href="page1.php" class="boldnav">Some text</a></tr>

Dosen't do a thing, and thats what im now trying to figure out why not.

Hi Mate

 

As christian pointed out basename is needed - sry my bad i was typing it out a bit fast earlier.

 

Your code to grab what is essentially the file name should look like this:

 

$PageLink = basename($_SERVER['PHP_SELF']);

 

Then the conditional for the link will look something like

 

<a href="fysio.php" <?php if($PageLink == 'fysio.php'){ echo 'class="ClassToApplyUnderline"';}
else {echo 'class="ClassThatDoesNotUseUnderline"';}?> >page1</a>

 

A good practice if you need to test a variable that doesn't appear to be matching is to echo it out.

 

So try doing

 

echo $PageLink;

 

This way you can see what basename(...) is giving you to work with and you can formulate your if statement from there.

 

Hope that helps!

 

 

[quote name=Togo' timestamp='1360361064' post='

1411153]

Yes, and that is exactly what i want the script to do.

I want the link beeing clicked on underlined when the condition is met (beeing on the page the link refers too), and if not then don't underline it.

 

My html table may be a mess, but it looks fine to me. I did try building it the way i assume you wan't to, but it didn't give me a different result.

 

But this:

 

Dosen't do a thing, and thats what im now trying to figure out why not.

Edited by Drongo_III

I do understand basic HTML, thanks. I realise it should probaly looks like this:

 

<TABLE>

<TR>

<TD><a href="page1.php>Page 1</a></TD>

<TD><a href="page1.php>Page 1</a></TD>

<TD><a href="page1.php>Page 1</a></TD>

<TD><a href="page1.php>Page 1</a></TD>

</TR>

</TABLE>

 

But thats the least of my problems, and it shoulden't really be a distraction from the PHP problem.

 

 

Hi Mate

 

As christian pointed out basename is needed - sry my bad i was typing it out a bit fast earlier.

 

Your code to grab what is essentially the file name should look like this:

 

$PageLink = basename($_SERVER['PHP_SELF']);

 

Then the conditional for the link will look something like

 

<a href="fysio.php" <?php if($PageLink == 'fysio.php'){ echo 'class="ClassToApplyUnderline"';}
else {echo 'class="ClassThatDoesNotUseUnderline"';}?> >page1</a>

 

A good practice if you need to test a variable that doesn't appear to be matching is to echo it out.

 

So try doing

 

echo $PageLink;

 

This way you can see what basename(...) is giving you to work with and you can formulate your if statement from there.

 

Hope that helps!

 

Thank's Drongo, a lot! I have now verified that the PHP code works exactly as it should! :) Now on to the next part of the troubleshooting :)

Edited by Togo

Togo, just thought I'd give you another example I threw together just for the heck of it.

 

<style>
a { color: blue; text-decoration: none;}

#underline { color: red; text-decoration: underline; };

</style>

<?php
$currentPage = $_GET['page'];
$pageNo = array(1,2);

foreach($pageNo as $page){
  if($page == $currentPage) {
 echo "If page is current page underline only the current page. Page:<a href=index.php?page=$page><div id='underline'>$page</div></a> ";
} else {
  echo "<a href=index.php?page=$page>$page</a> ";
}
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.