Probleme pour faire un select multiple

kitten13 Messages postés 16 Date d'inscription jeudi 14 juin 2007 Statut Membre Dernière intervention 17 février 2017 - 29 déc. 2008 à 11:27
kitten13 Messages postés 16 Date d'inscription jeudi 14 juin 2007 Statut Membre Dernière intervention 17 février 2017 - 29 déc. 2008 à 16:23
Bonjour,

J'ai un souci pour faire une modif en php, j'aimerais votre aident svp.

Je vais essayer d'être la plus claire possible.

Sur mon site j'ai une liste des dernières annonces classer selon une région, à la première visite de l'internaute si aucune région n'a était choisie alors est afficher une région par défaut avec cette variable:

// the first city in the database will be taken as the default.
$default_city = -14;


Id -14 correspond à la région iles de france pour mieux comprendre voici l'architecture de ma table :

table countries

--
-- Structure de la table `countries`
--

CREATE TABLE IF NOT EXISTS `countries` (
  `countryid` smallint(5) unsigned NOT NULL auto_increment,
  `countryname` varchar(50) NOT NULL default '',
  `pos` smallint(5) unsigned NOT NULL default '0',
  `enabled` enum('0','1') NOT NULL default '0',
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`countryid`),
  KEY `pos` (`pos`),
  KEY `enabled` (`enabled`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;

--
-- Contenu de la table `countries`
--

INSERT INTO `countries` (`countryid`, `countryname`, `pos`, `enabled`, `timestamp`) VALUES
(2, 'Alsace', 1, '1', '2008-12-03 00:28:35'),
(3, 'Aquitaine', 2, '1', '2008-12-03 00:28:35'),
(4, 'Auvergne', 3, '1', '2008-12-03 00:28:35'),
(5, 'Basse Normandie', 4, '1', '2008-12-03 00:28:35'),
(6, 'Bourgogne', 5, '1', '2008-12-03 00:28:35'),
(7, 'Bretagne', 6, '1', '2008-12-03 00:28:35'),
(8, 'Centre Val de Loire', 7, '1', '2008-12-03 00:28:35'),
(9, 'Champagne-Ardenne', 8, '1', '2008-12-03 00:28:35'),
(10, 'Corse', 9, '1', '2008-12-03 00:28:35'),
(11, 'Dom-Tom', 10, '1', '2008-12-03 00:28:35'),
(12, 'Franche-Comté', 11, '1', '2008-12-03 00:28:35'),
(13, 'Haute Normandie', 12, '1', '2008-12-03 00:28:35'),
(14, 'Ile de France', 13, '1', '2008-12-03 00:28:35'),
(15, 'Languedoc-Roussillon', 14, '1', '2008-12-03 00:28:35'),
(16, 'Limousin', 15, '1', '2008-12-03 00:28:35'),
(17, 'Lorraine', 16, '1', '2008-12-03 00:28:35'),
(18, 'Midi-Pyrénées', 17, '1', '2008-12-03 00:28:35'),
(19, 'Nord-Pas-de-Calais', 18, '1', '2008-12-03 00:28:35'),
(20, 'PACA', 19, '1', '2008-12-03 00:28:35'),
(21, 'Pays de la Loire', 20, '1', '2008-12-03 00:28:35'),
(22, 'Picardie', 21, '1', '2008-12-03 00:28:35'),
(23, 'Poitou-Charentes', 22, '1', '2008-12-03 00:28:35'),
(24, 'Rhône-Alpes', 23, '1', '2008-12-03 00:28:35');
   

la fonction variable qui permet de choisir un id region est ici :

// Get current city
if ($_GET['cityid'] > 0)
{
    $xcityid = $_GET['cityid'];
}
elseif ($_GET['cityid'] < 0)
{
    $xcountryid = abs($_GET['cityid']);
    $xcityid = $_GET['cityid'];
}
elseif ($_COOKIE[$ck_cityid] > 0)
{
    $xcityid = $_COOKIE[$ck_cityid];
}
elseif ($_COOKIE[$ck_cityid] < 0)
{
    $xcountryid = abs($_COOKIE[$ck_cityid]);
    $xcityid = $_COOKIE[$ck_cityid];
}
elseif ($default_city)
{
    $xcityid = $default_city;
    if($xcityid < 0) $xcountryid = -($xcityid);
}

if ($xcityid)
{    if ($xcityid > 0) $sql "SELECT COUNT(*) FROM $t_cities WHERE cityid '$xcityid'";    else $sql "SELECT COUNT(*) FROM $t_countries WHERE countryid '$xcountryid'";

    list($city_exists) = @mysql_fetch_array(mysql_query($sql));
    if(!$city_exists) $xcityid = 0;
}

/*if(!$xcityid)
{
    $sql = "SELECT countryid
            FROM $t_countries
            WHERE enabled = '1'
            LIMIT 1";
    list($xcountryid) = mysql_fetch_array(mysql_query($sql));
    $xcityid = 0-$xcountryid;
}*/

if(!$xcityid)
{
    $sql = "SELECT cityid
            FROM $t_cities
            WHERE enabled = '1'
            LIMIT 1";
    list($xcityid) = @mysql_fetch_array(mysql_query($sql));
}

if (!$xcityid &amp;&amp; !$in_admin)
{
    die("No locations defined!");
}

setcookie($ck_cityid, $xcityid, time()+(60*24*60*60), "/");

// Get city name
if ($xcityid > 0)
{
    $sql = "SELECT c.countryname, c.countryid, ct.cityname
            FROM $t_cities ct
                INNER JOIN $t_countries c ON c.countryid = ct.countryid
            WHERE cityid = '$xcityid'";
    list($xcountryname, $xcountryid, $xcityname)= @mysql_fetch_array(mysql_query($sql));
}
elseif ($xcountryid)
{
    $sql = "SELECT c.countryname
            FROM $t_countries c 
            WHERE countryid = '$xcountryid'";
    list($xcountryname)= @mysql_fetch_array(mysql_query($sql));
    $xcityname = $xcountryname;
}


Vous allez me dire et ou est le problème !

Et bien en faite j'essaie depuis hier de faire un select sur toutes les id régions en même temps au lieu d'être limiter à faire un choix sur un id région.

En gros si c'est la première visite de l'internaute alors lui afficher les dernières annonces de toutes les région et pas uniquement la région -14 (ile de france).

Quelqu'un pourrais m'aider, merci d'avance.  :wink:

4 réponses

Noreaga10000 Messages postés 46 Date d'inscription vendredi 24 octobre 2008 Statut Membre Dernière intervention 27 janvier 2009
29 déc. 2008 à 11:40
Bonjour,

Je m'y perd un peu entre city et country.

Mais peut-être que à la place de:

if ($xcityid)
{
if ($xcityid > 0) $sql "SELECT COUNT(*) FROM $t_cities WHERE cityid '$xcityid'";
else $sql "SELECT COUNT(*) FROM $t_countries WHERE countryid '$xcountryid'";

list($city_exists) = @mysql_fetch_array(mysql_query($sql));
if(!$city_exists) $xcityid = 0;
}

tu pourrais essayer:

if ($xcityid)
{
if ($xcityid > 0) $sql "SELECT COUNT(*) FROM $t_cities WHERE cityid '$xcityid'";
else $sql = "SELECT COUNT(*) FROM $t_countries ORDER BY timestamp DESC LIMIT 5";

list($city_exists) = @mysql_fetch_array(mysql_query($sql));
if(!$city_exists) $xcityid = 0;
}
0
kitten13 Messages postés 16 Date d'inscription jeudi 14 juin 2007 Statut Membre Dernière intervention 17 février 2017
29 déc. 2008 à 11:56
Tout d'abord Merci pour ton aide Noreaga10000,

En faite ta solution ne fonctionne pas, je commence à désespérer!!

Ce que je souhaite par contre c'est faire un select sur tout les régions de la table countries en même temps.



Le post n'est pas propre désoler je ne trouve pas le bouton pour éditer à nouveaux mon poste.
0
Noreaga10000 Messages postés 46 Date d'inscription vendredi 24 octobre 2008 Statut Membre Dernière intervention 27 janvier 2009
29 déc. 2008 à 15:00
Franchement je vais te proposer un autre truc tout a fait au hasard, car je m'y perd réellement dans tes variables (je sais pas comment tu t'y retouves).

A la fin du code :
elseif ($xcountryid)
{
$sql = "SELECT c.countryname
FROM $t_countries c
WHERE countryid = '$xcountryid'";
list($xcountryname)= @mysql_fetch_array(mysql_query($sql));
$xcityname = $xcountryname;
}


Essayes plutôt:
elseif ($xcountryid)
{
$sql = "SELECT c.countryname, c.timestamp
FROM $t_countries c
ORDER by c.timestamp DESC
LIMIT 5";
list($xcountryname)= @mysql_fetch_array(mysql_query($sql));
$xcityname = $xcountryname;
}
0
kitten13 Messages postés 16 Date d'inscription jeudi 14 juin 2007 Statut Membre Dernière intervention 17 février 2017
29 déc. 2008 à 16:23
Oui je comprends il faut dire que le code n'est pas très lisible sur le poste et j'ai pris une mauvaise manie de pas commenter mon code.

Bon je vais reprendre au début car je tes pas tout donner les infos pour que tu puisse m'aider.

Voici la structure des 3 tables en question :
0
Rejoignez-nous