
09-24-04, 10:16 AM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Understanding Arrays
Ok, this is my first array attempt at doing something I can functionally use. I want to set the valid extention types of a file. This is what I have so far, but it doesn't work. I can't be too far off here, or I'm trying to use to wrong statement to accomplish the job.
This is just a quick short version of the whole script just to see if I can get this working. There is a ./content/home.html file. All I want is for it to determine which extention in the array the file is and set that extention to the $ext variable.
Shoot, this won't work at all. Even as I'm looking at it, I think it's just looping over and over again, this produces a blank page. UGH.
Any ideas?
|

09-24-04, 10:38 AM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm going through the book trying to figure out a way I can get this to work. I think I may need to use foreach instead of for, but I still have the problem of how to get it to stop the loop when a match to the file is found. grrrr. Books are great, but I find they don't give allot of practicle examples. Everything is echo'ing multiple output, when I only want one output when it's found. grrr.
|

09-24-04, 11:14 AM
|
|
Wannabe Coder
|
|
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Last edited by mikaelf; 09-24-04 at 11:18 AM.
Reason: reason for editing
|

09-24-04, 11:39 AM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks mikaelf, that works. I'd like to understand it a bit more, so have a few questions.
1. how do you determine when it's best to use single quote over double quote?
2. The final include ends up being $file . $ext, so in this what is the difference between $file . $extension[$i] and $file. '.' .$extension[$i]
3. Does "break" stop the loop when a match is found? Something else that the book is not exactly clear on to me.
4. The book said sizeof was an alias of count, does it matter which one you use? I used sizeof because the wording makes more since to me. Do it to the full size of the array.
Thanks,
RJ
|

09-24-04, 11:54 AM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
Originally Posted by rjwebgraphix
thanks mikaelf, that works. I'd like to understand it a bit more, so have a few questions.
2. The final include ends up being $file . $ext, so in this what is the difference between $file . $extension[$i] and $file. '.' .$extension[$i]
Thanks,
RJ
|
Well, I take that back, without the array I was able to use $file . $ext, but with the array I now have to use $file. '.' .$ext. Strange, but it works, so I'm not complaining. I'd just like to understand how it works not just that it works.
|

09-24-04, 11:54 AM
|
|
Wannabe Coder
|
|
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
1.single quote -> the string enclosed within won't be parsed thus increasing whole script parsing speed
2.your $file is './content/home' and its array of extensions doesn't have a dot as delimiter between file name and file extension. so, writing $file.$extension[$i] will end up with, for example ./content/homehtml (here, the dot (.) is used to concatenate string variables) while using $file.'.'.$extension[$i] will yield ./content/home.html for the same value of $i.
3.Yeah, it will stop the loop. If no match is found you have to set default condition (an error page)
4.It's based on your own preference.
Enjoy the learning!!
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
|

09-24-04, 12:28 PM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
Originally Posted by mikaelf
3.Yeah, it will stop the loop. If no match is found you have to set default condition (an error page)
Enjoy the learning!!
|
Enjoy the learning, yes of course. I've always shy'd away from the programming aspect of things, but I'm now seeing the benefit and am forced to do it.
You just reminded me there, almost forgot to bring that function back into the script. It should actually read.
Where gohome is just a function that sends them to the url specified in the $whereto variable.
|

09-24-04, 02:29 PM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I just can't seem to leave it alone. I guess this is all a part of the learning process.
Ok, I've decided to take this a little further. The original intent was to be used in a single template file. After all the work gone into this.... (I know for some people this would have been a 10 minute task. For me it's been an all week task.) ... I've decided it would be nice to have the ability to have multiple template files. It will default to one if the template is not specified in the url. I'm quite sure I have all of it done except for one minor portion that I can't seem to get right.
I've tried this two ways and both ways fail. in my tests I'm using one html and one php file. I think I can do it with two separate for statements, but trying to do it with a single one. Is this possible?
First way tried...
Second way tried...
I didn't echo the results on the first one, but the second one returned html for both $t_ext and $c_ext when the template extention is PHP and the content extention is html. UGH.
Do I need to use two separate for statements for the same task?
|

09-24-04, 02:40 PM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried this also to no evail. and it returns html for the content exention, but is not returning anything on the template extension. grrrrrrrr
|

09-27-04, 02:14 AM
|
|
Newbie Coder
|
|
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, I figured it out all on my own. I'm not sure if this is the best way, but it works. Can someone tell me if there is a better or easier way to do it?
Again, all I'm trying to do is to determine what is the extension of the two different files that do exist.
Also, this works fine for two files, but what about 5, or 10, or worse yet 100 files. All this nomatch, matchone, matchboth would quickly become finallymatchall100 if that was what I was going for. That's why I'm trying to see if there is an easier way to do this. Not to mention to help me learn a bit more.
Again, this works for my purposes, I'd just like to see if there's an easier way to accomplish the same job. Hmmm. If you did have more, set something like this up in a function, and call the function 100 times?
Code as follows:
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|