Jump to content

custom functions?


joecooper

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.