Joc de curse folosind session
Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
-
Komfort
- Mesaje:157
Joc de curse folosind session
va rog.....am nevoie de ajutorul vostru:( din nou
scuzati:(
indexcurse.php
Cod: Selectaţi tot
<?php
session_start();
echo '<center>';
echo '<h2>Home</h2>';
if(!isset($_SESSION['nume'])){ $_SESSION['nume']=" "; }
if(!isset($_SESSION['bani'])){ $_SESSION['bani']=10; }
echo '<form method=post action=" ">
Nume:<input type=text name=nume><br>
<input type=submit name=submit value=inscrie-te></form>';
if(isset($_POST['submit'])){
$_SESSION['nume']=$_POST['nume'];
echo 'Bun venit';
echo $_SESSION['nume'];
echo '<br>';
echo 'Banii tai sunt:';
echo $_SESSION['bani'];
echo '<br>';
echo '<a href="curse.php">Spre curse</a>';
}
?>
curse.php
Cod: Selectaţi tot
<?php
session_start();
echo '<center>';
echo '<h2>Curse de cai</h2>';
echo '<form method=post action=" ">
Pariaza:<input type=text name=suma><br>
Alege calul:<input type="radio" name="cal" value="1" /> <img src="cal1.jpg" height=46 width=46><br />
<input type="radio" name="cal" value="2" /> <img src="cal2.jpg" height=46 width=46><br />
<input type="radio" name="cal" value="3" /> <img src="cal3.jpg" height=46 width=46><br />
<input type=submit name=da value=pariaza></form>';
if(isset($_POST['da'])) {
if(!isset($_SESSION['cal1'])){ $_SESSION['cal1']=rand(0,10); }
if(!isset($_SESSION['cal2'])){ $_SESSION['cal2']=rand(0,16);}
if(!isset($_SESSION['cal3'])){ $_SESSION['cal3']=rand(0,30);}
if(!isset($_SESSION['suma'])){ $_SESSION['suma']=$_POST['suma'];}
if(!isset($_SESSION['cal'])){ $_SESSION['cal']=$_POST['cal'];}
echo 'Tu ai ales calul:';
echo $_SESSION['cal'];
echo '<br>';
if($_SESSION['cal']==1) { $_SESSION['cal']=$_SESSION['cal1'];}
if($_SESSION['cal']==2) { $_SESSION['cal']=$_SESSION['cal2'];}
if($_SESSION['cal']==3) { $_SESSION['cal']=$_SESSION['cal3'];}
if($_SESSION['cal']>=$_SESSION['cal1'] && $_SESSION['cal']>=$_SESSION['cal2'] && $_SESSION['cal']>=$_SESSION['cal3']) { echo 'calul tau a castigat'; $_SESSION['bani']=$_SESSION['bani']+$_SESSION['suma'];}
else { echo 'calul tau a pierdut'; $_SESSION['bani']=$_SESSION['bani']-$_SESSION['suma'];}
echo 'Mai ai:';
echo $_SESSION['bani'];
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal1']; echo"><img src='cal1.jpg' height=46 width=46></marquee>"; echo '<br>';
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal2']; echo"><img src='cal2.jpg' height=46 width=46></marquee>"; echo '<br>';
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal3']; echo"><img src='cal3.jpg' height=46 width=46></marquee>"; echo '<br>';
}
?>
reset.php
nu stiu de ce nu merge:(
http://www.testinfo.webuda.com/indexcurse.php
imi zice ca am ales calul 18:|
bani nu mi arata dupa fiecare castig sau pierderer:(?
va rog frumos sa ma ajutati daca se poate...multumesc mult
MarPlo
Mesaje:4343
Salut
Am facut un scurt test, si imi arata ca rezultat asa:
Acel 7 presupun ca sunt banii ramasi, deci ii arata.
Komfort
Mesaje:157
indexcurse.php
Cod: Selectaţi tot
<?php
echo '<center>';
echo '<h2>Home</h2>';
if(!isset($_SESSION['nume'])){ $_SESSION['nume']=" "; }
if(!isset($_SESSION['bani'])){ $_SESSION['bani']=1; }
echo '<form method=post action=" ">
Nume:<input type=text name=nume><br>
<input type=submit name=submit value=inscrie-te></form>';
if(isset($_POST['submit'])){
$_SESSION['nume']=$_POST['nume'];
echo 'Bun venit ';
echo $_SESSION['nume'];
echo '<br>';
echo 'Banii tai sunt:';
echo $_SESSION['bani'];
echo '<br>';
echo '<a href="curse.php">Spre curse</a>';
}
?>
curse.php
Cod: Selectaţi tot
<?php
echo '<center>';
echo '<h2>Curse de cai</h2>';
echo '<form method=post action=" ">
Pariaza:<input type=text name=suma><br>
Alege calul:<input type="radio" name="cal" value="1" /> <img src="cal1.jpg" height=46 width=46><br />
<input type="radio" name="cal" value="2" /> <img src="cal2.jpg" height=46 width=46><br />
<input type="radio" name="cal" value="3" /> <img src="cal3.jpg" height=46 width=46><br />
<input type=submit name=da value=pariaza></form>';
if(isset($_POST['da'])) {
if(!isset($_SESSION['cal1'])){ $_SESSION['cal1']=rand(0,10); }
if(!isset($_SESSION['cal2'])){ $_SESSION['cal2']=rand(0,16);}
if(!isset($_SESSION['cal3'])){ $_SESSION['cal3']=rand(0,30);}
if(!isset($_SESSION['suma'])){ $_SESSION['suma']=$_POST['suma'];}
if(!isset($_SESSION['cal'])){ $_SESSION['cal']=$_POST['cal'];}
echo 'Tu ai ales calul:';
echo $_SESSION['cal'];
echo '<br>';
if($_SESSION['cal']==1) { $_SESSION['cal']=$_SESSION['cal1'];}
if($_SESSION['cal']==2) { $_SESSION['cal']=$_SESSION['cal2'];}
if($_SESSION['cal']==3) { $_SESSION['cal']=$_SESSION['cal3'];}
if($_SESSION['cal']>=$_SESSION['cal1'] && $_SESSION['cal']>=$_SESSION['cal2'] && $_SESSION['cal']>=$_SESSION['cal3']) { echo 'calul tau a castigat'; $_SESSION['bani']=$_SESSION['bani']+$_SESSION['suma'];}
else { echo 'calul tau a pierdut'; $_SESSION['bani']=$_SESSION['bani']-$_SESSION['suma'];}
echo 'Mai ai:';
echo $_SESSION['bani'];
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal1']; echo"><img src='cal1.jpg' height=46 width=46></marquee>"; echo '<br>';
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal2']; echo"><img src='cal2.jpg' height=46 width=46></marquee>"; echo '<br>';
echo "<marquee DIRECTION=RIGHT SCROLLAMOUNT="; echo $_SESSION['cal3']; echo"><img src='cal3.jpg' height=46 width=46></marquee>"; echo '<br>';
}
echo '<br>';
echo '<br>';
$_SESSION['cal1']=" ";
$_SESSION['cal2']=" ";
$_SESSION['cal3']=" ";
$_SESSION['cal']=" ";
echo '<a href=reset.php>Reseteaza bani</a>';
?>
reset.php
merge foarte bine...dar de ce nu mi se salveaza bani
in sesiune:(?
multumesc??:(
MarPlo
Mesaje:4343
Lipseste session_start(); , alta eroare nu am observat, poate si findca prefer sa stau departe de jocuri de noroc fara sa ma bag cu mintea in ele.
Komfort
Mesaje:157
cand adaug session_start(); in curse.php....nu-mi arat ce cal am ales si banii cresc din 12 in 12 sau din 14 in 14:| si castig de fiecare data
cand nu adaug session_start(); tot decurge normal:(
chiar nu stiu
va rog sa ma ajutati
MULTUMESC:(
MarPlo
Mesaje:4343
O eroare, de logica, ar fi urmatoarea:
Deoarece conform codului:
Cod: Selectaţi tot
if(!isset($_SESSION['suma'])){ $_SESSION['suma']=$_POST['suma'];}
Suma se actualizeaza /adauga in $_SESSION['suma'] doar daca acea sesiune nu exista, odata ce a fost setata prima data, sare peste acea linie de cod si nu mai adauga ce e la $_POST['suma'] ramane cu valoarea adaugata prima data.
Incearca in loc de:
Cod: Selectaţi tot
... $_SESSION['bani']=$_SESSION['bani']+$_SESSION['suma']; ...
sa folosesti cu valoarea de la POST:
Cod: Selectaţi tot
$_SESSION['bani']=$_SESSION['bani']+$_POST['suma'];
- la fel si unde se scad.
Daca or mai fi si alte erori, nu mai stiu. Incearca sa verifici cu "echo" valorile variabilelor, poate asa vezi ce e incorect.
Komfort
Mesaje:157
da..acuma se salveaza suma dar...nu imi zice ce cal aleg...si castig intruna:(?
gataaaa am rezolvat
trebuia unset($_SESSION['cal']); in loc de $_SESSION['cal']=" "; etc..
multumesc oricum