0

Newbie here

I am working on a small project which I use the following code show below on about 12 pages, and in time will likely use it in another twelve. To me, that seems like I ought to make it into a function and i've looked over at PHP.NET and read some cool stuff, and did some googles but nothing quite answered my question in a way I understood. I understand how to make a generic function (i.e. function EchoSomething(){echo 'print something';}. But I don't understand how to construct a function that would call an array and run it in a for each loop.

This is my starting code that, aside from using a different array name every time, is otherwise identical:

<?php

foreach($aaGenR1 AS $key => $value){

echo '<div class="3u"><a href="_img/fulls/' . $key .'" class="image full"><img src="_img/thumbs/' . $key .'" alt="" title="' . $value .'" /></a></div>';

}

?>

It seems to me that i ought to be able to write something like

function GetPortfolioArray(){

echo '<div class="3u"><a href="_img/fulls/' . $key .'" class="image full"><img src="_img/thumbs/' . $key .'" alt="" title="' . $value .'" /></a></div>';

}

And then when i would call the function in my code, i'd write 'GetPortfolioArray($arrayNameToBeUsed)'

I think i'm close, I hope i'm close anyway. Help is appreciated. thank you.

Cheers

3 Answers 3

1

First of all, create either a function.php file that you include, or something like Util.class.php. They will hold any of your further functions. I'd rather work with static functions in a file called XXXXXX.class.php. Mine is always the same in every projects and is called "Util.class.php". All my functions are there.

Add a require_once('Util.class.php'); in any file that will need your function.

To answer your question, i'd do a function such as:

/**
* A short description of your function
* @return void
/**
public static function getPortofolioArray($array)
{
    foreach ($array as $key => $val)
    {
        echo '<div class="3u"><a href="_img/fulls/'.$key.'" class="image full"><img src="_img/thumbs/'.$key.'" alt=""  title="'. $value.'" /></a></div>';
    }
}

Where ever you then need this function, first of all include Util at the top of your file:

require_once('Utils.class.php')

And call the function where you need it like:

Utils::getPortofolioArray($yourArray);

Of course that's a direct answer to the question, i did not improve you idea or anything.

Be carefull how you name your function. Basically you called it "getPortoFolioArray" but you don't do that in your function, you echo stuffs out and don't return any array. Rather call it something like: "printPortofolioLinks" as it's actually what it does...

Sign up to request clarification or add additional context in comments.

4 Comments

A functions class? I'm currently taking an intro to Csharp which I believe will cover classes, but we aren't there yet. I didn't know PHP could have classes. I did create a functions file which is where i placed my function that i am trying to call. I will have to experiment with that as that sounds like an amazing idea - kudos' to whoever came up with that and to you for sharing. And thanks for your comments - i see that I needed to include $array in my parens and in rewriting the code to functions i forgot to add the for loop (Duh!).
Question - why wouldn't i call it 'getStuff?' I'm getting the things i want aren't I? Why would we say echo or printWhatever - can you expand on this more please. And super thank yous - off to try all this info now and will report back with hopeful successes!
I attempted this an it almost worked as hoped. I got back my images and links, but after each one i also got an Undefined variable notice on line 36 in the functions file which is reads as: echo '<div class="3u"><a href="_img/fulls/' . $key .'" class="image full"><img src="_img/thumbs/' . $key .'" alt="" title="' . $value .'" /></a></div>'; Is it because i'm using the $key twice?
Figured it out - i need to change $val to $value. Thanks for the help!
1

You have to create the foreach inside the function like so :

function GetPortfolioArray($var){
  foreach($var as $key => $value){
  echo '<div class="3u"><a href="_img/fulls/' . $key .'" class="image full"><img src="_img/thumbs/' . $key .'" alt="" title="' . $value .'" /></a></div>';
  }
}

and then you can GetPortfolioArray($randomvar);

$randomvar being the array obviously

EDIT : Please considere I assume whatever is inside your echo is meant to be a row or a bloc that is obviously goign to repeat itself. If that's not what you want, then this loop is not the right way to go.

1 Comment

Thanks, yes it is a bloc, so i'll be calling the function a few times. When i get this down i'm going to try to build a foreach loop with a foreach loop inside it to handle multiple lines for longer arrays. I'm going in tiny baby steps cause i'm a tiny baby :D Thank you for your wisdom!
1

First i recommend some basic PHP tutorials or better some basic software development tutorials. This i something every beginner tutorial will answer.

Second what you want is a function which takes an array as an parameter, iterates over it and inserts key and value into a string template. To answer your question:

function printPortfolioArray($portfolioArray)
{
    foreach($portfolioArray as $key => $value)
    {
        echo '<div class="3u"><a href="_img/fulls/' . $key .'" class="image full"><img src="_img/thumbs/' . $key .'" alt="" title="' . $value .'" /></a></div>';
    }
}

It would be better to move the template into a second parameter and replace $key and $value with format variables as defined in sprintf() and printf()

function printArray($array, $template)
{
    foreach($array as $key => $value)
    {
        printf($template, $key, $value);
    }
}

Example:

$array = ...;
$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>';
printArray($array, $template);

3 Comments

Lot to chew on here - thank you. Will explore. Again thank you for this sagely advice. never earn head of a sprintf() or a printf(). We're not starting functions in my class for another two weeks - i'm experimenting to try to stay ahead so i won't slow my class down. Practice forward right? Again thank you!
WOW - this is huge - it really opened me to a lot of things. I wish i could vote for two answers - all the options here have been really eye opening to me. Thank you so much!
I totally recommend to do something by your self. Especially that "another two weeks" to learn functions is insane. This case here is the best example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.