Forum FAQForum FAQSearchSearch MemberlistMemberlist Forum ignore listForum ignore list RegisterRegister ProfileProfile Log in to check your private messagesLog in to check your private messages Log inLog in
php forme

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side
View previous topic :: View next topic  
Author Message
paor



Joined: 08 Aug 2008
Posts: 23

PostPosted: 07.06.2009 15:55    Post subject: php forme Add user to your forum ignore list Reply with quote

mali telefonski imenik
1. kako rješiti ako netko želi pretragu sa praznim inputtekst poljem (bez specifičnog prezimena)

2. koji option value staviti za Sve ulice da bi pretraga vraćala rezultate svih ulica, pokušao sam sa *, pa ne ide

Molim savjet ili barem link na neke php-mysql tutorijale

Hvala

dio koda

Code:
<form action="proba.php" method="post">

 prezime
<input type="text" name="prezime" size="15"   />
    <br>
 ulica
<select name="ulica">
<option value="?">Sve ulice</option>
<option value="M.Gupca">M.Gupca</option>
<option value="Berava">Berava</option>
<option value="Kladavac">Kladavac</option>
</select>
   <br>
 <input type="submit" />
</form>

Code:

$prezime=$_POST['prezime'];
  $ulica=$_POST['ulica']; 

$query = sprintf("SELECT prezime , ime , ulica , broj , telefon FROM telimenik WHERE prezime='$prezime' AND ulica='$ulica' LIMIT 0,30",
Back to top
View user's profile Send private message Visit poster's website
zytzagoo
mi3.crew


Joined: 25 Aug 2003
Posts: 1842
Location: Zagreb, Hrvatska

PostPosted: 07.06.2009 19:00    Post subject: Re: php forme Add user to your forum ignore list Reply with quote

paor wrote:
mali telefonski imenik
1. kako rješiti ako netko želi pretragu sa praznim inputtekst poljem (bez specifičnog prezimena)

Najbolje tako da izbacis prezime iz WHERE uvjeta u tom slucaju.
Detektiras u php-u ako je polje prazno, i ne stavis to polje u sql upit.
Quote:
2. koji option value staviti za Sve ulice da bi pretraga vraćala rezultate svih ulica, pokušao sam sa *, pa ne ide

Mozes probati staviti '%' znak, medjutim puno bolje bi bilo da stavis npr. 'sve', i to onda
ulovis s php-om, pa ako je odabrana ta opcija, polje ulica izbacis iz where uvjeta.

_________________
[+]I[+]am[+]my[+]own[+]religion[+]
Back to top
View user's profile Send private message Visit poster's website Twitter profile
paor



Joined: 08 Aug 2008
Posts: 23

PostPosted: 08.06.2009 07:56    Post subject: Add user to your forum ignore list Reply with quote

Quote:
Detektiras u php-u ako je polje prazno, i ne stavis to polje u sql upit.


pokušavam sa sintaksom if(empty($_POST['prezime'])), ali ne mogu pogoditi ispravnu sintaksu ili položaj u datoteci

Quote:
Mozes probati staviti '%' znak,


sa tim isto ne vraća nikakav rezultat


hvala
Back to top
View user's profile Send private message Visit poster's website
Moebius



Joined: 26 Jan 2004
Posts: 245
Location: zagreb

PostPosted: 08.06.2009 08:59    Post subject: Add user to your forum ignore list Reply with quote

Quote:
pokušavam sa sintaksom if(empty($_POST['prezime'])), ali ne mogu pogoditi ispravnu sintaksu ili položaj u datoteci


Vjerujem da postoji jednostavniji nacin dohvata, ali tek sam poceo sa php-om Smile

Code:
<?php
    // prvo provjeri da li je ista doslo preko POST-a
    if (isset($_POST["prezime"])) {
        // doslo je, sad vidi da li je prazan string ili ima nesto unutra:
        if (!empty($_POST["prezime"])) $prezime = $_POST["prezime"]; // ima nesto
    } else {
    $prezime = ""; // prezime nije dohvaceno.
    }
    echo $prezime;
?>
Back to top
View user's profile Send private message Visit poster's website
zytzagoo
mi3.crew


Joined: 25 Aug 2003
Posts: 1842
Location: Zagreb, Hrvatska

PostPosted: 08.06.2009 21:57    Post subject: dosadno... Add user to your forum ignore list Reply with quote

Eo simpl forma koja nadam se bolje ilustrira ono što htjedoh reći:

Code:

<?php

$fields_to_fetch = array('prezime', 'ime', 'ulica', 'broj', 'telefon');
$where_fields = array('prezime' => 1, 'ulica' => 1);

function make_var_safe($val) {
    if (eregi("\r", $val) || eregi("\n", $val)) {
        return null;
    }
    $val = htmlspecialchars(trim($val));
    if (get_magic_quotes_gpc()) {
        $val = stripslashes($val);
    }
    return $val;
}

if (isset($_POST['submit'])) {
    // generic form submit handling
    foreach ($where_fields as $field => $dummy) {
        // just a simplistic way to make sure input is somewhat safe
        $_POST[$field] = make_var_safe($_POST[$field]);
        // if a field is empty, unset it from the array
        // so it doesn't get included in the sql generation loop
        if (empty($_POST[$field])) {
            unset($where_fields[$field]);
        }
    }
    /**
    * special case for 'ulica', if "sve" is selected
    * unset the field so it doesn't get included in the query
    */
    if (isset($_POST['ulica']) && $_POST['ulica'] === 'sve') {
        unset($where_fields['ulica']);
    }
}

// main part of the query
$sql = "SELECT " . implode(', ', $fields_to_fetch) . " FROM telimenik";

// looping over the fields and generating the sql query we need
$total_fields = count($where_fields);
$cnt = 0;
if ($total_fields) {
   $sql .= ' WHERE ';
   foreach ($where_fields as $field => $dummy) {
       $cnt++;
       if ($cnt > 1) {
          $sql .= ' AND ';
       }
       $sql .= $field . " = '" . $_POST[$field] . "'";
    }
}
$sql = trim($sql);

echo '<strong>' . $sql . '</strong>';
?>

<form action="" method="post">
<input type="hidden" name="submit" value="1" />
 prezime <input type="text" name="prezime" size="15" /><br>
 ulica
<select name="ulica">
<option value="sve">Sve ulice</option>
<option value="M.Gupca">M.Gupca</option>
<option value="Berava">Berava</option>
<option value="Kladavac">Kladavac</option>
</select>
<br>
<input type="submit" />
</form>


_________________
[+]I[+]am[+]my[+]own[+]religion[+]
Back to top
View user's profile Send private message Visit poster's website Twitter profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group