Afficher le nom des utilisateurs plutôt l'id d'utilisateur de la base de données ?

  • iWiGG_2010
  • Newbie
  • Newbie
  • Avatar de l’utilisateur
  • Inscription: Nov 22, 2011
  • Messages: 10
  • Loc: Australia
  • Status: Offline

Message Novembre 22nd, 2011, 7:44 pm

Bonjour à tous

Je suis en train de faire une page de base de forum. À l'heure actuelle, il affiche l'utilisateur comme un nombre (qui est défini dans la base de données). A, vous vous demandez comment l'obtenir pour afficher le nom des utilisateurs plutôt que leur numéro de base de données ?

Il s'agit de mes tableaux :

« utilisateurs » - user_id, email, nom, mot de passe
« postes » - id, album_id, topic_id, post_creator, post_contact, post_date
« sujets » - id, album_id, topic_id, topic_creator, topic_last_user, topic_date, topic_reply_date, topic_views

Je peux seulement suppose que j'ai que beaucoup besoin pour rejoindre la .user_id « utilisateurs » tableau avec la .post_creator de « postes ». Si je dois utiliser des jointures je cadeaux semblent confondre avec cette partie.

Toute aide fera, merci.

Voici mon code :

view_topic.php
PHP Code: [ Select ]
<?php
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$sql = "SELECT * FROM `topics` WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 1) {
   echo "<table width='100%'>";
      if ($_SESSION['user_id']) {
         echo "<tr><td colspan='2'><center><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /></center><hr /><br />";
            } else {
             
               echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
      while ($row = mysql_fetch_assoc($res)) {
         $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
         $res2 = mysql_query($sql2) or die(mysql_error());
         while ($row2 = mysql_fetch_assoc($res2)) {
            echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
           
            <tr><td colspan='2'><hr /></td></tr>";
         }
         $old_views = $row['topic_views'];
         $new_views = $old_views + 1;
         $sql3 = "UPDATE `topics` SET `topic_views`='".$new_views."' WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
         $res3 = mysql_query($sql3) or die(mysql_error());
      }
         echo "</table>";
} else {
   echo "<p>This topic does not exist</p>";  
}
?>
 
  1. <?php
  2. $cid = $_GET['cid'];
  3. $tid = $_GET['tid'];
  4. $sql = "SELECT * FROM `topics` WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
  5. $res = mysql_query($sql) or die(mysql_error());
  6. if (mysql_num_rows($res) == 1) {
  7.    echo "<table width='100%'>";
  8.       if ($_SESSION['user_id']) {
  9.          echo "<tr><td colspan='2'><center><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /></center><hr /><br />";
  10.             } else {
  11.              
  12.                echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
  13.       while ($row = mysql_fetch_assoc($res)) {
  14.          $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
  15.          $res2 = mysql_query($sql2) or die(mysql_error());
  16.          while ($row2 = mysql_fetch_assoc($res2)) {
  17.             echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
  18.            
  19.             <tr><td colspan='2'><hr /></td></tr>";
  20.          }
  21.          $old_views = $row['topic_views'];
  22.          $new_views = $old_views + 1;
  23.          $sql3 = "UPDATE `topics` SET `topic_views`='".$new_views."' WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
  24.          $res3 = mysql_query($sql3) or die(mysql_error());
  25.       }
  26.          echo "</table>";
  27. } else {
  28.    echo "<p>This topic does not exist</p>";  
  29. }
  30. ?>
  31.  



view_topic_parse.php
PHP Code: [ Select ]
<?php
if (isset($_POST['topic_submit'])) {
   if (($_POST['topic_title'] == "") && ($_POST['topic_content']) == "") {
      echo "<p>Sorry, something went wrong! You did not fill in both fields. Please return to the previous plage.</p>";
      exit();
   } else {
      $cid = $_POST['cid'];
      $title = $_POST['topic_title'];
      $content = $_POST['topic_content'];
      $creator = $_SESSION['user_id'];
      $sql = "INSERT INTO topics (album_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
      $res = mysql_query($sql) or die(mysql_error());
      $new_topic_id = mysql_insert_id();
      $sql2 = "INSERT INTO posts (album_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
      $res2 = mysql_query($sql2) or die(mysql_error());
      $sql3 = "UPDATE albums SET last_post_date=now(), `last_user_posted`='".$creator."' WHERE `album_id`='".$cid."' LIMIT 1";
      $res3 = mysql_query($sql3) or die(mysql_error());
      if (($res) && ($res2) && ($res3)) {
         header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
      } else {
         echo "There was a problem creating your topic. Please try again!";
      }
   }
}
?>
 
 
 
  1. <?php
  2. if (isset($_POST['topic_submit'])) {
  3.    if (($_POST['topic_title'] == "") && ($_POST['topic_content']) == "") {
  4.       echo "<p>Sorry, something went wrong! You did not fill in both fields. Please return to the previous plage.</p>";
  5.       exit();
  6.    } else {
  7.       $cid = $_POST['cid'];
  8.       $title = $_POST['topic_title'];
  9.       $content = $_POST['topic_content'];
  10.       $creator = $_SESSION['user_id'];
  11.       $sql = "INSERT INTO topics (album_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
  12.       $res = mysql_query($sql) or die(mysql_error());
  13.       $new_topic_id = mysql_insert_id();
  14.       $sql2 = "INSERT INTO posts (album_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
  15.       $res2 = mysql_query($sql2) or die(mysql_error());
  16.       $sql3 = "UPDATE albums SET last_post_date=now(), `last_user_posted`='".$creator."' WHERE `album_id`='".$cid."' LIMIT 1";
  17.       $res3 = mysql_query($sql3) or die(mysql_error());
  18.       if (($res) && ($res2) && ($res3)) {
  19.          header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
  20.       } else {
  21.          echo "There was a problem creating your topic. Please try again!";
  22.       }
  23.    }
  24. }
  25. ?>
  26.  
  27.  
  28.  
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Novembre 22nd, 2011, 7:44 pm

  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8212
  • Loc: USA
  • Status: Offline

Message Novembre 25th, 2011, 1:16 pm

Quote:
Code: [ Select ]
      echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
     
      <tr><td colspan='2'><hr /></td></tr>";
  1.       echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
  2.      
  3.       <tr><td colspan='2'><hr /></td></tr>";

Je pense que c'est où vous voulez changer le code. Le $row2 ["post_creator"] est l'id utilisateur, droite ? Vous devez faire une requête SQL dans cette boucle pour récupérer le nom d'utilisateur et les utiliser à la place.

Code: [ Select ]
     while ($row2 = mysql_fetch_assoc($res2)) {
      $user_name = mysql_fetch(mysql_query("SELECT username FROM usertable WHERE userid = {$row2['post_creator']}"));
      echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
     
      <tr><td colspan='2'><hr /></td></tr>";
     }
  1.      while ($row2 = mysql_fetch_assoc($res2)) {
  2.       $user_name = mysql_fetch(mysql_query("SELECT username FROM usertable WHERE userid = {$row2['post_creator']}"));
  3.       echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
  4.      
  5.       <tr><td colspan='2'><hr /></td></tr>";
  6.      }

Je pense que c'est ce que vous voulez partir rapidement regarder.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • iWiGG_2010
  • Newbie
  • Newbie
  • Avatar de l’utilisateur
  • Inscription: Nov 22, 2011
  • Messages: 10
  • Loc: Australia
  • Status: Offline

Message Novembre 25th, 2011, 6:38 pm

Merci Bogey,

Il semble ressembler à ce que j'essaie de le faire, mais il toujours ne pas montrant le nom (seulement l'id d'utilisateur). Oui $row2 ["post_creator"] est l'id utilisateurs.

Peut-être que je fais quelque chose ? Désolé vraiment nouveau à php/mysql.

PHP Code: [ Select ]
               echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
      while ($row = mysql_fetch_assoc($res)) {
         $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
         $res2 = mysql_query($sql2) or die(mysql_error());
                  while ($row2 = mysql_fetch_assoc($res2)) {
          $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
          echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
       
         <tr><td colspan='2'><hr /></td></tr>";
         }
 
  1.                echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
  2.       while ($row = mysql_fetch_assoc($res)) {
  3.          $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
  4.          $res2 = mysql_query($sql2) or die(mysql_error());
  5.                   while ($row2 = mysql_fetch_assoc($res2)) {
  6.           $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
  7.           echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
  8.        
  9.          <tr><td colspan='2'><hr /></td></tr>";
  10.          }
  11.  
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8212
  • Loc: USA
  • Status: Offline

Message Novembre 26th, 2011, 10:38 pm

You got changer les $row2 ["post_creator"] pour $user_name sur la ligne 6...j'ai oublié de changer cela dans mon exemple sur mon premier post.
PHP Code: [ Select ]
               echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
      while ($row = mysql_fetch_assoc($res)) {
         $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
         $res2 = mysql_query($sql2) or die(mysql_error());
                  while ($row2 = mysql_fetch_assoc($res2)) {
          $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
          echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$user_name." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
     
        <tr><td colspan='2'><hr /></td></tr>";
         }
  1.                echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
  2.       while ($row = mysql_fetch_assoc($res)) {
  3.          $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";  
  4.          $res2 = mysql_query($sql2) or die(mysql_error());
  5.                   while ($row2 = mysql_fetch_assoc($res2)) {
  6.           $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
  7.           echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$user_name." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
  8.      
  9.         <tr><td colspan='2'><hr /></td></tr>";
  10.          }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Afficher de l'information

  • Total des messages de ce sujet: 4 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 114 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC