Using an alias from a table

danielos77 Posted messages 111 Registration date   Status Member Last intervention   -  
jee pee Posted messages 31889 Registration date   Status Moderator Last intervention   -

Hello,

I'm turning to the forum because I’m spinning my wheels (endlessly).

To avoid having to change the same information in multiple pages, I created a simple table with 3 fields (ID, Var, Contenu).

The idea is to replace an information with a variable named after the content of a column Var and have it display the corresponding Contenu content.

Table

ID | Var | Contenu

1 | Pdt | Alain DUPONT

2 | MailPdt | Alain.dupont@patate.com

Then on the page, I would simply place

$Pdt=$alias['Contenu']; at the place where I would like to have Alain DUPONT (with my query already prepared upstream on the page in question)

I manage to retrieve my pairs, each in an array, with the following query :

$sql = 'SELECT Var,Contenu FROM Alias'; try{ $req = $bdd->query($sql); $res = $req->fetchAll(); } catch(Exception $e){ // in case of error : echo " <br>Erreur ! ".$e->getMessage(); echo " <br>Les datas : " ; print_r($res); }

Or to list all the $alias['Contenu']; by adding a foreach

$sql = 'SELECT Var,Contenu FROM Alias'; try{ $req = $bdd->query($sql); $res = $req->fetchAll(); foreach($res as $alias){ echo "<br>".$alias['Contenu']."<br>"; } } catch(Exception $e){ // in case of error : echo " <br>Erreur ! ".$e->getMessage(); echo " <br>Les datas : " ; print_r($res); }

But then I’m stuck on how to do it... if it’s doable.

I did search also on the side

$sql="SELECT Contenu FROM Alias where Var= :var"; $requete =$bdd->prepare($sql);1 $requete->bindValue(":var", $Var, PDO::PARAM_STR); $requete->execute(); 

but without success with $Var because if I have to manually enter the value of $Var ;-(

Thanks to anyone who can give me a lead
 


3 answers

  1. Grandasse_ Posted messages 974 Registration date   Status Member Last intervention   614
     

    Hello,

    It’s very curious the way you’re doing things, why not simply have a table like this:

    ID | Pdt | MailPdt

    1 | Alain DUPONT | Alain.dupont@patate.com
    2 | Michèle MICHU | mich@michu.com
    ...

    That would make things much easier because for each row you can request your variable Pdt or MailPdt, and both pieces of information are linked together (i.e., on the same row).

    Sorry, I know this doesn’t answer your question but I find this organization in your DB really over-complicated.


    0
  2. danielos77 Posted messages 111 Registration date   Status Member Last intervention   2
     

    Hello,

    Simply because on some pages there will be the name and the tph and on others it will be the sending of an email, etc... But yes, the variable in place of the alias could be $var=$alias['nom_colonne'];

    What could simplify the table is to set Var as unique and to remove the ID which will not be useful to me.

    The idea is simply not to have to go around all the pages and to scan all the codes whenever there is a change in the association.

    But as you say, that doesn't move things forward.

    @+
    Daniel

    0
  3. jee pee Posted messages 31889 Registration date   Status Moderator Last intervention   9 981
     

    Hello,

    If you’re at the stage of defining data and designing a website, you should focus first on the former.

    I imagine that in an association one could have:

    a member table: id, last name, first name, phone, email, registration date, ...

    a office table: id, memberId, role, start date, end date, ...


    0