Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [php] Klasa - jak przekazac zmienna
Forum PHP.pl > Forum > Przedszkole
alanoe
W funkcji reserve() dodaje do zmiennej tablicowej reservationId
wartosci potrzebne mi takze w funkcji delete().

Wydawalo mi sie ze nadajac zmiennej wartosc w jednej funkcji
bede mogla ja odczytac rowniez w innej funkcji tej samej klasy.
Tak jednak nie jest sad.gif i w innej funkcji odczytuje ja jako tablice pusta...

Z tego wzgledu mam pytanie jak przekazac reservationId z jednej do drugiej funkcji w klasie?



  1. <?php
  2. // funkcja reserve
  3.  
  4. (...)
  5.  
  6. $this->messages[] = $this->msg;
  7.  
  8. $this->reservationId[] = $id;
  9. }
  10. return $this->messages;
  11. }
  12. public function delete()
  13. {
  14. $items = count($this->reservationId);
  15.  
  16. (...)
  17. ?>
kriqs
a napewno na poczatku zdeklarowales ta zmienna questionmark.gif
alanoe
Tak zadeklarowalam smile.gif

Zauwazylam jeszcze jedna rzecz...
Kiedy mam zadeklarowana zmienna $p w konstruktorze jako 0
dalej zmieniam ja w funkcji reserve na 5
to w funkcji delete ma ona znow wartosc 0 !
Zapewne to samo dzieje sie z moja tablica reservationId...
?
Jak wiec zatrzymac wartosci tej tablicy dla innych funkcji ?
  1. <?php
  2. public $reservationId;
  3.  
  4. public function __construct()
  5. {
  6. $this->reservationId = array();
  7. (...)
  8. }
  9. ?>
revyag
Pokaż kod klasy z funkcjami o których mówisz i przykład jak to wykorzystujesz/testujesz.
Puciek
U mnie dziala, skleilem cos takiego na szybko
  1. <?php
  2. class whatever
  3. {
  4. public $reservationId;
  5.  
  6. public function __construct()
  7. {
  8. $this->reservationId = array();
  9. }
  10.  
  11. public function reserve()
  12. {
  13. $this->reservationId[] = 15;
  14. }
  15.  
  16. public function delete()
  17. {
  18. echo( count($this->reservationId) );
  19. }
  20. }
  21.  
  22. $w = new whatever;
  23. $w->reserve();
  24. $w->delete();
  25. ?>
Moze zapomniales dodac () przy wywolywaniu funkcji ?
alanoe
Dziekuje za pomoc.
Jednak ja nadal nie wiem co stalo sie z moim kodem

Klasa wyglada mniej wiecej tak:

  1. <?php
  2. class Reservation
  3. {
  4. public $msg;
  5. public $messages;
  6. public $reservationId;
  7.  
  8.  
  9. public function __construct()
  10. {
  11. $this->msg = NULL;
  12. $this->messages = array();
  13. $this->reservationId = array();
  14. }
  15.  
  16.  
  17. public function reserve()
  18. {
  19.  
  20. $items = count($this->ObjSelect);
  21. $i = 0;
  22.  
  23. while ($i < $items)
  24. {
  25.  
  26. if ($k)
  27. {
  28. ...
  29. }
  30.  
  31. $this->messages[] = $this->msg;
  32. $this->reservationId[] = $id; // zwraca tablice wszystkich id
  33. $i++;
  34. }
  35. return $this->messages;
  36. }
  37.  
  38. public function delete()
  39. {
  40. $items = count($this->reservationId);
  41. print_r($items); // wyswietla 0 :(
  42.  
  43. $i = 0;
  44. while ($i < $items)
  45. {
  46. mysql_query ("DELETE FROM `reservation` WHERE `Reservation_id` = '".$this->reservationId[$i]."' ");
  47. $i++;
  48. }
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61. ?>




A wywolanie funkcji tak:
  1. <?php
  2. if(isset($_POST['delete']))
  3. {
  4. $oReservation->delete();
  5. }
  6. ?>



Jednak mam 3 formularze wysylane po kolei do jednej klasy.
Czy to moze byc przyczyna ze zeruja mi sie wartosci zmiennych?
Puciek
A wywolales funkcje reserve ?
Daj caly kod bo w zgaduj zgadule bawic mi sie nie chce
alanoe
Dziekuje za probe pomocy smile.gif
Ponizej wklejam kod klasy i pliku z wywolaniami funkcji:

Ale juz chyba wiem co jest przyczyna...
Wywoluje 2 razy formularz i pewnie przy drugim wywolaniu ponawia sie wywolanie new Reservation i zeruje wszystkie wartosci.

Jak mozna to zrobic inaczej?



klasa:

  1. <?php
  2.  
  3. class Reservation
  4. {
  5.  
  6. public $bReserved;
  7. public $iObjectId;
  8. public $iUserId;
  9. public $StartDate;
  10. public $StartDatetime;
  11. public $iStartHour;
  12. public $iStartMinute;
  13. public $EndDate;
  14. public $EndDatetime;
  15. public $iEndHour;
  16. public $iEndMinute;
  17. public $today;
  18. public $year;
  19. public $month;
  20. public $day;
  21. public $hour;
  22. public $minutes;
  23. public $aResult;
  24. public $ObjSelect;
  25. public $aSelected;
  26. public $allReservations;
  27. public $allReservationsId;
  28. public $msg;
  29. public $messages;
  30. public $reservationId;
  31. public $insert;
  32.  
  33. public function __construct()
  34. {
  35. $this->bReserved = false;
  36. $this->iObjectId = ($_POST['Object_id']);
  37. $this->iUserId = ($_POST['login']);
  38. $this->StartDate = ($_POST['start_calendar']);
  39. $this->EndDate = ($_POST['end_calendar']);
  40. $this->today = getdate();
  41. $this->year = 2000;
  42. $this->aResult = array();
  43. $this->ObjSelect = ($_POST['objSelect']);
  44. $this->aSelected = array();
  45. $this->StartDatetime = '';
  46. $this->iStartHour = ($_POST['startHour']);
  47. $this->iStartMinute = ($_POST['startMinute']);
  48. $this->EndDatetime = '';
  49. $this->iEndHour = ($_POST['endHour']);
  50. $this->iEndMinute = ($_POST['endMinute']);
  51. $this->allReservations = mysql_query("SELECT * FROM `reservation`");
  52. $this->allReservationsId = array();
  53. $this->msg = NULL;
  54. $this->messages = array();
  55. $this->reservationId = array();
  56. $this->insert = ($_POST['confirm']);
  57. }
  58.  
  59. public function getYear()
  60. {
  61. $this->year = $this->today[year];
  62. return $this->year;
  63. }
  64.  
  65. public function getMonth()
  66. {
  67. $this->month = $this->today[month];
  68. return $this->month;
  69. }
  70.  
  71. public function getDay()
  72. {
  73. $this->day = $this->today[mday];
  74. return $this->day;
  75. }
  76.  
  77. public function getHour()
  78. {
  79. $this->hour = $this->today[hours];
  80. return $this->hour;
  81. }
  82.  
  83. public function getMinutes()
  84. {
  85. $this->minutes = $this->today[minutes];
  86. return $this->minutes;
  87. }
  88.  
  89.  
  90. public function setUser($login)
  91. {
  92. $userLogin = $login;
  93. $query = mysql_query("SELECT `User_login_id` FROM `user_login` WHERE `Login` = '$login'");
  94. $zm = mysql_fetch_array($query);
  95. $this->iUserId = $zm['User_login_id'];
  96. return $this->iUserId;
  97. }
  98.  
  99. public function reserve()
  100. {
  101. $StartTime = $this->iStartHour.':'.$this->iStartMinute.':00';
  102. $withoutSpaces = str_replace(' ', '', $StartTime);
  103.  
  104. $this->StartDatetime = $this->StartDate." ".$withoutSpaces;
  105.  
  106. $EndTime = $this->iEndHour.':'.$this->iEndMinute.':00';
  107. $withoutSpaces2 = str_replace(' ', '', $EndTime);
  108.  
  109. $this->EndDatetime = $this->EndDate." ".$withoutSpaces2;
  110.  
  111.  
  112.  
  113. $items = count($this->ObjSelect);
  114. $i = 0;
  115. $id = 0;
  116.  
  117.  
  118. while ($i < $items)
  119. {
  120. $ifResObjExist = mysql_query("SELECT * FROM `reservation` WHERE `Object_id`='".$this->ObjSelect[$i]."' ");
  121. $catch = mysql_fetch_array($ifResObjExist);
  122.  
  123. $objName = mysql_query("SELECT `Name` FROM object WHERE `Object_id`='".$this->ObjSelect[$i]."'");
  124. $catchName = mysql_fetch_array($objName);
  125.  
  126. // *** CHECK IF THE OBJECT IS NOT RESERVED 
  127. $k = 1;
  128. if ($catch)
  129. {
  130. $start = $catch['Start_time'];
  131. $end = $catch['End_time'];
  132.  
  133. if ($this->StartDatetime > $start AND $this->StartDatetime < $end)
  134. {
  135. $ObjNotToReserve = $this->ObjSelect[$i];
  136. $this->msg = 'You can not reserve object '.$catchName['Name'];
  137. $k = 0;
  138. }
  139.  
  140. if ($this->EndDatetime > $start AND $this->EndDatetime < $end)
  141. {
  142. $ObjNotToReserve = $this->ObjSelect[$i];
  143. $this->msg = '<BR><b>You can not reserve object '.$catchName['Name'].'<br>';
  144. $k = 0;
  145. }
  146.  
  147. }
  148.  
  149. if ($k)
  150. {
  151. mysql_query( "INSERT INTO `reservation` (`Object_id`, `User_login_id`, `Start_time`, `End_time` ) 
  152. VALUES ( '".$this->ObjSelect[$i]."','".$this->iUserId."', '".$this->StartDatetime."', '".$this->EndDatetime."') ");
  153. $id = mysql_insert_id();
  154. $this->msg = '<br><br>You have reserved object '.$catchName['Name'].
  155. '<ul type="square"><li>from '.$this->StartDatetime.'<li>to '.$this->EndDatetime.'</ul>';
  156. }
  157.  
  158. $this->messages[] = $this->msg;
  159. $this->reservationId[] = $id;
  160. $i++;
  161. }
  162. return $this->messages;
  163.  
  164. }
  165.  
  166. public function delete()
  167. {
  168.  
  169. $items = count($this->reservationId);
  170.  
  171. // print_r($this->reservationId);
  172. // exit;
  173.  
  174. $i = 0;
  175. while ($i < $items)
  176. {
  177. mysql_query ("DELETE FROM `reservation` WHERE `Reservation_id` = '".$this->reservationId[$i]."' ");
  178. $i++;
  179. }
  180.  
  181. $this->msg = 'You did not reserve any item';
  182. return $this->msg;
  183. }
  184.  
  185.  
  186.  
  187. public function getAllReservationsId()
  188. {
  189. while( $aRow = mysql_fetch_array($this->allReservations) )
  190. {
  191. $this->allReservationsId[] = $aRow;
  192. }
  193. return $this->allReservationsId;
  194. }
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201.  
  202. ?>




HTML
Plik z wywolaniem funkcji i formularzami:

include('tajne/config.php');

include('User.class.php');
include('Office.class.php');
include('Object.class.php');
include('Reservation.class.php');
include('callendar.php');

$userLogin = new User();
$bIfLogin = $userLogin->login();

$oOfficeD = new Office();
$AllOffices = $oOfficeD->getAllOffices();

$iObject = new Object();
$allObjects = $iObject->getAllObjectTypes();
$aGetObject = $iObject->getObject();
$id = $_POST['Object_type_id'];
$sObjType = $iObject->getTypeName($id);
$sOfficeName = $iObject->getOffice();

$oReservation = new Reservation();
$oReservation->setUser($bIfLogin);

$msg = '';
$i = 0;

if(isset($_POST['reserveObj']))
{
$msg = $oReservation->reserve();
$numberOf = count($msg);
}

if(isset($_POST['delete']))
{
$oReservation->delete();
}

?>

<div id="Table_01">
<div id="index-01">
<br><a href="http://www.zapatec.com/website/main/products/prod1/"></a><br>
</div>

<div id="index-reservation">
<p>
<?php

if( $bIfLogin )
{

?>
<?php


if( !isset($_POST['selectingObj']) AND !isset($_POST['reserveObj']))
{
?>
</div>
<div id="index-form-reserve_object">

<form action="reservation.php" method="post" name="selectObject">
<input type="hidden" name="selectingObj" value="k">
<select name="type_of_object" class="option">
<option value = "" >
-- Choose type --
</option>
<?php
foreach ($allObjects as $key2 => $allObjects)
{
?><br>
<option value = "<?php echo $allObjects['Object_type_id'] ;?>" >
<?php echo $allObjects['Type_of_object'] ;?>
</option>
<?php
} ?>
</select>
<br>
<br>
<select name="office" class="option">
<option value = "" >
-- Choose office --
</option>
<?php
foreach ($AllOffices as $key => $AllOffices)
{
?> <br>
<option value = "<?php echo $AllOffices['Office_id'] ;?>" >
<?php echo $AllOffices['City'] ;?>, <?php echo $AllOffices['Street'] ;?>
</option>
<?php
} ?>
</select>
<br>
<br>
<br>

<input name="sub" type="submit" value="Submit" class="submit"/>
</form>

<?php

}
if( isset($_POST['selectingObj']) AND !isset($_POST['reserveObj']))
{
?>
<form action="" method="post" name="reserveObject">
<input type="hidden" name="reserveObj" value="k">
<select name="objSelect[]" class="mul_sel" multiple="multiple">
<?php
foreach ($aGetObject as $key3 => $aGetObject)
{
?>
<br>
<option value = "<?php echo $aGetObject['Object_id'] ;?>" >
<?php echo $aGetObject['Name'] ?>   <?php echo $aGetObject['Room'] ?>
<?php
} ?>
</select>
<div id="office_obj">
<br>

OFFICE:<br>
OBJECT TYPE:

</div>
<div id="office_obj_display">
<?php

?
>
<br>
<?php

?
>
</div>
<br>
<br>

<div id="start_end_field">
Start date:
<p style="margin-top:80">
End date:
</p>
</div>
<noscript>
<br/>
your browser does not support Javascript.
<br/>
Either enable Javascript in your Browser or upgrade to a newer version.
</noscript>
<br><a href="http://www.zapatec.com/website/main/products/prod1/"></a><br>

</div>
<div id="reservation-hours">

<input type="text" id="start_calendar" name="start_calendar" class="field"/>
 
<button id="trigger" class="submit_small"> callendar</button>
<script type="text/javascript">
Zapatec.Calendar.setup({
firstDay : 1,
range : [2007.01, 2999.12],
electric : false,
inputField : "start_calendar",
button : "trigger",
ifFormat : "%Y-%m-%d",
daFormat : "%Y-%m-%d"
});
//]]></script>
<br>
<br>
<!-- *** Reservation starts at:-->
<select name="startHour" class="option_short">
<?php
for ($i=0; $i<24; $i++)
{
switch ($i)
{
case 0: $ii = "00"; break;
case 1: $ii = "01"; break;
case 2: $ii = "02"; break;
case 3: $ii = "03"; break;
case 4: $ii = "04"; break;
case 5: $ii = "05"; break;
case 6: $ii = "06"; break;
case 7: $ii = "07"; break;
case 8: $ii = "08"; break;
case 9: $ii = "09"; break;
default: $ii = $i; break;
}
?>
<option value = " <?php echo $ii; ?> " > <?php echo $ii ?>
</option>
<?php
}
?>
</select>
<select name="startMinute" class="option_short">
<?php
$i = 0;
while ($i<60)
{

switch ($i)
{
case 0: $ii = "00"; break;
case 5: $ii = "05"; break;
default: $ii = $i; break;
}
?>
<option value = " <?php echo $ii; ?> " > <?php echo $ii ?>
</option>
<?php
$i = $i + 5;
}
?>
</select>
<br>
<br>
<br>
<br>
<!-- *** Reservation ends at:-->

<input type="text" id="end_calendar" name="end_calendar" class="field"/>
 
<button id="trigger2" class="submit_small">callendar</button>
<script type="text/javascript">//<![CDATA[
Zapatec.Calendar.setup({
firstDay : 1,
range : [2007.01, 2999.12],
electric : false,
inputField : "end_calendar",
button : "trigger2",
ifFormat : "%Y-%m-%d",
daFormat : "%Y-%m-%d"
});
//]]></script>
<br>
<br>

<select name="endHour" class="option_short">
<?php
for ($i=0; $i<24; $i++)
{
switch ($i)
{
case 0: $ii = "00"; break;
case 1: $ii = "01"; break;
case 2: $ii = "02"; break;
case 3: $ii = "03"; break;
case 4: $ii = "04"; break;
case 5: $ii = "05"; break;
case 6: $ii = "06"; break;
case 7: $ii = "07"; break;
case 8: $ii = "08"; break;
case 9: $ii = "09"; break;
default: $ii = $i; break;
}
?>

<option value = " <?php echo $i ?> " > <?php echo $ii ?>
</option>
<?php
}
?>
</select>


<select name="endMinute" class="option_short">
<?php
$i = 0;
while ($i<60)
{

switch ($i)
{
case 0: $ii = "00"; break;
case 5: $ii = "05"; break;
default: $ii = $i; break;
}
?>
<option value = " <?php echo $ii ?> " > <?php echo $ii ?>
</option>
<?php
$i = $i + 5;
}
?>
</select>
<br>
<br>
<br>
<input name="sub" type="submit" value="Submit" class="submit"/>
</form>



<?php
}
else if(isset($_POST['reserveObj']))
{

for ($i = 0; $i < $numberOf; $i++)
echo $msg[$i];
?>
<br><br>
<form action="" method="post" name="deleteObject">
<input type="hidden" name="confirm" value="k">
<input name="confirm" type="submit" value="confirm" class="submit"/>
</form>

<form action="" method="post" name="reserveObject">
<input type="hidden" name="delete" value="k">
<input name="delete" type="submit" value="undo" class="submit"/>
</form>


<?php
}
else if (isset($_POST['delete']))
{
echo $msg;
}
?>
</div>
<?php
}
else if( !$bIfLogin )
{
?>
<div id="index-login">
You are not Logged in!
Please go to
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.