Functie uitvoeren

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

John Doe

John Doe

21/06/2009 08:31:00
Quote Anchor link
Hoi allemaal
Ik heb een script gemaakt dat de prijs van een item van de site haalt en deze vermenigvuldigt met de hoeveelheid die je hebt. Dit is gelukt maar nou wou ik ook dat je meerdere items in kon vullen en dat alles bij elkaar op geteld word.

Oja als je andere dingen in het script opmerkt post die even.

Voorbeeld
Dit is mijn script
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
//laat alle errors zien
ini_set('display_errors',1);
error_reporting(E_ALL);
//checkt of het formulier in is gevult
if($_SERVER['REQUEST_METHOD'] == "POST" ) {
    
    
//Price checken en vermenigvuldigen met het amount
function getprice($id,$amount) {
$begin = "<b>Market price:</b> ";
$eind = "</span>";
$maximale_delay = 5;
$site = "http://itemdb-rs.runescape.com/viewitem.ws?obj=".$id;
$curl_init = curl_init();
curl_setopt ($curl_init, CURLOPT_URL, $site);
curl_setopt ($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_init, CURLOPT_CONNECTTIMEOUT, $maximale_delay);
$start = curl_exec($curl_init);
curl_close($curl_init);
$exploded = explode($begin,$start);
$exploded = explode($eind,$exploded[1]);
$result = str_replace('<br />','',nl2br($exploded[0]));
   return $price;
}


if(IsSet($_POST['calculate']))
{

  for($i=0; $i < count($amount); $i++)
  {

    //ZOEK EN BEREKEN
    $amount[$i] * getprice($id,$amount);
   }

   echo "hier zeg je nog wat";
}

}
else{
//het formulier
?>

<html>
<head>
<title>Price check</title>
</head>
<body>
<form id="pricecheckform" name="pricecheckform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="75%"border="0">
  <tr>
    <td>Amount</td>
    <td>Id</td>
  </tr>
  <tr>
    <td><input type="text" name="amount[1]" /></td>
    <td><input type="text" name="id[1]" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input name="calc" type="submit" value="Calculate" /></td>
  </tr>
</table>
</form>
</body>
</html>
<?php
}
?>
Gewijzigd op 01/01/1970 01:00:00 door John Doe
 
PHP hulp

PHP hulp

19/04/2024 05:08:57
 
Eddy E

Eddy E

21/06/2009 09:06:00
Quote Anchor link
Het is wellicht handig om dan een 2e functie te maken.
Of gewoon met een array doorlopen met array_walk().
Je zet je resultaten zo neer:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
<?php
$prices
= array_walk($_POST, "getprice", $_POST['id'], $_POST['amount']);
$total_price = array_sum($prices);
echo '<b>______________+<br>' . $total_price . '<br>';

// koppel ID aan prijs
$price[$_POST[10]] = $prices[10];
?>


Niet getest en vraagt wellicht nog een kleine check.
Gewijzigd op 01/01/1970 01:00:00 door Eddy E
 
Tikkes C

Tikkes C

21/06/2009 09:25:00
Quote Anchor link
je kan ook gewoon met een foreach werken...


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
<?PHP

Function getPrice($price(), $amount())
{


   foreach($price as $prijs)
   {

     hier tel je op enzovoort
    }
}


?>



je moet dat wel arrays meegeven aan je functie uiteraard...
 
John Doe

John Doe

21/06/2009 09:27:00
Quote Anchor link
Sorry maar ik ben nog niet zo goed met php, dus als ik nu meerdere inputboxen neem voor id en amount alles bij elkaar op getelt word? Ofwel ik heb eigenlijk geen idee hoe ik dit moet toepassen.

Edit: is het niet mogelijk een array van inputvelden te maken?
Gewijzigd op 01/01/1970 01:00:00 door John Doe
 
Tikkes C

Tikkes C

21/06/2009 09:37:00
Quote Anchor link
wel je noemt je tekstvelden in je html eerst en vooral amount[] en id[], op die manier hebben we arrays gecreerd van de verschillende input-waarden (je voegt uiteraard nog enkele velden amount en id toe)

nu geven we door:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

if(IsSet($_POST['calculate']))
{

  for($i=0; $i < count($amount); $i++)
  {

    //ZOEK EN BEREKEN
    $amount[$i] * $price ($price moet je opzoeken in je ding daarboven)
   }

   echo "hier zeg je nog wat";
}


?>
 
John Doe

John Doe

21/06/2009 09:50:00
Quote Anchor link
Moet het er ongeveer zo uitzien?
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
//laat alle errors zien
ini_set('display_errors',1);
error_reporting(E_ALL);
//checkt of het formulier in is gevult
if($_SERVER['REQUEST_METHOD'] == "POST" ) {
    
//Price checken en vermenigvuldigen met het amount
function getprice($id = array()){
$begin = "<b>Market price:</b> ";
$eind = "</span>";
$maximale_delay = 5;
$site = "http://itemdb-rs.runescape.com/viewitem.ws?obj=".$id;
$curl_init = curl_init();
curl_setopt ($curl_init, CURLOPT_URL, $site);
curl_setopt ($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_init, CURLOPT_CONNECTTIMEOUT, $maximale_delay);
$start = curl_exec($curl_init);
curl_close($curl_init);
$exploded = explode($begin,$start);
$exploded = explode($eind,$exploded[1]);
$result = str_replace('<br />','',nl2br($exploded[0]));
   return $price;
}


if(IsSet($_POST['calc']))
{

  for($i=0; $i < count($amount); $i++)
  {

    //ZOEK EN BEREKEN
    $amount[$i] * getprice($id,$amount);
   }

   echo "hier zeg je nog wat";
}

}
else{
//het formulier
?>

<html>
<head>
<title>Price check</title>
</head>
<body>
<form id="pricecheckform" name="pricecheckform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="75%"border="0">
  <tr>
    <td>Amount</td>
    <td>Id</td>
  </tr>
  <tr>
    <td><input type="text" name="amount[]" /></td>
    <td><input type="text" name="id[]" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input name="calc" type="submit" value="Calculate" /></td>
  </tr>
</table>
</form>
</body>
</html>
<?php
}
?>
Gewijzigd op 01/01/1970 01:00:00 door John Doe
 
Tikkes C

Tikkes C

21/06/2009 09:53:00
Quote Anchor link
nu geef je in je functie GEEN array mee ...

zet er:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php

function getprice($amount(), $id())

?>


verder geef je nog steeds maar 1 argument mee dus jah...

een array begint van argument 0


EDIT:
noem je velden gewoon amount[] en id[] (ZONDER CIJFER)
Gewijzigd op 01/01/1970 01:00:00 door Tikkes C
 
John Doe

John Doe

21/06/2009 10:06:00
Quote Anchor link
Heb het even aangepast in mijn laatste reactie maar nu krijg ik
Quote:
Parse error: syntax error, unexpected '(', expecting ')' in /customers/voogsgerd.nl/voogsgerd.nl/httpd.www/pricecheck.php on line 10
 
Tikkes C

Tikkes C

21/06/2009 10:09:00
Quote Anchor link
ohw sorry...maar er eens van $id = array()
 
John Doe

John Doe

21/06/2009 10:16:00
Quote Anchor link
heb het weer in de zelfde post aangepast maar nu krijg ik dit
Quote:
Notice: Undefined variable: amount in /customers/voogsgerd.nl/voogsgerd.nl/httpd.www/pricecheck.php on line 28
hier zeg je nog wat

Wat niet gek is aangezien $amount nog nergens waarde heeft gekregen.
 
Tikkes C

Tikkes C

21/06/2009 10:21:00
Quote Anchor link
dan zal je dat eens moeten doen :p
 
John Doe

John Doe

21/06/2009 10:27:00
Quote Anchor link
uhm als ik $_post['amount[]'] invoer zegt hij dat amount[] niet bestaat
 
Afra ca

Afra ca

21/06/2009 10:46:00
Quote Anchor link
$_POST['amount']= array();

$_POST['amount'][] = iets

enz.
 
John Doe

John Doe

21/06/2009 14:04:00
Quote Anchor link
Heb nu anderhalf uur lopen puzzellen en ik krijg het niet voor elkaar kan iemand anders het even goed in elkaar zetten?

Het ligt aan $amount

Dit heb ik nu
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
//laat alle errors zien
ini_set('display_errors',1);
error_reporting(E_ALL);
//checkt of het formulier in is gevult
if($_SERVER['REQUEST_METHOD'] == "POST" ) {
    
$amount= array();
$amount= $_POST['amount']['0'];
$id= array();
$id= $_POST['id']['0'];

//Price checken en vermenigvuldigen met het amount
function getprice($id){
$begin = "<b>Market price:</b> ";
$eind = "</span>";
$maximale_delay = 5;
$site = "http://itemdb-rs.runescape.com/viewitem.ws?obj=".$id;
$curl_init = curl_init();
curl_setopt ($curl_init, CURLOPT_URL, $site);
curl_setopt ($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_init, CURLOPT_CONNECTTIMEOUT, $maximale_delay);
$start = curl_exec($curl_init);
curl_close($curl_init);
$exploded = explode($begin,$start);
$exploded = explode($eind,$exploded[1]);
$result = str_replace('<br />','',nl2br($exploded[0]));
   return $result;
}


    for($i=0; $i < count($amount[$i]); $i++)
  {

   //ZOEK EN BEREKEN
   $price = $amount * getprice($id);
   }

   echo $price ;

}
else{
//het formulier
?>

<html>
<head>
<title>Price check</title>
</head>
<body>
<form id="pricecheckform" name="pricecheckform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="75%"border="0">
  <tr>
    <td>Amount</td>
    <td>Id</td>
  </tr>
  <tr>
    <td><input type="text" name="amount[]" /></td>
    <td><input type="text" name="id[]" /></td>
  </tr>
  <tr>
    <td><input type="text" name="amount[]" /></td>
    <td><input type="text" name="id[]" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input name="calc" type="submit" value="Calculate" /></td>
  </tr>
</table>
</form>
</body>
</html>
<?php
}
?>
Gewijzigd op 01/01/1970 01:00:00 door John Doe
 
Steen

steen

21/06/2009 14:21:00
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
function getprice($id = array()){


Is $id een array? Volgens mij niet hoor...
 
John Doe

John Doe

21/06/2009 14:40:00
Quote Anchor link
Maakt eigenlijk niks uit heb het nu verandert maar nothing happen. Ik kan nu maar 1 ding berekenen. Hoe zorg ik er voor dat hij dit bij elke inputbox doet?


Edit:
Het is gelukt was vergeten dat de waarde van id ook steeds moest veranderen. het script post ik binnenkort bij scripts.

Dankjewel allemaal

Lolzzzman
Gewijzigd op 01/01/1970 01:00:00 door John Doe
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.