Reputation: 141110
if ( $_GET['tab'] == 'newest' ) {
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
// Grab the title for the first array
$title = $titles [ $tags_and_Qid['question_id'] ] ['title'];
// Grab the tags for the question from the second array
$tags = $end_array [ $tags_and_Qid['question_id'] ] ['tag'];
// Grab the username for the question from the second array
$username = $usernames [ $tags_and_Qid['question_id'] ] ['username'];
--- cut ----
}
}
I need to use this code often. The only difference is the array_reverse (..., true)
in the first example.
I have tried to solve the problem by making a function organize_question
to solve this problem. I was unsuccessful:
function organize_questions ( $tab ) {
if ( $_GET['tab'] == 'newest' ) {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
if ( $_GET['tab'] == 'oldest' ) {
echo ( "$end_array" );
// this does not work
} else {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
}
I then changed the relevant line in my code to this:
foreach( organize_question( $tab ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
The problem is in transferring variables from one function to another.
I tried to put all necessary variables in the parameters of the function, but everything gets broken, since there are many dependencies on this function.
I am new to PHP so there must be easier way to do this than what I am trying.
Upvotes: 1
Views: 2269
Reputation: 10033
function sortArray($direction, $array)
{
switch ($direction) {
case 'oldest':
return array_reverse($array, true);
case 'newest':
return $array;
default:
return array();
}
}
function processQuestions($array)
{
foreach($array as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] ) {
//code
}
}
$sortedArray = sortArray($tab, $end_array);
processQuestions($sortedArray);
And you should probably rewrite the following.
foreach($array as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
//could be rewritten as
foreach($array as $question_id => $title)
Upvotes: 0
Reputation: 11818
What you're looking for is a strategy....
$strategies = array(
'oldest' => create_function(
'$questions',
'return organize_questions($questions);'
),
'hottest' => create_function(
'$questions',
'return organize_questions(sort_by_hottness($questions));'
),
'default' => create_function(
'$questions',
'return organize_questions(array_reverse($questions, true));'
),
);
$strategy = 'default';
if (array_key_exists($strategies, $_GET['tab'])
$strategy = $_GET['tab'];
print_r( $strategies[$strategy]($questions) );
You're basically saying that you have these things (questions) that you want to do something on (sort them).
You might also want to look at the usort function, http://www.php.net/manual/en/function.usort.php
Upvotes: 1
Reputation: 13972
function organize_questions ()
{
if ( $_GET['tab'] == 'newest' )
{
print_r( array_reverse( $end_array , true ) );
}
else if ( $_GET['tab'] == 'oldest' )
{
print_r($end_array);
}
else
{
print_r(array_reverse ( $end_array , true ) );
}
}
I removed the echos and used print_r (assuming that those variables were actually arrays). Additionally unless you're using $tab somewhere else in the function it was unneeded.
EDIT: I wouldn't actually use print_r...it's useful for debug and such. Usually you'd want some way to pick the pieces from an array that you actually want to display and use echo or print for the individual pieces.
EDIT2: I'm getting both upvoted and downvoted on this. It's rewritten the function in question with correct syntax. Portions of the question are very vague so I'll continue. You seem to also be asking of passing information into functions. the $_GET['tab'] in question is accessing get variables (yoursite.com/index.php?tab=newest). What you seem to be asking with is how to use functions at all. You had it correct going with:
function organize_questions( $tab )
{
...
}
Assuming that you were going to use the variable tab. In order to use this function you would call it as such from another function within the file, or another file that does a php_require or php_include:
$mytab = 'bob';
organize_questions( $mytab);
And then you would use the original $tab in the function as you created it earlier or as I stated just above with $tab in the parameter list
Upvotes: 1
Reputation: 4932
It's a good question. You can definitely spend a long time just trying out different ways to keep yourself from reusing code. I would probably do one of the function suggestions as listed above, but another option would be to put the code in a separate PHP file and then include it where you want. This basically becomes the equivalent of an inline function in other languages, and if you're worried about speed of execution it's a good way to go. In most cases, however, you will be more worried about the size of page that you're sending the client over http, so this won't be as acceptable as writing a function. I'm mostly pointing out that each situation has a different "best" solution - in your case I'd say McAden's answer is a good one.
Using include:
//myscript.php
if ( $_GET['tab'] == 'newest' )
{
print_r( array_reverse( $end_array , true ) );
}
else if ( $_GET['tab'] == 'oldest' )
{
print_r($end_array);
}
else
{
print_r(array_reverse ( $end_array , true ) );
}
And then in your code later:
//myexecutionplace.php
$end_array = foo;
include 'myscript.php';
doStuffWith($end_array);
$end_array = foo2;
include 'myscript.php';
doStuffWith($end_array2);
Upvotes: 1
Reputation: 94147
It sounds like this part of your code does the bulk of the work:
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
-- cut ---
}
I would separate the $_GET['tab']
checking out of your organize_questions()
function, and do the parameter decisions elsewhere. Like this:
function organize_questions($array)
{
foreach($array as $questionId => $title )
{
//do the work
}
}
And then base your decision making code elsewhere:
if ( $_GET['tab'] == 'newest' )
{
organize_questions(array_reverse ( $end_array , true ));
}
else if ( $_GET['tab'] == 'oldest' )
{
organize_questions($end_array);
}
else
{
//etc.
}
Upvotes: 5