Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
PHP - zapytanie. Pomóżcie
#1
Czy ktoś mógłby mi pomóc?
Z góry dziękuję

Mam baze postgre 9x i nie działa poniższe zapytanie o typ oferty "offerHasOfferType". Przy postgre 8x działało.

Jeśli na stałe tutaj dodam tabele offerHasOfferType to działa, ale jeżeli użytkownik nie wybierz typu oferty to nie pokazuje sie nic na stronie.

from
offers, cities, countries, "cantonmentTypes", "serviceHasOffers" "+offerHasOfferType"


Jak zrobić warunek żeby nie uwzgledniac w zapytaniu sql listy tabel - tabeli offerHasOfferType w momencie jak nie jest podany w wyszukiwarce typ oferty

czyli jezeli jest typ oferty to lista tabel z których ma pobrac dane:
offers, cities, countries, "cantonmentTypes", "serviceHasOffers", "offerHasOfferType"

a jak nie ma to:
offers, cities, countries, "cantonmentTypes", "serviceHasOffers"


================


if (is_numeric($link['typ']))
{
$tabela = ', "offerHasOfferType" ';
$dodaj = $dodaj.' and "offerHasOfferType"."offerId"=offers."offerId" and "offerHasOfferType"."offerTypeId"='.$link['typ'].' ';
}



$sql = 'select offers."offerId",offers.name,offers."galleryId",offers.description,offers."pictureId", cities.name as miasto, countries.name as kraj, "cantonmentTypes".name as nocleg, (select price from termins where termins."offerId"=offers."offerId" and termins."currencyId"=1 order by termins.price asc limit 1) as cena, (select "dateFrom" from termins where termins."offerId"=offers."offerId" and termins."dateFrom">\''.$dzis.'\' order by termins."dateFrom" asc limit 1) as terminod
from
offers, cities, countries, "cantonmentTypes", "serviceHasOffers"
where
offers."isActive"=true and
cities."cityId"=offers."cityId" and
countries ."countryId"=offers."countryId" and
"cantonmentTypes"."cantonmentTypeId"=offers."cantonmentTypeId" '.$dodaj.' and '.$warunek_dod.'
order by terminod,cena asc, offers."offerId" desc limit 15 offset '.$granica.' ';


Kod PHP:
<?
$_GET
['cachewalker'] = NULL;
include (
"funkcje/cache_start.php");
include (
"funkcje/funkcje.php");
 
foreach (
$_GET as $klucz => $wartosc
    {
    if (!
checkField($_GET[$klucz])) {header ("Location: index.php"); exit;}
    }
#przygotowanie zmiennych
foreach ($_GET as $klucz => $wartosc
    {
    
$link[$klucz] = przygotoj($_GET[$klucz]);
    }
 
if (
$link['kraj'])
    {
    if (
is_numeric($link['kraj'])) $dodaj $dodaj.' and offers."countryId"='.$link['kraj'].' ';
    }
if (
$link['miejscowosc']) 
    {
    if (
is_numeric($link['miejscowosc'])) $dodaj $dodaj.' and offers."cityId"='.$link['miejscowosc'].' ';
    }
if (
$link['obiekt']) 
    {
    if (
is_numeric($link['obiekt'])) $dodaj $dodaj.' and offers."cantonmentTypeId"='.$link['obiekt'].' ';
    }
if (
$link['dojazd']) 
    {
    if (
is_numeric($link['dojazd'])) $dodaj $dodaj.' and offers."journeyTypeId"='.$link['dojazd'].' ';
    }
if (
$link['typ'])
    {
    if (
is_numeric($link['typ'])) 
        {
        
$tabela ', "offerHasOfferType" ';
        
$dodaj $dodaj.' and "offerHasOfferType"."offerId"=offers."offerId" and "offerHasOfferType"."offerTypeId"='.$link['typ'].' ';
        }
    }
 
$naglowek "Tekst przykładowy tytułu";
include (
"layout.php");
?>
<center>
<div class="wysz">
<form action="szukaj.php" method="get" style="margin:0px;padding:0px;">
<div><b>Wyszukiwarka</b></div>
<div style="margin:8px;">
<?
$warunek_dod 
' "serviceHasOffers"."serviceId"=40 and "serviceHasOffers"."offerId"=offers."offerId" ';
?>
 
<select name="kraj" onchange="getMiasto(this)">
        <option value="">- kraj -</option>
        <?
        $sql 
'select distinct(countries.name) as kraj, countries."countryId" 
                    from 
                    offers, countries, "serviceHasOffers"
                    where 
                    offers."isActive"=true and 
                    countries."countryId"=offers."countryId" and '
.$warunek_dod.' order by  countries.name asc ';
 
        
$wynik pg_query($sql);
        
$ilosc pg_num_rows($wynik);
        for (
$i=0;$i<$ilosc;$i++)
            {
            
$re pg_fetch_array($wynik);
            echo 
"<option value='".$re['countryId']."'>".$re['kraj']."</option>";
            }
        
?>
        </select> &nbsp; &nbsp; &nbsp; 
<select name="miejscowosc" id="miejscowosc" style="width:200px;"><option value="">wybierz kraj</option></select> &nbsp; &nbsp; &nbsp; 
<select name="obiekt">
        <option value="">- rodzaj obiektu -</option>
        <?
        $sql 
'select distinct("cantonmentTypes"."cantonmentTypeId"), "cantonmentTypes".name as nocleg
                    from
                    offers, "cantonmentTypes", "serviceHasOffers"
                    where 
                    offers."isActive"=true and 
                    "cantonmentTypes"."cantonmentTypeId"=offers."cantonmentTypeId" and '
.$warunek_dod.' order by "cantonmentTypes".name asc';
 
        
$wynik pg_query($sql);
        
$ilosc pg_num_rows($wynik);
        for (
$i=0;$i<$ilosc;$i++)
            {
            
$re pg_fetch_array($wynik);
            echo 
"<option value='".$re['cantonmentTypeId']."'>".$re['nocleg']."</option>";
            }
        
?>
        </select><br /><br />
<select name="dojazd">
        <option value="">- dojazd -</option>
        <option value='2'>Bus</option>
        <option value='3'>Dojazd własny</option>
        <option value='4'>Pociąg</option>
        <option value='1'>Lot</option>
        </select>  &nbsp; &nbsp; &nbsp; 
<select name="typ">
        <option value="">- typ oferty -</option>
        <?
        $sql 
'select distinct("offerTypes"."offerTypeId"), "offerTypes".name as typ
                    from
                    offers, "offerTypes", "serviceHasOffers" 
                    where 
                    offers."isActive"=true  and '
.$warunek_dod.' order by "offerTypes".name asc';
 
        
$wynik pg_query($sql);
        
$ilosc pg_num_rows($wynik);
        for (
$i=0;$i<$ilosc;$i++)
            {
            
$re pg_fetch_array($wynik);
            echo 
"<option value='".$re['offerTypeId']."'>".$re['typ']."</option>";
            }
        
?>
        </select> 
</div>
<div style="margin-top:12px;text-align:right;">
<input type="submit" name="go" value="szukaj">
</div>
</form>
</div>
<?
if ($page$granica $page 30;
if (!
$page) {$granica=0;$page=0;}
 
$sql 'select count(offers."offerId") 
        from 
        offers, cities, countries, "cantonmentTypes"      
        where 
        offers."isActive"=true and 
        cities."cityId"=offers."cityId" and 
        countries ."countryId"=offers."countryId" and 
        "cantonmentTypes"."cantonmentTypeId"=offers."cantonmentTypeId" '
.$dodaj.'  and  '.$warunek_dod.'  ';
$wynik pg_query($sql);
$ilosc_calk pg_fetch_array($wynik);
 
$strony $ilosc_calk[0]/30;
$strony ceil($strony); 
$dzis date("Y-m-d");
 
 
$sql 'select offers."offerId",offers.name,offers."galleryId",offers.description,offers."pictureId", cities.name as miasto, countries.name as kraj, "cantonmentTypes".name as nocleg, (select price from termins where termins."offerId"=offers."offerId" and termins."currencyId"=1 order by termins.price asc limit 1) as cena, (select "dateFrom" from termins where termins."offerId"=offers."offerId" and termins."dateFrom">\''.$dzis.'\' order by termins."dateFrom" asc limit 1) as terminod 
from
offers, cities, countries, "cantonmentTypes", "serviceHasOffers" 
where 
offers."isActive"=true and 
cities."cityId"=offers."cityId" and 
countries ."countryId"=offers."countryId" and 
"cantonmentTypes"."cantonmentTypeId"=offers."cantonmentTypeId" '
.$dodaj.'  and  '.$warunek_dod.
order by terminod,cena asc, offers."offerId" desc limit 15 offset '
.$granica.' ';
 
 
$wynik pg_query($sql);
$ilosc pg_num_rows($wynik);
 
if (
$ilosc==0) echo "Nie znaleziono ofert spełniających Twoje kryteria.";
 
for (
$i=0;$i<$ilosc;$i++)
    {
    
$re pg_fetch_array($wynik);
 
    
$temp $i/2;
    if (
is_int($temp)) $back "#ffffff";
    else 
$back "#f3f9fe";
 
    
$url tworz_url($re['name'], $re['offerId']);
    if (!
$re['terminod']) 
        {
        
$sql 'select "dateFrom" from termins where "offerId"='.$re['offerId'].' and "isActive"=true order by "dateFrom" desc limit 1';
        
$dat pg_query($sql);
        
$datt pg_fetch_array($dat);
        if (!
$datt['dateFrom']) $re['terminod'] = "-";
        else 
$re['terminod'] = $datt['dateFrom'];
        }
 
 
 
    echo 
"<div class='oferta1'>
          <div class='oferta2'>"
;
    echo 
"<table style='width:100%;' cellspacing='2' cellpadding='2'>";
    echo 
"<tr>
            <td width='110' valign='top'><a href='"
.$url.".html' class='none'>";
    if (
is_file("../img/gallery/".$re['galleryId']."/small".$re['pictureId'].".jpg")) echo "<img src='/gallery/".$re['galleryId']."/small".$re['pictureId'].".jpg' alt='".$re['miasto']."' border='0'  width='120' height='90'>";
    else echo 
"<img src='gfx/brak.png' alt=' ' border='0' />";
    echo 
"</a></td>
            <td valign='top'>
                <div style='line-height:19px;'>
                <a href='"
.$url.".html' class='none'><b style='color:#b0d224;font-size:13px;'>".$re['name']."</b></a><br>
                <div class='oferta3'>
                Lokalizacja: <b>"
.$re['kraj']."</b> -> <b>".$re['miasto']."</b><br>
                Obiekt: <b>"
.$re['nocleg']."</b>,<br>";
    if (
$re['cena']>=1) echo "Cena od: <b>".$re['cena']."</b> PLN, <br>";
    else
        {
        
$sql 'select price from termins where "offerId"='.$re['offerId'].' and "currencyId"=3 order by price asc limit 1';
        
$odp pg_query($sql);
        
$cena_eur pg_fetch_array($odp);
        if (
$cena_eur['price']) echo "Cena od: <b>".$cena_eur['price']."</b> EUR, <br>";
        else echo 
"Cena od: <b>-</b> <br>";
        }
    echo 
"Termin od: <b>".$re['terminod']."</b>";
    echo 
"</div>
            <div style='text-align:right;'><a href='"
.$url.".html' style='color:#fe6e00;text-decoration:none;'>[zobacz]</a></div>
            </td>
            </tr>
            </table>"
;
    echo 
"</div>";
        
$opis str_replace("<br />"" "$re['description']);
        
$opis str_replace("<br>"" "$opis);
        
$opis strip_tags($opis);
        
$opis substr($opis0370);
        
$spacja explode(" "$opis);
        
$max sizeof($spacja);
        
$opis str_replace($spacja[$max-1], ""$opis);
    echo 
"<div class='ofertaopis'>
            "
.$opis."...
          </div><br style='clear:both;' /></div>"
;
    }
 
 
 
if (
$strony>1)
    {
    echo 
"|"
 
    
$pp 0;
    
$start 0;
    
$pp $page/23;
    
$pp floor($pp);
    
$start $page 11;
    if (
$start<0$start 0;
    if (
$page<=9$plus 11 $page;
    else 
$plus 0;
 
    
$temp $page 11 $plus;
    if (
$temp>$strony$end $strony;
    else 
$end $temp;
 
    for (
$i=$start;$i<$end;$i++)
            {
            
$j $i+1;
            if (
$page==$i) echo " <b>[".$j."]</b> |";
            else echo 
" <a href='szukaj.php?kraj=".$link['kraj']."&miejscowosc=".$link['miejscowosc']."&obiekt=".$link['obiekt']."&dojazd=".$link['dojazd']."&typ=".$link['typ']."&go=szukaj&page=".$i."' class='pages'>".$j."</a> |";
             }
     }
?>
</center>
<?
include ("layout_dol.php");
include (
"funkcje/cache_end.php");
?>
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  Zapytanie pod warunkiem 1 nazwy Intelactive 13 9,138 12-12-2012, 00:19
Ostatni post: Intelactive
  SQL zapytanie o group Radian 5 4,044 01-06-2012, 05:36
Ostatni post: Marys
Question Prepared statements - czy zapytanie jest wystarczająco bezpieczne atrapa 0 1,526 15-11-2011, 23:23
Ostatni post: atrapa
  Nieprawidłowe zapytanie do bazy MYSQL kraskaj 0 1,975 03-08-2011, 17:36
Ostatni post: kraskaj
  [SQL] zapytanie VirusX 1 1,846 10-11-2010, 23:13
Ostatni post: Pedro84

Skocz do:


Użytkownicy przeglądający ten wątek:
Sponsorzy i przyjaciele
SeoHost.pl