Jump to content

How to Avoid Using GLOBALS in Function?


chaseman

Recommended Posts

I'm learning functions and I'm working on a rating script. This is a small test script that works, you can try it out yourself:

 

<?php

// Rating System

function while_test (){

$a = 1;
$b = 4;
$t_id = 1;

global $likes;
global $dislikes;
global $con_id;

while ($a++ <= $b){

echo "<center>";
echo "<table><tr><td>Table: </td></tr>";
echo "<tr><td>This is a table test </td></tr>"; 
echo "<tr><td><form action='' method='post'>";
echo "<button type='submit' name='likes' value='Y'>likes</button>";
echo "<button type='submit' name='dislikes' value='N'>dislikes</button>";
echo "<input type='hidden' name='hidden_id' value='" . $t_id . "' /></form></td></tr></table>"; 
echo "</center><br /><br />"; 

$t_id++;

$likes = $_POST['likes'];	
$dislikes = $_POST['dislikes'];
$con_id = $_POST['hidden_id'];

	}

}



while_test();


if ($likes) {

echo "likes it: " . $likes . " con_id: " . $con_id;

} elseif ($dislikes) {

echo "dislikes it: " . $dislikes . " con_id: " . $con_id;

}

?>

 

I've gotten recommended before not use globals, because the projects would become unmanageable, and I'm wondering how would I be able to avoid using globals in this example?

 

I'm able to in-ject variables through the parenthesis, but I'm not able to out-ject variables, if that makes sense. (?)

At least it doesn't work for me.

 

How would I use those three variables $likes, $dislikes and $con_id outside the function without setting them as globals, what would be good practice?

Link to comment
Share on other sites

I must be doing something wrong, I'm not even able to echo out the array INSIDE the function let alone outside the function.

 

This is what I tried:

 


// Rating System

function while_test ($array){

$a = 1;
$b = 4;
$t_id = 1;



while ($a++ <= $b){

echo "<center>";
echo "<table><tr><td>Table: </td></tr>";
echo "<tr><td>This is a table test </td></tr>"; 
echo "<tr><td><form action='' method='post'>";
echo "<button type='submit' name='likes' value='Y'>likes</button>";
echo "<button type='submit' name='dislikes' value='N'>dislikes</button>";
echo "<input type='hidden' name='hidden_id' value='" . $t_id . "' /></form></td></tr></table>"; 
echo "</center><br /><br />"; 

$t_id++;

$likes = $_POST['likes'];	
$dislikes = $_POST['dislikes'];
$con_id = $_POST['hidden_id'];

$array = array ($likes, $dislikes, $con_id);

	}


echo "test " . $array[0];
return $array;
}


 

Again echoing out the array does not work inside the function nor does it work outside the function.

 

I simply get "test" with no variable value.

Link to comment
Share on other sites

When I make the array global, like this one, it works, do I take the global away it does not work again:

 

// Rating System

function while_test ($array){

$a = 1;
$b = 4;
$t_id = 1;

global $array;

while ($a++ <= $b){

echo "<center>";
echo "<table><tr><td>Table: </td></tr>";
echo "<tr><td>This is a table test </td></tr>"; 
echo "<tr><td><form action='' method='post'>";
echo "<button type='submit' name='likes' value='Y'>likes</button>";
echo "<button type='submit' name='dislikes' value='N'>dislikes</button>";
echo "<input type='hidden' name='hidden_id' value='" . $t_id . "' /></form></td></tr></table>"; 
echo "</center><br /><br />"; 

$t_id++;

$likes = $_POST['likes'];	
$dislikes = $_POST['dislikes'];
$con_id = $_POST['hidden_id'];

$array = array ($likes, $dislikes, $con_id);

	}



return $array;
}



while_test($array);




if ($array[0]){

echo "likes: " . $array[0] . "con_id: " . $array[2];

} elseif ($array[1]) {

echo "dislikes: " . $array[1] . "con_id: " . $array[2];

}

Link to comment
Share on other sites

ohh ok that makes sense.

 

Basically I want to print out a list of tables with fetched data from the database, the list can be sorted by category AND/OR ordered by date, I created the sorting functionality with a bunch of if and elseif statements and INSIDE those I insert the FUNCTION with the table to avoid DUPLICATE CODE.

 

NOW, I want to implement a database based rating functionality, similar to the one in the test script, with likes and dislikes buttons. The user clicks on one of the buttons, and the button gives the value to the script OUTSIDE the function. Which is +1 (for one more vote) and it also gives the HIDDEN VALUE of the con_id (which is the auto increment value in the database) so the vote is being added to the correct row in the database.

 

The query would look something like this:

 

UPDATE con SET likes = likes+1 WHERE con_id = $con_id;

 

I hope that made sense.

 

Since I've gotten recommended to not use globals, I wondered how to make the code better and avoid globals.

 

But so more I think about it I'm realizing that this case is acceptable for a global. If I simply put the array as a global like I did in the example above it should be ok, or am I seeing it wrongly?

Link to comment
Share on other sites

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.