Jump to content

Récupération id dans l'url en poo pdo


ab1011
 Share

Recommended Posts

<?php

class cityManager {

	private $_db;

	public function __construct(PDO $db) {

		$this->setDb($db);
	}

	public function setDb($dbh) {

		$this->_db = $dbh;
	}
  
    public function getDisplayCity(City $city) {

    	$sql = 'SELECT city_id, city_name, city_text FROM city WHERE city_id = :city_id';

    	$city_id = $city->getCityId();
    	$city_name = htmlspecialchars($city->getCityName());
    	$city_text = htmlspecialchars($city->getCityText());

		$stmnt = $this->_db->prepare($sql);

		$stmnt->bindParam(':city_id', $city_id = $_GET['city_id']);
		$stmnt->bindParam(':city_name', $city_name);
		$stmnt->bindParam(':city_text', $city_text);

		$stmnt->execute();

		$city_data = $stmnt->fetch(PDO::FETCH_ASSOC);

		$cities_data[] = $city_data;

		return $cities_data;
    }
<?php

class City {
    
    protected $city_id;
	protected $city_name;
	protected $city_text;
	
	protected static $error;
    
    const MSG_ERROR_CITY_ID = 'CITY ID doit être un entier';
	const MSG_ERROR_CITY_NAME = 'CITY NAME doit être une chaîne de caractères';
	const MSG_ERROR_CITY_TEXT = 'CITY TEXT doit être une chaîne de caractères';
	const MSG_ERROR_OBJET = 'L\'OBJET ne peut pas être crée';

	public function __construct(array $data) {
        
        $this->setCityId($data['city_id']);
		$this->setCityName($data['city_name']);
		$this->setCityText($data['city_text']);

		if(!empty(self::$error)) {

        	throw new Exception(self::$error . self::MSG_ERROR_OBJET);
        }
	}

    public function setError($msg) {

		self::$error = $msg;
	}

	public function getError() {

		return self::$error;
	}

	public function setCityId($city_id) {

		if((is_int($city_id)) AND ($city_id > 0)) {

			$this->city_id = $city_id;
		}
		else {

			$this->setError(self::MSG_ERROR_CITY_ID);
		}
	}

	public function setCityName($city_name) {

		if(is_string($city_name)) {

			$this->city_name = $city_name;
		}
		else {

			$this->setError(self::MSG_ERROR_CITY_NAME);
		}
	}

	public function setCityText($city_text) {

		if(is_string($city_text)) {

			$this->city_text = $city_text;
		}
		else {

			$this->setError(self::MSG_ERROR_CITY_TEXT);
		}
	}

	public function getCityId() {

		return $this->city_id;
	}

	public function getCityName() {

		return $this->city_name;
	}

	public function getCityText() {

		return $this->city_text;
	}
}
<?php

require('../includes/inc-connexion.php');
require('../class/cityManager.php');

?>

<!DOCTYPE html>

<html lang="fr">
     <head>
          <meta charset="utf-8">
          <title>Accueil Administration</title>
          <link rel="stylesheet" href="css/style.css">
     </head>

     <body>
          <div id="wrapper">
              <h1>Administration du mini-site des villes</h1>
              <div id="bloc_left">
              	  <p>
                     L'administration du site vous permet d'ajouter<br>
                     une nouvelle ville au site ou de modifier ou<br>
                     supprimer une ville existante.
                  </p>
              </div>

              <div id="bloc_right">
              	  <ul>
              	  	 <li><a href="insert-city.php">Ajouter une ville</a></li>
              	  	 <li><a href="../index.php">Voir le site</a></li>
                     <li><a href="logout.php">Déconnexion</a></li>
              	  </ul>
              </div>

              <p>Liste des villes : </p>

              <?php 

              $manager = new cityManager($db);
              $cities_data = $manager->getDisplayCities();

              ?>

              <div id="menu_vertical">
                <ul>
                  <li><a href="../index.php">Accueil</a></li>
                  <?php if(!empty($cities_data)) : ?>
                    <?php foreach($cities_data as $city_data) : ?>
                      <li>
                        <a href="../city.php?city_id=<?php echo $city_data['city_id'] ?>"><?php echo $city_data['city_name'] ?></a> -
                        <a href="update-city.php?city_id=<?php echo $city_data['city_id'] ?>">[editer]</a> -
                        <a href="delete-city.php?city_id=<?php echo $city_data['city_id'] ?>">[supprimer]</a>
                      </li>
                    <?php endforeach ?>
                  <?php endif ?>
                </ul>
              </div>
<?php

require('../includes/inc-connexion.php');
require('../process/process-update-city.php');

$cities_data = array(

	'city_id' => $city_id,
	'city_name' => $city_name,
	'city_text' => $city_text
);

$city_data = new City($cities_data);

$manager = new cityManager($db);
$cities_data = $manager->getDisplayCity($city_data);

?>

<!DOCTYPE html>

<html lang="fr">

<head>
	<title>Modifier une ville</title>
	<meta charset="utf-8">
	<link rel="stylesheet" href="css/style.css">
</head>

<body>
	<div id="wrapper">
		<h1>Modifier une ville</h1>
		<form action="" method="post">
			<table>
				<?php if(!empty($cities_data)) : ?>
					<?php foreach($cities_data as $city_data) : ?>
						<tr>
							<td>
								<input type="text" id="city_name" name="city_name_form" placeholder="Nom de ville"
						               value="<?php echo $city_data['city_name'] ?>">
					        </td>
				        </tr>

				        <tr>
					       <td><label for="city_text">Texte de présentation</label></td>
				        </tr>

				        <tr>	
					       <td>
					       	<textarea rows="25" cols="50" id="city_text" name="city_text_form">
					       	<?php echo $city_data['city_text'] ?>
							
						    </textarea>
					       </td>
				        </tr>

				        <tr>
					       <td><input type="reset" value="Remise à zéro du formulaire"></td>
				        </tr>

				        <tr>
					       <td><input type="submit" id="submit" name="submit_form" value="Modifier la ville"></td>
				        </tr>
				    <?php endforeach ?>
				<?php endif ?>
			</table>
		</form>

		<?php if(isset($message)) echo $message ?>
	</div>
</body>

</html>
<?php

require('../includes/inc-connexion.php');
require('../class/City.php');
require('../class/cityManager.php');

if(isset($_POST['submit_form'])) {

  $city_name_form = trim($_POST['city_name_form']);
  $city_text_form = trim($_POST['city_text_form']);

  if((empty($city_name_form)) OR (empty($city_text_form))) {

    $message = '<p class="error">Veuillez remplir tous les champs !</p>';
  }
  else {

  	$data_city = array(
      
  		'city_name' => $city_name_form,
  		'city_text' => $city_text_form
  	);

  	$data_cities = new City($data_city);

  	$manager = new cityManager($db);

    if(!$manager->getCity($city_name_form, $city_text_form)) {

      $message = '<p class="error">La ville ' . $city_name_form . ' n\'a pas été mise à jour !</p>';
    }
    else {

      $manager->updateCity($data_cities);

      $message = '<p class="success">La ville ' . $city_name_form . ' a été mise à jour !</p>';
    }
  }
}

Bonjour, quand je clique sur le lien éditer de la page admin/index.php devant chaque ville, j'ai toujours la même ville qui s'affiche dans le formulaire de mise à jour de la ville,

Voici mes fichiers -> class/cityManager.php, class/City.php, admin/index.php, admin/update-city.php, process/process-update-city.php

Merci pour votre aide.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...