You are on page 1of 28

Exercici 1.

Modifica el codi solució de l’exercici 47 punt 4 el qual t’adjunto aquí de forma que ara en processar-se el
formulari es mostri amb el següent format:

Indicacions i requeriments:
1. De moment l’únic que has de modificar és la presentació. Pel que respecta al que guardem en un
arxiu no cal que en aquest punt facis res.
2. A continuació et poso el codi de partida per a realizar la pràctica:
Arxiu index.php
<?php
if(!isset($_GET['tireqty'])){
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>


<script type="text/javascript">

//Events en jQuery
$(document).ready(function(){

//En clicar en l'input de tipus button genera'


//Em cal l'event live donat que estic afegint codi html dinàmicament'
$('input[type=button]').live("click", function(){
valida();
});

});

function valida(){

//Primer que res recullo el valor dels tres camps usant AJAX
var u=jQuery('input:eq(0)').attr('value');
var dos=jQuery('input:eq(1)').attr('value');
var tres=jQuery('input:eq(2)').attr('value');

//Si un dels tres no és un nombre el posare a '' i a més el poso groc


//En cas siguin nombres els deixo en blanc
if(!jQuery.isNumeric(u)){
jQuery('input:eq(0)').attr('value','');
jQuery('input:eq(0)').attr('class','groc');
} else {
jQuery('input:eq(0)').attr('class','none');
}
if(!jQuery.isNumeric(dos)){
jQuery('input:eq(1)').attr('value','');
jQuery('input:eq(1)').attr('class','groc');
} else {
jQuery('input:eq(1)').attr('class','none');
}
if(!jQuery.isNumeric(tres)){
jQuery('input:eq(2)').attr('value','');
jQuery('input:eq(2)').attr('class','groc');
} else {
jQuery('input:eq(2)').attr('class','none');
}

//Crifo a procesa perque faci el procesament en cas que calgui


procesa();

function procesa(){
//Primer que res recullo el valor dels tres camps usant AJAX
var atr1 = jQuery('input:eq(0)').attr('value');
var atr2 = jQuery('input:eq(1)').attr('value');
var atr3 = jQuery('input:eq(2)').attr('value');
//I miro si algun dels tres és null ja que els incorrectes els haure posat a null
if ((atr1 == '') || (atr2 == '') ||(atr3 == '')){

//Vaig a fer aquí la creació del paràgraf per jQuery primer recollint el contingut del paragraf
alert('cas dolent');
var cont = jQuery('form').html();

//Perquè no em dupliqui la frase torna a introduir les dades


//conto quants elements strong hi ha i si no n'hi ha cap en poso un'
if(jQuery('strong').length == 0){
jQuery('form').html(cont + '<p><strong class="imp">Torna a introduir les dades!</strong></p>');
}else{
jQuery('form').html(cont);
}

//poso el valor perque html no l'agafa'


if ((atr1 != '')){jQuery('input:eq(0)').attr('value',atr1);}
if ((atr2 != '')){jQuery('input:eq(1)').attr('value',atr2);}
if ((atr3 != '')){jQuery('input:eq(2)').attr('value',atr3);}
jQuery('input:eq(3)').attr('value','submitorder');
}
else {

//Em cal encara el valor seleccionat de com ens va trobar


var fnd = jQuery('select').val();

//Ara he de construir a ma la direcció


var direccio='http://localhost/P54/index.php?tireqty='+atr1+'&oilqty='+atr2+'&sparkqty='+atr3+'&find='+fnd;

document.location.href=direccio;
}

</script>

<style type="text/css">
body {
padding: 40px;
}

form {
border: 1px solid;
padding: 15px;
width: 320px;
background-color: #ccffcc;
}
/*La regla per a posar en bermell el tema de l'important'*/
.imp{
color: red;
text-decoration: underline;
}
.groc{
background-color: greenyellow;
}

</style>

<title>Primer PHP</title>
</head>
<body>

<form>
<table border="0">
<tr bgcolor="red">
<td width="150">Item</td>
<td width="150">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Spark plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>How did you find us?</td>
<td align="center">
<select name="find">
<option value="a">I am a regular costumer</option>
<option value="b">Phone directory</option>
<option value="c">Tv advertising</option>
<option value="d">Others</option>
</select>
</td>

</tr>
<tr>
<td colspan="2" align="left"><input type="button" value="submitorder"/></td>
</tr>
</table>

</form>
</body>
</html>

<?php
}
else {
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">

</style>

<title>Comanda processada</title>

</head>
<body>

<h1>Bob's auto parts</h1>


<h2>Comanda processada</h2>

<?php
echo '<p>Comanda processada amb PHP en data '.date('H:i, jS F').'</p>';

$tireqty=$_GET['tireqty'];
$oilqty=$_GET['oilqty'];
$sparkqty=$_GET['sparkqty'];

$find=$_GET['find'];

$total=$tireqty*2+$oilqty*5+$sparkqty*3;

echo '<h5>Les quantitats introduides son:</h5>';


echo '<p>Tires: ';
echo $tireqty;
echo '</p>';

echo '<p>Oil: ';


echo $oilqty;
echo '</p>';

echo '<p>Spark plugs: ' . $sparkqty . '</p>';

echo '<p>Preu abans de descomptes: <em>' . $total . '</em></p>';

switch ($find)
{
case 'a': echo '<p>Has indicat que ets un client habitual</p>';
echo '<p>Preu amb descompte: <em>' . $total*(0.7). '</em></p>';
break;
case 'b': echo '<p>>Has indicat que ens has trobat a la guia telefonica</p>';
echo '<p>Preu amb descompte: <em>' . $total*(0.8). '</em></p>';
break;
case 'c': echo "<p>Has indicat que ens has trobat gracies a l'anunci de TV</p>";
echo '<p>Preu amb descompte: <em>' . $total*(0.9). '</em></p>';
break;
case 'd': echo '<p>Has indicat que ens has trobat per altres causes</p>';
echo '<p>Preu amb descompte: <em>' . $total*(0.95). '</em></p>';
break;
}

//Procedeixo a guardar en un arxiu


//Obro l'arxiu per afegir
$fp = fopen("order.txt",'a');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

$comanda=$tireqty."\r\n".$oilqty."\r\n".$sparkqty."\r\n".$find."\r\n".$total."\r\n";
echo $comanda;
fwrite($fp, $comanda);
flock($fp,LOCK_UN);
fclose($fp);
}

?>
</body>
</html>
<?php
}
?>

Esnombre.php
<?php

$tireqty=$_POST['tireqty'];
$oilqty=$_POST['oilqty'];
$sparkqty=$_POST['sparkqty'];

$sor=0;

if (is_numeric($tireqty) && is_numeric($oilqty) && is_numeric($sparkqty)){


$sor=1;
echo $sor;
} else {
echo $sor;
}

?>
?>

Resposta :

<?php
if(!isset($_GET['tireqty'])){
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">

//Events en jQuery
$(document).ready(function(){

//En clicar en l'input de tipus button genera'


//Em cal l'event live donat que estic afegint codi html dinàmicament'
$('input[type=button]').live("click", function(){
valida();
});

});

function valida(){

//Primer que res recullo el valor dels tres camps usant AJAX
var u=jQuery('input:eq(0)').attr('value');
var dos=jQuery('input:eq(1)').attr('value');
var tres=jQuery('input:eq(2)').attr('value');

//Si un dels tres no és un nombre el posare a '' i a més el poso groc


//En cas siguin nombres els deixo en blanc
if(!jQuery.isNumeric(u)){
jQuery('input:eq(0)').attr('value','');
jQuery('input:eq(0)').attr('class','groc');
} else {
jQuery('input:eq(0)').attr('class','none');
}
if(!jQuery.isNumeric(dos)){
jQuery('input:eq(1)').attr('value','');
jQuery('input:eq(1)').attr('class','groc');
} else {
jQuery('input:eq(1)').attr('class','none');
}
if(!jQuery.isNumeric(tres)){
jQuery('input:eq(2)').attr('value','');
jQuery('input:eq(2)').attr('class','groc');
} else {
jQuery('input:eq(2)').attr('class','none');
}

//Crifo a procesa perque faci el procesament en cas que calgui


procesa();

function procesa(){
//Primer que res recullo el valor dels tres camps usant AJAX
var atr1 = jQuery('input:eq(0)').attr('value');
var atr2 = jQuery('input:eq(1)').attr('value');
var atr3 = jQuery('input:eq(2)').attr('value');
//I miro si algun dels tres és null ja que els incorrectes els haure posat a null
if ((atr1 == '') || (atr2 == '') ||(atr3 == '')){

//Vaig a fer aquí la creació del paràgraf per jQuery primer recollint el contingut del paragraf
alert('cas dolent');
var cont = jQuery('form').html();

//Perquè no em dupliqui la frase torna a introduir les dades


//conto quants elements strong hi ha i si no n'hi ha cap en poso un'
if(jQuery('strong').length == 0){
jQuery('form').html(cont + '<p><strong class="imp">Torna a introduir les dades!</strong></p>');
}else{
jQuery('form').html(cont);
}

//poso el valor perque html no l'agafa'


if ((atr1 != '')){jQuery('input:eq(0)').attr('value',atr1);}
if ((atr2 != '')){jQuery('input:eq(1)').attr('value',atr2);}
if ((atr3 != '')){jQuery('input:eq(2)').attr('value',atr3);}
jQuery('input:eq(3)').attr('value','submitorder');
}
else {

//Em cal encara el valor seleccionat de com ens va trobar


var fnd = jQuery('select').val();

//Ara he de construir a ma la direcció


var direccio='http://localhost/P54/index.php?tireqty='+atr1+'&oilqty='+atr2+'&sparkqty='+atr3+'&find='+fnd;

document.location.href=direccio;
}

</script>

<style type="text/css">
body {
padding: 40px;
}

form {
border: 1px solid;
padding: 15px;
width: 320px;
background-color: #ccffcc;
}
/*La regla per a posar en bermell el tema de l'important'*/
.imp{
color: red;
text-decoration: underline;
}
.groc{
background-color: greenyellow;
}

</style>

<title>Primer PHP</title>

</head>
<body>

<form>
<table border="0">
<tr bgcolor="red">
<td width="150">Item</td>
<td width="150">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Spark plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>How did you find us?</td>
<td align="center">
<select name="find">
<option value="a">I am a regular costumer</option>
<option value="b">Phone directory</option>
<option value="c">Tv advertising</option>
<option value="d">Others</option>
</select>
</td>

</tr>
<tr>
<td colspan="2" align="left"><input type="button" value="submitorder"/></td>
</tr>
</table>

</form>
</body>
</html>

<?php
}
else {
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
table{
border: 2px solid;

border-collapse: collapse;
}
thead{

border-bottom: 2px solid;


background-color: red;
}
th{
padding: 5px;
align: center;
border: 1px solid;
}
td{
padding: 5px;
align: center;
border: 1px solid;
}

</style>

<title>Comanda processada</title>

</head>
<body>

<h1>Bob's auto parts</h1>


<h2>Comanda processada</h2>

<?php
echo '<p>Comanda processada amb PHP en data '.date('H:i, jS F').'</p>';

$tireqty=$_GET['tireqty'];
$oilqty=$_GET['oilqty'];
$sparkqty=$_GET['sparkqty'];

$find=$_GET['find'];

$total=$tireqty*2+$oilqty*5+$sparkqty*3;

echo '<h5>Les quantitats introduides son:</h5>';


?>
<table>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th>
</tr>
</thead>
<tbody><tr>
<?php
echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

switch ($find)
{
case 'a': echo '<td>Has indicat que ets un client habitual</td>';
echo '<td>'. $total*(0.7). '</td>';
break;
case 'b': echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
echo '<td>'. $total*(0.8). '</td>';
break;
case 'c': echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
echo '<td>' . $total*(0.9). '</td>';
break;
case 'd': echo '<td>Has indicat que ens has trobat per altres causes</td>';
echo '<td>' . $total*(0.95). '</td>';
break;
}
//la data

echo '<td>'. date('H:i, jS F') . '</td>';

echo '</tr></tbody></table>';

//Procedeixo a guardar en un arxiu


//Obro l'arxiu per afegir
$fp = fopen("order.txt",'a');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

$comanda=$tireqty."\r\n".$oilqty."\r\n".$sparkqty."\r\n".$find."\r\n".$total."\r\n";
echo $comanda;
fwrite($fp, $comanda);
flock($fp,LOCK_UN);
fclose($fp);
}

?>
</body>
</html>
<?php
}
?>

Exercici 2.
Modifica el codi solució de l’exercici anterior de forma que ara el que aconsegueixis sigui el següent:

Indicacions i requeriments:
1. Fíxa-t’hi que a part de mostrar la darrera comanda introduïda des del formulari ens apareixen en
forma de resum totes les que el client ha emmagatzemat en l’arxiu de text.

Resposta :
<?php
if(!isset($_GET['tireqty'])){
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>


<script type="text/javascript">

//Events en jQuery
$(document).ready(function(){

//En clicar en l'input de tipus button genera'


//Em cal l'event live donat que estic afegint codi html dinàmicament'
$('input[type=button]').live("click", function(){
valida();
});

});

function valida(){

//Primer que res recullo el valor dels tres camps usant AJAX
var u=jQuery('input:eq(0)').attr('value');
var dos=jQuery('input:eq(1)').attr('value');
var tres=jQuery('input:eq(2)').attr('value');

//Si un dels tres no és un nombre el posare a '' i a més el poso groc


//En cas siguin nombres els deixo en blanc
if(!jQuery.isNumeric(u)){
jQuery('input:eq(0)').attr('value','');
jQuery('input:eq(0)').attr('class','groc');
} else {
jQuery('input:eq(0)').attr('class','none');
}
if(!jQuery.isNumeric(dos)){
jQuery('input:eq(1)').attr('value','');
jQuery('input:eq(1)').attr('class','groc');
} else {
jQuery('input:eq(1)').attr('class','none');
}
if(!jQuery.isNumeric(tres)){
jQuery('input:eq(2)').attr('value','');
jQuery('input:eq(2)').attr('class','groc');
} else {
jQuery('input:eq(2)').attr('class','none');
}

//Crifo a procesa perque faci el procesament en cas que calgui


procesa();

function procesa(){
//Primer que res recullo el valor dels tres camps usant AJAX
var atr1 = jQuery('input:eq(0)').attr('value');
var atr2 = jQuery('input:eq(1)').attr('value');
var atr3 = jQuery('input:eq(2)').attr('value');
//I miro si algun dels tres és null ja que els incorrectes els haure posat a null
if ((atr1 == '') || (atr2 == '') ||(atr3 == '')){

//Vaig a fer aquí la creació del paràgraf per jQuery primer recollint el contingut del paragraf
alert('cas dolent');
var cont = jQuery('form').html();

//Perquè no em dupliqui la frase torna a introduir les dades


//conto quants elements strong hi ha i si no n'hi ha cap en poso un'
if(jQuery('strong').length == 0){
jQuery('form').html(cont + '<p><strong class="imp">Torna a introduir les dades!</strong></p>');
}else{
jQuery('form').html(cont);
}
//poso el valor perque html no l'agafa'
if ((atr1 != '')){jQuery('input:eq(0)').attr('value',atr1);}
if ((atr2 != '')){jQuery('input:eq(1)').attr('value',atr2);}
if ((atr3 != '')){jQuery('input:eq(2)').attr('value',atr3);}
jQuery('input:eq(3)').attr('value','submitorder');
}
else {

//Em cal encara el valor seleccionat de com ens va trobar


var fnd = jQuery('select').val();

//Ara he de construir a ma la direcció


var direccio='http://localhost/P54/index.php?tireqty='+atr1+'&oilqty='+atr2+'&sparkqty='+atr3+'&find='+fnd;

document.location.href=direccio;
}

</script>

<style type="text/css">
body {
padding: 40px;
}

form {
border: 1px solid;
padding: 15px;
width: 320px;
background-color: #ccffcc;
}
/*La regla per a posar en bermell el tema de l'important'*/
.imp{
color: red;
text-decoration: underline;
}
.groc{
background-color: greenyellow;
}

</style>

<title>Primer PHP</title>

</head>
<body>

<form>
<table border="0">
<tr bgcolor="red">
<td width="150">Item</td>
<td width="150">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Spark plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>How did you find us?</td>
<td align="center">
<select name="find">
<option value="a">I am a regular costumer</option>
<option value="b">Phone directory</option>
<option value="c">Tv advertising</option>
<option value="d">Others</option>
</select>
</td>

</tr>
<tr>
<td colspan="2" align="left"><input type="button" value="submitorder"/></td>
</tr>
</table>

</form>
</body>
</html>

<?php
}
else {
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">
table{
border: 2px solid;

border-collapse: collapse;
}
thead{

border-bottom: 2px solid;


background-color: red;
}
th{
padding: 5px;
align: center;
border: 1px solid;
}
td{
padding: 5px;
align: center;
border: 1px solid;
}
caption{
background-color: oldlace;
border-top: 1px solid;
padding: 5px;
}

</style>

<title>Comanda processada</title>

</head>
<body>

<h1>Bob's auto parts</h1>


<h2>Comanda processada</h2>

<?php
echo '<p>Comanda processada amb PHP en data '.date('H:i, jS F').'</p>';

$tireqty=$_GET['tireqty'];
$oilqty=$_GET['oilqty'];
$sparkqty=$_GET['sparkqty'];

$find=$_GET['find'];

$total=$tireqty*2+$oilqty*5+$sparkqty*3;

echo '<h5>Les quantitats introduides son:</h5>';


?>
<table>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th>
</tr>
</thead>
<tbody><tr>
<?php
echo '<td>';
echo $tireqty;
echo '</td>';
echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

switch ($find)
{
case 'a': echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
break;
case 'b': echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
break;
case 'c': echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
break;
case 'd': echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
break;
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

//la data

echo '<td>'. date('H:i, jS F') . '</td>';

echo '</tr></tbody></table>';

//Procedeixo a guardar en un arxiu


//Obro l'arxiu per afegir
$fp = fopen("order.txt",'a');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);
$comanda=$tireqty."\r\n".$oilqty."\r\n".$sparkqty."\r\n".$total."\r\n".$find."\r\n".date('H:i, jS F')."\r\n".$totalD."\r\n";
//echo $comanda;

fwrite($fp, $comanda);
flock($fp,LOCK_EX);
fclose($fp);
}

?>

<!--Tema de la impressió de tot el contingut de l'arxiu order.txt-->


<br/>
<table>
<?php
echo '<caption>Resum de comandes fins: '. date('H:i, jS F').'</caption>';
?>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th>
</tr>
</thead>
<tbody><tr>
<?php
$fp = fopen("C:\wamp\www\P54\order.txt",'r');

if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

//Vaig extraient-los de 7 en 7 i els vaig imprimint


$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);
while(!feof($fp)){
echo '<tr>';

echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

//PHP strcmp() Function 0 - if the two strings are equal


//IF té el 1 com a true i el 0 com a false
//Per això em cap posar el negat
if (!strcmp('a',$find)){ echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
}
if (!strcmp('b',$find)){ echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
}
if (!strcmp('c',$find)){ echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
}
if (!strcmp('d',$find)) { echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

echo '<td>'. $data . '</td>';


echo '</tr>';
//Vaig extraient-los de 7 en 7 i els vaig imprimint
//Vigila que ara faig dos fgets per a tireqty ja que el cursor havia quedat darrera de la data i
//En la creació hi ha una el TOTAL que jo el calculo de nou!!!!!
$tireqty=fgets($fp,999);
$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);

flock($fp,LOCK_EX);
fclose($fp);
}

echo '</tr></tbody></table>';
?>

</body>
</html>
<?php
}
?>

Exercici 3.
Modifica el codi solució de l’exercici 2 de forma que ara el que se’t mostri sigui el següent:
Indicacions i requeriments:
1. Fíxa-t’hi que ara ha aparegut una nova columna en la taula resum en la que podem ticar no el
checkbox.
2. En cas d’estar ticat un checkbox i que es cliqui en el botó esborra ticats, s’han d’esborrar les
comandes ticades de l’arxiu order.txt. Podem ticar varies comandes si és que en volem esborrar vàries.
3. La forma de mirar en jQuery si un checkbox està ticat és la següent: $(elcheckbox).is(':checked')
4. De moment nomès cal que implementis l’esborrat en l’arxiu “font” order.txt. En l’exercici següent a
més a més el millorarem de forma que a part d’esborrar-se deixaran d’aparèixer en la taula.

Resposta :
INDEX.PHP

<?php
if(!isset($_GET['tireqty'])){
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>


<script type="text/javascript">

//Events en jQuery
$(document).ready(function(){

//En clicar en l'input de tipus button genera'


//Em cal l'event live donat que estic afegint codi html dinàmicament'
$('input[type=button]').live("click", function(){
valida();
});

});

function valida(){

//Primer que res recullo el valor dels tres camps usant AJAX
var u=jQuery('input:eq(0)').attr('value');
var dos=jQuery('input:eq(1)').attr('value');
var tres=jQuery('input:eq(2)').attr('value');
//Si un dels tres no és un nombre el posare a '' i a més el poso groc
//En cas siguin nombres els deixo en blanc
if(!jQuery.isNumeric(u)){
jQuery('input:eq(0)').attr('value','');
jQuery('input:eq(0)').attr('class','groc');
} else {
jQuery('input:eq(0)').attr('class','none');
}
if(!jQuery.isNumeric(dos)){
jQuery('input:eq(1)').attr('value','');
jQuery('input:eq(1)').attr('class','groc');
} else {
jQuery('input:eq(1)').attr('class','none');
}
if(!jQuery.isNumeric(tres)){
jQuery('input:eq(2)').attr('value','');
jQuery('input:eq(2)').attr('class','groc');
} else {
jQuery('input:eq(2)').attr('class','none');
}

//Crifo a procesa perque faci el procesament en cas que calgui


procesa();

function procesa(){
//Primer que res recullo el valor dels tres camps usant AJAX
var atr1 = jQuery('input:eq(0)').attr('value');
var atr2 = jQuery('input:eq(1)').attr('value');
var atr3 = jQuery('input:eq(2)').attr('value');
//I miro si algun dels tres és null ja que els incorrectes els haure posat a null
if ((atr1 == '') || (atr2 == '') ||(atr3 == '')){

//Vaig a fer aquí la creació del paràgraf per jQuery primer recollint el contingut del paragraf
alert('cas dolent');
var cont = jQuery('form').html();

//Perquè no em dupliqui la frase torna a introduir les dades


//conto quants elements strong hi ha i si no n'hi ha cap en poso un'
if(jQuery('strong').length == 0){
jQuery('form').html(cont + '<p><strong class="imp">Torna a introduir les dades!</strong></p>');
}else{
jQuery('form').html(cont);
}

//poso el valor perque html no l'agafa'


if ((atr1 != '')){jQuery('input:eq(0)').attr('value',atr1);}
if ((atr2 != '')){jQuery('input:eq(1)').attr('value',atr2);}
if ((atr3 != '')){jQuery('input:eq(2)').attr('value',atr3);}
jQuery('input:eq(3)').attr('value','submitorder');
}
else {

//Em cal encara el valor seleccionat de com ens va trobar


var fnd = jQuery('select').val();

//Ara he de construir a ma la direcció


var direccio='http://localhost/P54/index.php?tireqty='+atr1+'&oilqty='+atr2+'&sparkqty='+atr3+'&find='+fnd;

document.location.href=direccio;
}

</script>

<style type="text/css">
body {
padding: 40px;
}

form {
border: 1px solid;
padding: 15px;
width: 320px;
background-color: #ccffcc;
}
/*La regla per a posar en bermell el tema de l'important'*/
.imp{
color: red;
text-decoration: underline;
}
.groc{
background-color: greenyellow;
}
</style>

<title>Primer PHP</title>

</head>
<body>

<form>
<table border="0">
<tr bgcolor="red">
<td width="150">Item</td>
<td width="150">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Spark plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>How did you find us?</td>
<td align="center">
<select name="find">
<option value="a">I am a regular costumer</option>
<option value="b">Phone directory</option>
<option value="c">Tv advertising</option>
<option value="d">Others</option>
</select>
</td>

</tr>
<tr>
<td colspan="2" align="left"><input type="button" value="submitorder"/></td>
</tr>
</table>

</form>
</body>
</html>

<?php
}
else {
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--Em cal ara afegir els scripts-->


<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
jQuery('input:last').click(function(){
//Miro quins són els que estan ticats
//i els posaré en un arrau
var arr = new Array();
var c=0;
var n = jQuery('input').length;
for(var i=0;i<n;i++){
if(jQuery('input:eq('+i+')').is(':checked')){
//Vaig posant a dins d'un array les posicions ticades'
alert("ticat: "+i+" poscion array: "+c);
arr[c]=i;
c=c+1;
}else{
alert("no ticat: "+i);}
}

//Fent Ajax aconsegueixo que me'ls esborri un a un'


alert("llargada array: "+arr.length);

//Vegades que ha de fer AJAX


var na=arr.length;

//Nombre de "entrades"
n=n-1;

for(z=arr.length;z>0;z--){
var pos=arr[z-1];
alert("posició extreta de l'array: "+(z-1)+" amb volta a esborrar pos: "+pos);
alert("Nombre de vegades que faré AJAX: "+na);
alert("Nombre de voltes a larxiu nombre: "+n);
$.ajax({
type: 'POST',
url: 'http://localhost/P54/esborra.php',
dataType: 'html',
data: {
nombre: n,
posicio: pos
},
success: function(suc) {
alert('el retornat: '+suc);
},
error: function() {alert('An error occurred!');}
});
//Una menys
n=n-1;
}

//

});

});

</script>

<style type="text/css">
table{
border: 2px solid;

border-collapse: collapse;
}
thead{

border-bottom: 2px solid;


background-color: red;
}
th{
padding: 5px;
align: center;
border: 1px solid;
}
td{
padding: 5px;
align: center;
border: 1px solid;
}
caption{
background-color: oldlace;
border-top: 1px solid;
padding: 5px;
}

</style>

<title>Comanda processada</title>

</head>
<body>

<h1>Bob's auto parts</h1>


<h2>Comanda processada</h2>

<?php
echo '<p>Comanda processada amb PHP en data '.date('H:i, jS F').'</p>';

$tireqty=$_GET['tireqty'];
$oilqty=$_GET['oilqty'];
$sparkqty=$_GET['sparkqty'];
$find=$_GET['find'];

$total=$tireqty*2+$oilqty*5+$sparkqty*3;

echo '<h5>Les quantitats introduides son:</h5>';


?>
<table>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th>
</tr>
</thead>
<tbody><tr>
<?php
echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

switch ($find)
{
case 'a': echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
break;
case 'b': echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
break;
case 'c': echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
break;
case 'd': echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
break;
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

//la data

echo '<td>'. date('H:i, jS F') . '</td>';

echo '</tr></tbody></table>';

//Procedeixo a guardar en un arxiu


//Obro l'arxiu per afegir
$fp = fopen("order.txt",'a');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);
$comanda=$tireqty."\r\n".$oilqty."\r\n".$sparkqty."\r\n".$total."\r\n".$find."\r\n".date('H:i, jS F')."\r\n".$totalD."\r\n";
//echo $comanda;

fwrite($fp, $comanda);
flock($fp,LOCK_EX);
fclose($fp);
}

?>

<!--Tema de la impressió de tot el contingut de l'arxiu order.txt-->


<br/>
<table>
<?php
echo '<caption>Resum de comandes fins: '. date('H:i, jS F').'</caption>';
?>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th><th>Esborrar</th>
</tr>
</thead>
<tbody><tr>
<?php
$fp = fopen("C:\wamp\www\P54\order.txt",'r');

if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

//Variable $i per anar numerant els checboxes


$i=0;

//Vaig extraient-los de 7 en 7 i els vaig imprimint


$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);
while(!feof($fp)){
echo '<tr>';

echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

//PHP strcmp() Function 0 - if the two strings are equal


//IF té el 1 com a true i el 0 com a false
//Per això em cap posar el negat
if (!strcmp('a',$find)){ echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
}
if (!strcmp('b',$find)){ echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
}
if (!strcmp('c',$find)){ echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
}
if (!strcmp('d',$find)) { echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

echo '<td>'. $data . '</td>';


echo '<td><input type="checkbox" name="chkb'.$i.'" disable="false" /></td>';
echo '</tr>';
//Vaig extraient-los de 7 en 7 i els vaig imprimint
//Vigila que ara faig dos fgets per a tireqty ja que el cursor havia quedat darrera de la data i
//En la creació hi ha una el TOTAL que jo el calculo de nou!!!!!
$tireqty=fgets($fp,999);
$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);

//Incremento $i
$i=$i+1;
}

//Tema del botó per esborrar els que toqui


echo '</tr>';
echo '<tr><td colspan="8"><input type="button" value="Esborra ticats" name="esborraBoto"/></td></tr>';

flock($fp,LOCK_EX);
fclose($fp);
}

echo '</tr></tbody></table>';
?>

</body>
</html>
<?php
}
?>

ESBORRA.PHP

<?php

$pos=$_POST['posicio'];

//Li passo el nombre per saber quants grups he de llegir


$nombre=$_POST['nombre'];

$fp = fopen("C:\wamp\www\P54\order.txt",'r');

$pos=$pos+1;

//Inicialitzo les dues variables


$con="";
$kk="";

//Inicialitzo la volta que estic donant


$volta=1;

if(!$fp){
echo '<p>No he pogut. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

//Vaig extraient-los de 7 en 7
while(!feof($fp) && $nombre>=$volta){
if($volta != $pos){
$con=$con.fgets($fp,1024);
$con=$con.fgets($fp,1024);
$con=$con.fgets($fp,1024);
$con=$con.fgets($fp,1024);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$con=$con.fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$con=$con.fgets($fp,1024);
$con=$con.fgets($fp,1024);
$con=$con.fgets($fp,1024);

}
if($volta == $pos){
$kk=$kk.fgets($fp,1024);
$kk=$kk.fgets($fp,1024);
$kk=$kk.fgets($fp,1024);
$kk=$kk.fgets($fp,1024);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$kk=$kk.fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$kk=$kk.fgets($fp,1024);
$kk=$kk.fgets($fp,1024);
$kk=$kk.fgets($fp,1024);
}
$volta=$volta+1;
}

flock($fp,LOCK_UN);
fclose($fp);
}

//Un cop reconstruit ho torno a ficar en el mateix arxiu

$fq = fopen("C:\wamp\www\P54\order.txt",'w+');

if(!$fq){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fq,LOCK_EX);

fwrite($fq, $con);

flock($fq,LOCK_UN);
fclose($fq);
}

echo 'OK';
?>

Exercici 4.
Modifica el codi solució de l’exercici 3 de forma que ara el que se’t mostri sigui el següent:
Tico el primer i el tercer:

I com seria desitjable a part d’esborrar-se de l’arxiu order.txt desapareixen de la taula tal com pots
veure:
Resposta :

<?php
if(!isset($_GET['tireqty'])){
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>


<script type="text/javascript">

//Events en jQuery
$(document).ready(function(){

//En clicar en l'input de tipus button genera'


//Em cal l'event live donat que estic afegint codi html dinàmicament'
$('input[type=button]').live("click", function(){
valida();
});

});

function valida(){

//Primer que res recullo el valor dels tres camps usant AJAX
var u=jQuery('input:eq(0)').attr('value');
var dos=jQuery('input:eq(1)').attr('value');
var tres=jQuery('input:eq(2)').attr('value');

//Si un dels tres no és un nombre el posare a '' i a més el poso groc


//En cas siguin nombres els deixo en blanc
if(!jQuery.isNumeric(u)){
jQuery('input:eq(0)').attr('value','');
jQuery('input:eq(0)').attr('class','groc');
} else {
jQuery('input:eq(0)').attr('class','none');
}
if(!jQuery.isNumeric(dos)){
jQuery('input:eq(1)').attr('value','');
jQuery('input:eq(1)').attr('class','groc');
} else {
jQuery('input:eq(1)').attr('class','none');
}
if(!jQuery.isNumeric(tres)){
jQuery('input:eq(2)').attr('value','');
jQuery('input:eq(2)').attr('class','groc');
} else {
jQuery('input:eq(2)').attr('class','none');
}

//Crifo a procesa perque faci el procesament en cas que calgui


procesa();
}

function procesa(){
//Primer que res recullo el valor dels tres camps usant AJAX
var atr1 = jQuery('input:eq(0)').attr('value');
var atr2 = jQuery('input:eq(1)').attr('value');
var atr3 = jQuery('input:eq(2)').attr('value');
//I miro si algun dels tres és null ja que els incorrectes els haure posat a null
if ((atr1 == '') || (atr2 == '') ||(atr3 == '')){

//Vaig a fer aquí la creació del paràgraf per jQuery primer recollint el contingut del paragraf
alert('cas dolent');
var cont = jQuery('form').html();

//Perquè no em dupliqui la frase torna a introduir les dades


//conto quants elements strong hi ha i si no n'hi ha cap en poso un'
if(jQuery('strong').length == 0){
jQuery('form').html(cont + '<p><strong class="imp">Torna a introduir les dades!</strong></p>');
}else{
jQuery('form').html(cont);
}

//poso el valor perque html no l'agafa'


if ((atr1 != '')){jQuery('input:eq(0)').attr('value',atr1);}
if ((atr2 != '')){jQuery('input:eq(1)').attr('value',atr2);}
if ((atr3 != '')){jQuery('input:eq(2)').attr('value',atr3);}
jQuery('input:eq(3)').attr('value','submitorder');
}
else {

//Em cal encara el valor seleccionat de com ens va trobar


var fnd = jQuery('select').val();

//Ara he de construir a ma la direcció


var direccio='http://localhost/P54/index.php?tireqty='+atr1+'&oilqty='+atr2+'&sparkqty='+atr3+'&find='+fnd;

document.location.href=direccio;
}

</script>

<style type="text/css">
body {
padding: 40px;
}

form {
border: 1px solid;
padding: 15px;
width: 320px;
background-color: #ccffcc;
}
/*La regla per a posar en bermell el tema de l'important'*/
.imp{
color: red;
text-decoration: underline;
}
.groc{
background-color: greenyellow;
}

</style>

<title>Primer PHP</title>

</head>
<body>

<form>
<table border="0">
<tr bgcolor="red">
<td width="150">Item</td>
<td width="150">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Spark plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>How did you find us?</td>
<td align="center">
<select name="find">
<option value="a">I am a regular costumer</option>
<option value="b">Phone directory</option>
<option value="c">Tv advertising</option>
<option value="d">Others</option>
</select>
</td>

</tr>
<tr>
<td colspan="2" align="left"><input type="button" value="submitorder"/></td>
</tr>
</table>

</form>
</body>
</html>

<?php
}
else {
?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--Em cal ara afegir els scripts-->


<script type="text/JavaScript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
jQuery('input:last').click(function(){
//Miro quins són els que estan ticats
//i els posaré en un arrau
var arr = new Array();
var c=0;
var n = jQuery('input').length;
for(var i=0;i<n;i++){
if(jQuery('input:eq('+i+')').is(':checked')){
//Vaig posant a dins d'un array les posicions ticades'
alert("ticat: "+i+" poscion array: "+c);
arr[c]=i;
c=c+1;
}else{
alert("no ticat: "+i);}
}

//Fent Ajax aconsegueixo que me'ls esborri un a un'


alert("llargada array: "+arr.length);

//Vegades que ha de fer AJAX


var na=arr.length;

//Nombre de "entrades"
n=n-1;

for(z=arr.length;z>0;z--){
var pos=arr[z-1];

//Vaig a esborrar-los de la taula un per un


//Com que l'ordre és thead tfood i tbody CREC QUE PER AQUESTA CAUSA N'HE DE SUMAR 2 DE TH'S!!!!
var pos2=pos+2;
alert('tr:eq('+pos2+'),table:eq(1)');
alert(jQuery('tr:eq('+pos2+')','table:eq(1)').html());
jQuery('tr:eq('+pos2+')','table:eq(1)').remove();

alert("posició extreta de l'array: "+(z-1)+" amb volta a esborrar pos: "+pos);


alert("Nombre de vegades que faré AJAX: "+na);
alert("Nombre de voltes a larxiu nombre: "+n);
$.ajax({
type: 'POST',
url: 'http://localhost/P54/esborra.php',
dataType: 'html',
data: {
nombre: n,
posicio: pos
},
success: function(suc) {
alert('el retornat: '+suc);
},
error: function() {alert('An error occurred!');}
});
//Una menys
n=n-1;
}

//

});

});

</script>

<style type="text/css">
table{
border: 2px solid;

border-collapse: collapse;
}
thead{

border-bottom: 2px solid;


background-color: red;
}
th{
padding: 5px;
align: center;
border: 1px solid;
}
td{
padding: 5px;
align: center;
border: 1px solid;
}
caption{
background-color: oldlace;
border-top: 1px solid;
padding: 5px;
}

</style>

<title>Comanda processada</title>

</head>
<body>

<h1>Bob's auto parts</h1>


<h2>Comanda processada</h2>

<?php
echo '<p>Comanda processada amb PHP en data '.date('H:i, jS F').'</p>';

$tireqty=$_GET['tireqty'];
$oilqty=$_GET['oilqty'];
$sparkqty=$_GET['sparkqty'];

$find=$_GET['find'];

$total=$tireqty*2+$oilqty*5+$sparkqty*3;

echo '<h5>Les quantitats introduides son:</h5>';


?>
<table>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th>
</tr>
</thead>
<tbody><tr>
<?php
echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

switch ($find)
{
case 'a': echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
break;
case 'b': echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
break;
case 'c': echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
break;
case 'd': echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
break;
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

//la data

echo '<td>'. date('H:i, jS F') . '</td>';

echo '</tr></tbody></table>';

//Procedeixo a guardar en un arxiu


//Obro l'arxiu per afegir
$fp = fopen("order.txt",'a');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);
$comanda=$tireqty."\r\n".$oilqty."\r\n".$sparkqty."\r\n".$total."\r\n".$find."\r\n".date('H:i, jS F')."\r\n".$totalD."\r\n";
//echo $comanda;

fwrite($fp, $comanda);
flock($fp,LOCK_EX);
fclose($fp);
}

?>

<!--Tema de la impressió de tot el contingut de l'arxiu order.txt-->


<br/>
<table>
<?php
echo '<caption>Resum de comandes fins: '. date('H:i, jS F').'</caption>';
?>
<thead>
<tr>
<th>Tires</th><th>Oil</th><th>Spark plugs</th><th>Preu (a/d)</th><th>Tipus client</th><th>Preu final</th><th>Data</th><th>Esborrar</th>
</tr>
</thead>
<tbody><tr>
<?php
$fp = fopen("C:\wamp\www\P54\order.txt",'r');
if(!$fp){
echo '<p>No he pogut emmagatzemar el contingut de la comanda. Torna a introduir-la</p>';
}
else {
//En cas que hagi pogut obrir bloquejo
flock($fp,LOCK_EX);

//Variable $i per anar numerant els checboxes


$i=0;

//Vaig extraient-los de 7 en 7 i els vaig imprimint


$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);
while(!feof($fp)){
echo '<tr>';

echo '<td>';
echo $tireqty;
echo '</td>';

echo '<td>';
echo $oilqty;
echo '</td>';

echo '<td>' . $sparkqty . '</td>';

echo '<td>'. $total . '</td>';

//PHP strcmp() Function 0 - if the two strings are equal


//IF té el 1 com a true i el 0 com a false
//Per això em cap posar el negat
if (!strcmp('a',$find)){ echo '<td>Has indicat que ets un client habitual</td>';
$totalD = $total*(0.7);
}
if (!strcmp('b',$find)){ echo '<td>Has indicat que ens has trobat a la guia telefonica</td>';
$totalD = $total*(0.8);
}
if (!strcmp('c',$find)){ echo "<td>Has indicat que ens has trobat gracies a l'anunci de TV</td>";
$totalD = $total*(0.9);
}
if (!strcmp('d',$find)) { echo '<td>Has indicat que ens has trobat per altres causes</td>';
$totalD = $total*(0.95);
}

//Calculo en el swuitch el total amb descompte i aquí l'imprimeixo

echo '<td>'. $totalD. '</td>';

echo '<td>'. $data . '</td>';


echo '<td><input type="checkbox" name="chkb'.$i.'" disable="false" /></td>';
echo '</tr>';
//Vaig extraient-los de 7 en 7 i els vaig imprimint
//Vigila que ara faig dos fgets per a tireqty ja que el cursor havia quedat darrera de la data i
//En la creació hi ha una el TOTAL que jo el calculo de nou!!!!!
$tireqty=fgets($fp,999);
$tireqty=fgets($fp,999);
$oilqty=fgets($fp,999);
$sparkqty=fgets($fp,999);
$total=fgets($fp,999);
//Al tanto que aquí em cal la fgetc!!!!!!!!!!
$find=fgetc($fp);
//Per la data faig dos fgets ja que amb el primer el que aconsegueixo es arribar fins a final de línia
$data=fgets($fp,999);
$data=fgets($fp,999);

//Incremento $i
$i=$i+1;

//Tema del botó per esborrar els que toqui


echo '</tr>';
echo '<tr><td colspan="8"><input type="button" value="Esborra ticats" name="esborraBoto"/></td></tr>';
flock($fp,LOCK_EX);
fclose($fp);
}

echo '</tr></tbody></table>';
?>

</body>
</html>
<?php
}
?>

You might also like