joecooper Posted May 6, 2008 Share Posted May 6, 2008 im sorry, i did google it but didnt find what i was looking for... i have to do the same script 7 times over on a page, but would rather have it as a function and call it 7 times. $sql = "SELECT * FROM prices WHERE location='1' LIMIT 1"; //return the value of funds from the user account $result = mysql_query($sql); //do the query $row = mysql_fetch_array($result); //get results $lastnumber= $row['food']; $newnumber= rand($lastnumber-1, $lastnumber+1); echo("last: $lastnumber, new: $newnumber"); mysql_query("UPDATE prices SET food='$newnumber' WHERE location='1'"); the only thing that need to change per each time its run is: location='1' needs to be from 1 to 7 Link to comment https://forums.phpfreaks.com/topic/104441-custom-functions/ Share on other sites More sharing options...
Btown2 Posted May 6, 2008 Share Posted May 6, 2008 Make location a variable that is passby value on the function call... for instance PSEUDO CODE************** int myfunction(int location) and then call it whith myfunction($i) //where $i is the number you want location to be. ************************* Link to comment https://forums.phpfreaks.com/topic/104441-custom-functions/#findComment-534629 Share on other sites More sharing options...
rhodesa Posted May 6, 2008 Share Posted May 6, 2008 here it is as a function... <?php function doUpdate ( $id ) { $sql = "SELECT * FROM prices WHERE location='{$id}' LIMIT 1"; //return the value of funds from the user account $result = mysql_query($sql); //do the query $row = mysql_fetch_array($result); //get results $lastnumber= $row['food']; $newnumber= rand($lastnumber-1, $lastnumber+1); echo("id: $id, last: $lastnumber, new: $newnumber"); mysql_query("UPDATE prices SET food='$newnumber' WHERE location='{$id}'"); return $newnumber; } doUpdate(1); doUpdate(2); ?> are the 7 items in a row? if so you can just do a loop: <?php for($id=1;$id <= 7;$id++) { $sql = "SELECT * FROM prices WHERE location='{$id}' LIMIT 1"; //return the value of funds from the user account $result = mysql_query($sql); //do the query $row = mysql_fetch_array($result); //get results $lastnumber= $row['food']; $newnumber= rand($lastnumber-1, $lastnumber+1); echo("id: $id, last: $lastnumber, new: $newnumber"); mysql_query("UPDATE prices SET food='$newnumber' WHERE location='{$id}'"); } ?> Link to comment https://forums.phpfreaks.com/topic/104441-custom-functions/#findComment-534633 Share on other sites More sharing options...
joecooper Posted May 6, 2008 Author Share Posted May 6, 2008 thanks, i did think it could be done in a loop but it was too late i had put this message in lol.... but thats exactly what i needed! thanks! Link to comment https://forums.phpfreaks.com/topic/104441-custom-functions/#findComment-534661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.