Forum Moderators: coopster

Message Too Old, No Replies

mysql fetch object and add if

         

helenp

9:50 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, I have a database where I insert bookings, all days that are booked are marked with an N in the database. And I have a calculator script that checks availability and calculates the price, this script works perfect.
The problem is I would like to add to this calculator script that if any property is marked with an L instead of an N, that it is only available for long term rentals,
I hope I explained myself.
This is the part of the code where I need to insert and I think I need to change to mysql_fetch_object:

$result = mysql_query ("SELECT DATE_FORMAT(llegada, '%e %b %Y') as lleg, DATE_FORMAT(salida, '%e %b %Y') as sal, propiedad, TO_DAYS('$salida') - TO_DAYS('$llegada') as dias from bookings where ( propiedad = '$propiedad' )
AND (('$llegada' BETWEEN llegada AND date_sub(salida, interval +1 day))
or ('$salida' BETWEEN date_sub(llegada, interval -1 day) AND salida) or (llegada <= '$llegada' AND salida >= '$salida') or (llegada >= '$llegada' AND salida <= '$salida'))", $dbh);
if ($row = mysql_fetch_array($result)){
do {
$propiedad = str_replace("_", " ", $propiedad);
echo "<span class="red\">$propiedad is occupied from ".$row["lleg"]." until ".$row["sal"]."</span><br>";

} while ($row = mysql_fetch_array($result));
$propiedad = str_replace("_", " ", $propiedad);

$availability="<span class="red\">No, $propiedad is not available from $arrival_display to $departure_display.</span>";
echo "</table> \n";
}
else { etc.

And this is what I want to do but it doesnīt work and donīt give any errors, can anyone see anything wrong?
$result = mysql_query ("SELECT DATE_FORMAT(llegada, '%e %b %Y') as lleg, DATE_FORMAT(salida, '%e %b %Y') as sal, propiedad, TO_DAYS('$salida') - TO_DAYS('$llegada') as dias from bookings where ( propiedad = '$propiedad' )
AND (('$llegada' BETWEEN llegada AND date_sub(salida, interval +1 day))
or ('$salida' BETWEEN date_sub(llegada, interval -1 day) AND salida) or (llegada <= '$llegada' AND salida >= '$salida') or (llegada >= '$llegada' AND salida <= '$salida'))", $dbh);
if ($row = mysql_fetch_object($result)){
if ($row->lleg=="L"){ echo "This property is available only for long term rentals"; }
elseif ($row->lleg=="N") {
$propiedad = str_replace("_", " ", $propiedad);
echo "<span class="red\">$propiedad is occupied from ".$row->lleg." until ".$row->sal."</span><br>";
$availability="<span class="red\">No, $propiedad is not available from $arrival_display to $departure_display.</span>";
echo "</table> \n";
}
}

else { etc.

Habtom

10:06 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ($row = mysql_fetch_object($result)){
...

Shouldn't you be looping that?

while ($row = mysql_fetch_object($result)){
...

Habtom

helenp

10:12 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Habtom
I tried that, that gives me error (unexpted else) on the last else I pasted

Habtom

11:43 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that gives me error (unexpted else)

yes right, do you need the else.

Remove the else block (the whole block) and see if you can accomodate it without it or include it separately on the blocks.

helenp

11:47 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



no I can not remove the else block, I have several else blocks, in the else blocks (i.e the property is available,) there is where I calculate the price

helenp

11:51 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I eliminated all the else blocks, just to try,
and gives me nothing, no error or anything (using while instead of if)

Habtom

11:55 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is your query outputting anything, check if there are any results.

> I am not sure if this affects it, but try replacing the following block with the one following it.

if ($row = mysql_fetch_object($result)){
if ($row->lleg=="L"){ echo "This property is available only for long term rentals"; }
elseif ($row->lleg=="N") {
$propiedad = str_replace("_", " ", $propiedad);
echo "<span class="red\">$propiedad is occupied from ".$row->lleg." until ".$row->sal."</span><br>";
$availability="<span class="red\">No, $propiedad is not available from $arrival_display to $departure_display.</span>";
echo "</table> \n";
}
}

to

$row = mysql_fetch_object($result);
if ($row->lleg=="L"){ echo "This property is available only for long term rentals"; }
elseif ($row->lleg=="N") {
$propiedad = str_replace("_", " ", $propiedad);
echo "<span class="red\">$propiedad is occupied from ".$row->lleg." until ".$row->sal."</span><br>";
$availability="<span class="red\">No, $propiedad is not available from $arrival_display to $departure_display.</span>";
echo "</table> \n";
}

Habtom

helenp

12:05 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



?
After eliminating all the else I left just the new script, and I changed it to Habtoms... I get another error?
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/#*$!xx/public_html/calculator_new2.php on line 51

eliminating those lines I get no errors but no results,
the problems seems to bee, it does not read if there is any L or N in the database

helenp

12:09 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to shorten the script and find out what does not work,
I left it to just this, it should work as there is an L marked in the database, but I donīt get any result and no errors:

<?php
include("conex/conexion.php");
if ($_POST['enviar']) {
$result = mysql_query ("SELECT DATE_FORMAT(llegada, '%e %b %Y') as lleg, DATE_FORMAT(salida, '%e %b %Y') as sal, propiedad, TO_DAYS('$salida') - TO_DAYS('$llegada') as dias from bookings where ( propiedad = '$propiedad' )
AND (('$llegada' BETWEEN llegada AND date_sub(salida, interval +1 day))
or ('$salida' BETWEEN date_sub(llegada, interval -1 day) AND salida) or (llegada <= '$llegada' AND salida >= '$salida') or (llegada >= '$llegada' AND salida <= '$salida'))", $dbh);
$row = mysql_fetch_object($result);
if ($row->lleg=="L"){ echo "This property is available only for long term rentals"; }
}
?>

d40sithui

12:20 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



1) if you manually run the query in mysql, do you get any results?
2) also i recommend changing the if statement from
if ($row->lleg=="L"){
to
if(strcasecmp($row->lleg, "L")==0){
see if that will do anything

helenp

12:33 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



d40sithui
1) If I manually do the query I get result yes
2) I have tried that and the same, no result no errors
Thanks

helenp

3:27 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think I got it, the L was in another field in the database lol :)

helenp

3:53 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try to test it, now I get another error, I get unexptected else whatever I do,...

if ($row = mysql_fetch_object($result)); {
if ($row->tiporeserva=="larga"){ echo "This property is available only for long term rentals"; }
elseif($row->tiporeserva=="cliente") {
$propiedad = str_replace("_", " ", $propiedad);
echo "<span class='red'>$propiedad is occupied from ". $row->lleg ." until ". $row->sal ."</span><br>";
$availability="<span class='red'>No, $propiedad is not available from $arrival_display to $departure_display.</span>";
echo "</table> \n";
}
}
else {

d40sithui

5:13 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



well that would explain it eh haha good work.
as for this new error could it be related to you having a semicolon at the end of your if statement?

if ($row = mysql_fetch_object($result)); {

helenp

6:04 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




well that would explain it eh haha good work.
as for this new error could it be related to you having a semicolon at the end of your if statement?

Lol, yeap, that makes the difference,
seems to work now,
I just couldnīt understand why the error.