Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Poziv funkcije unutar hyperlinka

[es] :: PHP :: PHP za početnike :: Poziv funkcije unutar hyperlinka

[ Pregleda: 1142 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Kondenzator
Studiram
BiH

Član broj: 273610
Poruke: 22
31.176.204.*

Sajt: www.rodjendanskecestitke...


Profil

icon Poziv funkcije unutar hyperlinka21.01.2013. u 15:59 - pre 136 meseci
Pozdrav,

Radim na jednom malom projektu i došao sam do jednog dijela gdje sam "zaribao" i ne mogu nikako da rijesim ovo. Mozda imam pogrešan pristup, ne znam.. Nisam pametan :)

list.php
Code:

<?php
Class User {
    public $id;
   
    public $first_name;
    public $last_name;
    public $comments;
    
    
 public function __construct($Id,$name, $secondName, $comment){
        $this->id=$Id;
        $this->first_name=$name;
        $this->last_name=$secondName;
        $this->comments=$comment;
    
    }
    
    public function getID(){
        return  $this->id;
    }
    
     public function getFirstName(){
        return $this->first_name;
    }
    
     public function getLastName(){
        return $this->last_name;
    }
    
     public function getComment(){
        return $this->comments;
    }

public function save(){
        
    }
     
    
    public static function getAllUsers(){
        $upit="SELECT * FROM user";
        if($querry_run=mysql_query($upit))
        {
            while($querry_row=mysql_fetch_assoc($querry_run))
            {
                
                $niz[]= new User($querry_row['id'],$querry_row['FirstName'],$querry_row['LastName'],$querry_row['Comment']);
                
            }
            return $niz;
        }
        else
        {
            echo 'Ne moRe';
        }
    }
    
    public function if_delete(){        ->>>>>>> OVDJE JE POTENCIJALNO PROBLEM
        if($_GET['run_func'] == yes) {
        delete();
        }
        
    }
    public function delete(){      ->>>>>>> OVDJE JE POTENCIJALNO PROBLEM
       if (isset($_GET['id']) && is_numeric($_GET['id']))
       {
        $id = $_GET['id'];
        $result = mysql_query("DELETE FROM user WHERE id=$id")
        or die(mysql_error());
       }
    }


}



a sada nastavak list.php gdje ce biti hyperlnik preko kojeg zelim da pozovem metod delete()
Code:

require 'db.php';


$Nestoo =new User(0,'admin','admin','neki admin');

$harko=$Nestoo->getAllUsers();
$max = sizeof($harko);
$result = mysql_query("SELECT * FROM user") 
                or die(mysql_error());  




echo "<table border='1'>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Comment</th>
</tr>";

//
//while($row = mysql_fetch_array( $result )){
  for($i=0;$i<$max;$i++){ 
    echo "<tr>";
    echo "<td>" . $harko[$i]->getID(). "</td>";
    echo "<td>" . $harko[$i]->getFirstName() . "</td>";
    echo "<td>" . $harko[$i]->getLastName() . "</td>";
    echo "<td>" . $harko[$i]->getComment() . "</td>";
    echo "<td>" . '<form action="update-user.php" method="POST" >
                   <input name="custom" type="hidden" value="$harko[$i]->getID()" />
                   <input type="submit" value="Update">
                   </form>'
                   . "</td>";
    echo "<td>" . '<a href="delete.php?id=' . $harko[$i]->getID() . '">Link to First</a>' . "</td>";   ->>> OVDJE JE POTENCIJALNO PROBLEM

    echo "</tr>";

   }
// zatvaram tabelu
echo "</table>";

?>


Objašnjenje problema:

U list.php imam ispisanu tabelu koja pored ID,FIRSTNAME,LASTNAME,COMMENT ima polje DELETE za brisanje korisnika.

Zanima me kako da implementiram da preko linka:
'<a href="delete.php?id=' . $harko[$i]->getID() . '">Link to First</a>' . "</td>"; ->>> OVDJE JE POTENCIJALNO PROBLEMio

pozovem metod delete() i da obrišem user-a iz tabele. Da li je pristup pogrešan?

Napomena: Mora mi sve biti u ovom fajlu dakle nemam dozvolu da pravim dodatni fajl npr. delete.php.

Nadam se da ste skontali da nisam bio previse konfuzan...
 
Odgovor na temu

kelja

Član broj: 70429
Poruke: 1416
*.dynamic.isp.telekom.rs.



+35 Profil

icon Re: Poziv funkcije unutar hyperlinka21.01.2013. u 18:29 - pre 136 meseci
Code:
<?php
Class User {
    public $id;
   
    public $first_name;
    public $last_name;
    public $comments;
    
    
 public function __construct($Id,$name, $secondName, $comment){
        $this->id=$Id;
        $this->first_name=$name;
        $this->last_name=$secondName;
        $this->comments=$comment;
    
    }
    
    public function getID(){
        return  $this->id;
    }
    
     public function getFirstName(){
        return $this->first_name;
    }
    
     public function getLastName(){
        return $this->last_name;
    }
    
     public function getComment(){
        return $this->comments;
    }

public function save(){
        
    }
     
    
    public static function getAllUsers(){
        $upit="SELECT * FROM user";
        if($querry_run=mysql_query($upit))
        {
            while($querry_row=mysql_fetch_assoc($querry_run))
            {
                
                $niz[]= new User($querry_row['id'],$querry_row['FirstName'],$querry_row['LastName'],$querry_row['Comment']);
                
            }
            return $niz;
        }
        else
        {
            echo 'Ne moRe';
        }
    }
    
  
    public function delete(){     
       if (isset($_GET['id']) && is_numeric($_GET['id']))
       {
        $id = $_GET['id'];
        $result = mysql_query("DELETE FROM user WHERE id=$id")
        or die(mysql_error());
       }
    }


}
require 'db.php';


$Nestoo =new User(0,'admin','admin','neki admin');

$harko=$Nestoo->getAllUsers();
$max = sizeof($harko);
$result = mysql_query("SELECT * FROM user") 
                or die(mysql_error());  




echo "<table border='1'>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Comment</th>
</tr>";

//
//while($row = mysql_fetch_array( $result )){
  for($i=0;$i<$max;$i++){ 
    echo "<tr>";
    echo "<td>" . $harko[$i]->getID(). "</td>";
    echo "<td>" . $harko[$i]->getFirstName() . "</td>";
    echo "<td>" . $harko[$i]->getLastName() . "</td>";
    echo "<td>" . $harko[$i]->getComment() . "</td>";
    echo "<td>" . '<form action="update-user.php" method="POST" >
                   <input name="custom" type="hidden" value="$harko[$i]->getID()" />
                   <input type="submit" value="Update">
                   </form>'
                   . "</td>";
    echo "<td>" . '<a href="list.php?id=' . $harko[$i]->getID() . '&del=1">Delete User</a>' . "</td>"; 

    echo "</tr>";

   }
// zatvaram tabelu
echo "</table>";

//
if($_GET['del']=='1') {

$user= new User;
$user->delete();
header('location:list.php');    
}

?>


moze ovako?
 
Odgovor na temu

Kondenzator
Studiram
BiH

Član broj: 273610
Poruke: 22
31.176.196.*

Sajt: www.rodjendanskecestitke...


Profil

icon Re: Poziv funkcije unutar hyperlinka21.01.2013. u 19:52 - pre 136 meseci
@kelja: Moze, hvala ti! Svaka ti dala :). Zivio!
 
Odgovor na temu

[es] :: PHP :: PHP za početnike :: Poziv funkcije unutar hyperlinka

[ Pregleda: 1142 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.