Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dal123

macrumors 6502a
Original poster
Oct 23, 2008
903
0
England
I'm trying to learn php and don't fully understand the basic functions, elements etc. :eek:.
It would be nice to see existing stuff on site, e.g. I'd like to learn how to display in my nav bar what page I'm on by turning the link in my nav bar a different colour or maybe display an image. I know a website that does this and like to see how the effect is achieved.
I have firebug and web developer toolbar in firefox, could I use these? Also how do you validate scripts with the developer toolbar?
:)
 
Unless there is a HUGE lapse in security on the server, you won't be able to see the PHP code itself. (unless you ask the owner to share it with you).

PHP is run on the server and the output (generally HTML, CSS or JS) is sent to the browser for parsing.

There are a fairly decent number of PHP tutorials out there. However, if you are struggling with the general logic and programming aspects, try and wrap your head around variables and if/else statements and then start drawing out some diagrams (some people have also been able to learn the general concepts using something called "pseudo code")

HTH!

~ Jeremy
 
You won't be able to see. I would recommend using a program like Coda where you can edit the files remotely on the server and when you save the file, you just refresh the webpage and see what occurred.
 
Also how do you validate scripts with the developer toolbar?

The other posters answered well on the main question and I agree with them, so I will just address this piece.

You didn't mention what kind of scripts you want to validate. For JavaScript, the only validator I am aware of is JSLint, which is really more about making sure you use well-formed code, but is nice. I also use the Error Console in the browser to view any errors that come up during execution, which should show you any bad code.

For PHP, I don't know too much in terms of validation. NetBeans will give you live warnings and errors though in your code, which can be handy. It's just a bit overweight though for casual use. Worth checking out though. For PJP I just keep the PHP error log file handy as I am trying out code in MAMP.
 
Check out the Wordpress code. It's all PHP and if you install it on your local machine using MAMP, you can play with it to your heart's content without ever making anything public. Plus, there are tons of tutorials about WP that might help you learn the fundamentals of PHP.
 
Check out the Wordpress code. It's all PHP and if you install it on your local machine using MAMP, you can play with it to your heart's content without ever making anything public. Plus, there are tons of tutorials about WP that might help you learn the fundamentals of PHP.

You can install MAMP without wordpress and it will allow you to view/edit PHP pages locally. You just have to setup the MAMP software and create a development directory. There is a great howto video if you do a search for installing MAMP on a Mac. Crap... I'll just go find it and post the link below.

http://www.lullabot.com/videos/install-local-web-server-mac-osx
 
Thanks for all the replies :D. I have installed mamp and it's pretty cool, though php is very hard and those who are fluent must be pure genius :cool:.
I have watched countless tutorials, and have put in about 70 hours this week (honestly not kidding :eek:) and I don't understand the different types and get them confused. I have noted down the main types in my notebook and keep reading through a mammoth php developing book :(.
Someone on another forum has really, really helped me out and helped me with this cool script that opens my folder and displays my pics out for me:
PHP:
<?php
// settings
$base = "data/retaining-walls";
$thumbs = "thumbs";
$files = scandir($base);
unset($files[0], $files[1]);
unset($files[array_search('.DS_sStore', $files)]); 
foreach ($files as $file)
{
echo "<a href='$base/$file' rel='lightbox'>		<img class='thumbs' src='$base/$thumbs/$file'>";
}
?>
I have just come across a new problem where, I have about 30pics on one page and after about 10 or so I'd like to enter some more content header and paragraph and maybe a movie later. What would be the best command to execute this? I'm guessing an if <10 or something like that but it looks like this is a multidimensional array and not sure what to do :eek:.
Also when I display my thumbnail pics I'm displaying a directory folder 'thumbs' and I don't want that as it's messing up my display a little. My friend managed to cure the .DS_Store ones for me but not sure how to add another condition. I tried many different logical ways , && ('.DS_Store'); but whole page disapeers :(. What would I add to this to create another condition?
PHP:
unset($files[array_search('.DS_sStore', $files)]);
 
You can install MAMP without wordpress and it will allow you to view/edit PHP pages locally.

True, but if you don't know how to build PHP sites, looking at the Wordpress code and then looking at the HTML it generates in your browser can really help you understand how it works.
 
and keep reading through a mammoth php developing book

Perhaps you should try a different, perhaps simpler, book? It really isn't that difficult to pick up the basics of PHP. I wouldn't dream of learning it from online tutorials only, but with a decent book it shouldn't be too hard to pick up the basics.
 
You would''ve thought I'd be able to perform my posted script after 70 hours though!:(
 
You would''ve thought I'd be able to perform my posted script after 70 hours though!:(

Not if you're learning the wrong way. I'm a designer by training but I've picked up a bunch of different web programming languages as I've needed them. In the beginning I knew what I wanted to do but didn't know how to get there. I didn't even know what terms to google. It can be especially infuriating trying to learn something new if you start too advanced. That's often the case with some of the web tutorials -- they assume you know more than you do. If you don't know what an array is exactly there's no real point to learning an array function. I've found good books much easier to learn from.

In your case, do you understand what each line of your script is doing exactly?
 
I understand that I'm assigning variables as folder directories.
PHP:
$files = scandir($base);
Looks like function, or arry :eek:, basically we're saying that the variable files is the the directory variable base being scanned and read.
Choosing not to display files with ... for security reasons, and also .DS_Store as we don't want to display it. (though I don't understand how to add another condition, tried adding comma, brackets && etc but just don't know how to add more conditions.
Then for each file that is same name as each other we display them on the screen.
I think I've got a little gist of it, though not sure about writing the code, comma's etc.:eek:
 
Code:
unset($files[array_search('.DS_sStore', $files)]);

I thought I should point out ."DS_sStore" is spelled wrong.

And the code you've shown so far isn't using conditions to remove things, which is likely why your attempts keep failing.

Here's the doc on the "unset" function.

http://php.net/unset

There a a couple types of conditional statements, for instance theres the if statement which would look like

Code:
if(/*add a condition here like 1+1 = 2, etc..*/){
        //do something
}

Theres also the switch statement:
Code:
switch(/*variable you're trying to test against. lets say its $name*/){
          case($name == "Jen"):
            //do something when $name = Jen
          break;
}

With the switch statement, you can continue to add cases until you cover everything you might want to test against.

Theres also the while loop too.

Hope that helps a bit!
 
You're right, scandir is a function. It outputs an array with all the files in the folder it's pointed to. The unset functions remove values from that array. The foreach then walks through the array and outputs the html to display an image for each entry.

What other conditions are you trying to add?
 
I have just come across a new problem where, I have about 30pics on one page and after about 10 or so I'd like to enter some more content header and paragraph and maybe a movie later. What would be the best command to execute this?

Create an iterator variable that will count along with the loop. Then using the iterator plus the modulus operator you can catch every tenth iteration.

PHP:
$i = 0;
foreach ($files as $file)
{
	// For every tenth iteration, do something special
	if ($i++ % 10 == 0)
	{
		// Do whatever you need
	}
	echo "<a href='$base/$file' rel='lightbox'>        <img class='thumbs' src='$base/$thumbs/$file'>";
}

As an alternative to using the scandir and removing unwanted bits, try out the glob function.
 
Here is live: http://www.preciseformwork.co.uk/new/radius%20formwork.php
Thanks for all your input people. Angelwatt, that's pretty impressive - what I would like to do is enter custom certain different bits of information at different times. What I'd like is to perform different actions each time e.g. a certain paragraph about something, then a different paragraph about something else. Possibly even insert a movie and another paragraph.
At the moment every 10th item is performing the same action, it also performs it at the start, displaying the paragraph before my loop begins.
I also have another problem - I need to create a condition that will not display a directory, or only to display jpegs. As one of the directorys is being displayed and it makes it look like there are only 9 in that row and makes it look messy.
This is clever stuff, well beyond my capabilites :eek::eek:. So interesting though, can see how I'm becoming addicted :).
 

Attachments

  • Picture 1.png
    Picture 1.png
    589 KB · Views: 115
PHP:
$i = 0;
foreach ($files as $file)
{
	// For every tenth iteration, do something special
	if ($i++ % 10 == 0)
	{
		// Do whatever you need
	}
	echo "<a href='$base/$file' rel='lightbox'>        <img class='thumbs' src='$base/$thumbs/$file'>";
}

Here is my modification of angelwatt's script...

PHP:
$i = 0;
foreach ($files as $file)
{
	// For every tenth iteration, do something special - skip the first one
	if ($i++ % 10 == 0 && $i!=0)
	{
		if($i==10)
                {
                       // do what you want for the first one
                }
		elseif($i==20)
                {
                       // do what you want for the second one
                }
		else
                {
                       // a "default" action if none of the above match
                }


	}
	echo "<a href='$base/$file' rel='lightbox'>        <img class='thumbs' src='$base/$thumbs/$file'>";
}
 
Thanks for all the help with this chaps.
I tried your modification on angelwatt's genius, I can see the theory behind it. I must be putting something in wrong, as I can't display my paragraph and break. This is exactly how I have it :confused:. Sorry to be a nuisance.
PHP:
<?php
 
// settings
$base = "data/retaining-walls";
$thumbs = "thumbs";
 
$files = scandir($base);
unset($files[0], $files[1]);
unset($files[array_search('.DS_sStore', $files)]);
$i = 0;
foreach ($files as $file)
{
    // For every tenth iteration, do something special - skip the first one
    if ($i++ % 10 == 0 && $i!=0)
    {
        if($i==10)
                {
echo "</p><p> This is the photo's of the way it was done.</p>";
                }
        elseif($i==20)
                {
                       // do what you want for the second one
                }
        else
                {
                       // a "default" action if none of the above match
                }


    }
    echo "</br><a href='$base/$file' rel='lightbox'>        <img class='thumbs' src='$base/$thumbs/$file'>";
} 
  
?>
 
No I don't think that's it as the paragraph is not being displayed either. I have tried correcting the break and it does not make much difference.
Thanks for suggestion though ;).
 
No I don't think that's it as the paragraph is not being displayed either. I have tried correcting the break and it does not make much difference.
Thanks for suggestion though ;).

The problem is in designguy79's addition to the code.

PHP:
$i++ % 10 == 0 && $i!=0

In this code the $i++ is a post increment. That means that the $i % 10 code is evaluated and then the $i is incremented. For example, if $i is 10 it evaluates 10 % 10 which returns a 0 and then $i is increased by one to 11.

The problem is that designguy79's code is checking if $i is 10. The solution is to adjust the if statement to check when $i is 11.

PHP:
    // For every tenth iteration, do something special - skip the first one 
    if ($i++ % 10 == 0 && $i!=0) 
    { 
        if($i==11) 
                { 
echo "<p> This is the photo's of the way it was done.</p>"; 
                } 
        elseif($i==21) 
                { 
                       // do what you want for the second one 
                } 
        else 
                { 
                       // a "default" action if none of the above match 
                } 


    }

Alternatively you could change the initial if clause to a pre increment:

PHP:
++$i % 10 == 0 && $i!=0

The problem with doing that is that the paragraph will then show up after the 9th image. The solution is to set $i = -1 instead of 0 initially.

You also need to change .DS_sStore to .DS_Store .

A good way to solve problems like this is to print out all the variable to see what they're doing. Instead of outputting the images have it output the $i value, the value of $i % 10, and the file name. That way you can really see what's happening.

EDIT: And just to show you that there's often more than one way to skin a cat here's the way I would have done it:

PHP:
<?php 
  
// settings 
$base = "data/retaining-walls";
$thumbs = "thumbs"; 
  
$files = scandir($base); 
unset($files[0], $files[1]); 
unset($files[array_search('.DS_Store', $files)]); 

$chunk = array_chunk($files, 10);


// Define Paragraphs
$paragraph[1] = "<p>First Paragraph</p>";
$paragraph[2] = "<p>Second Paragraph</p>";


foreach($chunk as $i => $filearray)
{
	if ($paragraph[$i])
		echo "$paragraph[$i]";
	else if ($i > 0)
		echo "<p>Default Paragraph</p>";
	
	foreach ($filearray as $file)
	{
		echo "<a href='$base/$file' rel='lightbox'><img class='thumbs' src='$base/$thumbs/$file'><br/>";
	}
	
}

?>
 
Thanks panoz, that inserts the paragraphs where I want them. Unfortunately the thumbnails are now in a vertical list. How do I get them to display inline?:)
 
Thanks panoz, that inserts the paragraphs where I want them. Unfortunately the thumbnails are now in a vertical list. How do I get them to display inline?:)

Use CSS!

PHP:
.thumbs{display: inline;}

This will cause the images to appear in rows as wide as the container they are in, in this case the paragraph which may actually be as wide as the browser window.

If you want to limit this to say four images at most on a row then you will need another condition to output the closing tag and open another paragraph.
 
Thanks for that suggestion Dunmail, I've tried it out in my css file and it does not work.
The php code must be creating something, as they were flowing inline before so gotta be something with the php :confused:.
 
They're showing up vertically because you're inserting a br tag with the linked image, which will make them go to the next line. Images are already inline elements so using CSS to make them inline is unnecessary.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.