chaseman Posted February 21, 2011 Share Posted February 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/ Share on other sites More sharing options...
Psycho Posted February 21, 2011 Share Posted February 21, 2011 Have the function return an array of those three values. Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/#findComment-1177636 Share on other sites More sharing options...
chaseman Posted February 21, 2011 Author Share Posted February 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/#findComment-1177659 Share on other sites More sharing options...
chaseman Posted February 21, 2011 Author Share Posted February 21, 2011 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/#findComment-1177682 Share on other sites More sharing options...
kenrbnsn Posted February 21, 2011 Share Posted February 21, 2011 You are creating a form in the function. The $_POST array won't be populated until after the form is submitted, so this function doesn't have any values to return. What are you trying to accomplish? Ken Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/#findComment-1177683 Share on other sites More sharing options...
chaseman Posted February 21, 2011 Author Share Posted February 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228391-how-to-avoid-using-globals-in-function/#findComment-1177698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.