Multiple rows in mysql

Post June 21st, 2006, 7:53 pm

ok i am trying to get all the information to show up from only postusername but only one row in the database with the username in postusername col will show up

Code: [ Download ] [ Select ]
<?
/* Requested Username error checking */
$req_user = $_GET['user'];
if(!$req_user || strlen($req_user) == 0 ||
  !eregi("^([0-9a-z])+$", $req_user) ||
  !$database->usernameTaken($req_user)){
  die("Username not registered ");
}


/* Display requested user information */
$req_user_postusername = $database->getUserComments($req_user);
?>
     <br>
     <table width="100%" border="0" class="comments"><tr><td width="24%"><?php
            echo "".$req_user_postusername['username'].""; ?></td>
     <td width="76%"><?php
            echo "".$req_user_postusername['comment'].""; ?></td>
     </tr></table>
 </p>
  1. <?
  2. /* Requested Username error checking */
  3. $req_user = $_GET['user'];
  4. if(!$req_user || strlen($req_user) == 0 ||
  5.   !eregi("^([0-9a-z])+$", $req_user) ||
  6.   !$database->usernameTaken($req_user)){
  7.   die("Username not registered ");
  8. }
  9. /* Display requested user information */
  10. $req_user_postusername = $database->getUserComments($req_user);
  11. ?>
  12.      <br>
  13.      <table width="100%" border="0" class="comments"><tr><td width="24%"><?php
  14.             echo "".$req_user_postusername['username'].""; ?></td>
  15.      <td width="76%"><?php
  16.             echo "".$req_user_postusername['comment'].""; ?></td>
  17.      </tr></table>
  18.  </p>


that is my code and what do i do to make it where all the postusername and comments from that pitucaler postusername will show up?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 21st, 2006, 7:53 pm

  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 12:03 am

Hi,

well, could you show us the function getUserComments(), and better, all the definition of class of the object "$database".
THere's probably a loop to implement but we need more informations.

Post June 22nd, 2006, 5:37 am

Code: [ Download ] [ Select ]
function getUserComments($username){
     $q = "SELECT * FROM ".TBL_COMMENTS." WHERE postusername = '$username'";
   $result = mysql_query($q, $this->connection);
   /* Error occurred, return given name by default */
   if(!$result || (mysql_numrows($result) < 1)){
     return NULL;
   }
   /* Return result array */
   $dbarray = mysql_fetch_array($result);
   return $dbarray;
  }
  1. function getUserComments($username){
  2.      $q = "SELECT * FROM ".TBL_COMMENTS." WHERE postusername = '$username'";
  3.    $result = mysql_query($q, $this->connection);
  4.    /* Error occurred, return given name by default */
  5.    if(!$result || (mysql_numrows($result) < 1)){
  6.      return NULL;
  7.    }
  8.    /* Return result array */
  9.    $dbarray = mysql_fetch_array($result);
  10.    return $dbarray;
  11.   }
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 6:35 am

OK you should return only the recordset pointer, so that you can loop all the rows for the display stuff.

Code: [ Download ] [ Select ]
function getUserComments($username){
     $q = "SELECT * FROM ".TBL_COMMENTS." WHERE postusername = '$username'";
   $result = mysql_query($q, $this->connection);
   /* Error occurred, return given name by default */
   if(!$result || (mysql_numrows($result) < 1)){
     return NULL;
   }
   /* Return result array */
   //$dbarray = mysql_fetch_array($result);
   //return $dbarray;
   return $result;
  }
  1. function getUserComments($username){
  2.      $q = "SELECT * FROM ".TBL_COMMENTS." WHERE postusername = '$username'";
  3.    $result = mysql_query($q, $this->connection);
  4.    /* Error occurred, return given name by default */
  5.    if(!$result || (mysql_numrows($result) < 1)){
  6.      return NULL;
  7.    }
  8.    /* Return result array */
  9.    //$dbarray = mysql_fetch_array($result);
  10.    //return $dbarray;
  11.    return $result;
  12.   }

display stuff :
Code: [ Download ] [ Select ]
 
  <?
/* Requested Username error checking */
$req_user = $_GET['user'];
if(!$req_user || strlen($req_user) == 0 ||
  !eregi("^([0-9a-z])+$", $req_user) ||
  !$database->usernameTaken($req_user)){
  die("Username not registered ");
}


/* Display requested user information */
$rs_pointer = $database->getUserComments($req_user);
?>
     <br>
     <table width="100%" border="0" class="comments">
     <?php
     while($req_user_postusername = mysql_fetch_array($rs_pointer))
     {
                        echo "<tr><td width=\"24%\">".$req_user_postusername['username']."</td>"; 
         echo "<td width=\"76%\">".$req_user_postusername['comment']."</td></tr>\n";
        }
        ?>
     </table>
 </p>
  1.  
  2.   <?
  3. /* Requested Username error checking */
  4. $req_user = $_GET['user'];
  5. if(!$req_user || strlen($req_user) == 0 ||
  6.   !eregi("^([0-9a-z])+$", $req_user) ||
  7.   !$database->usernameTaken($req_user)){
  8.   die("Username not registered ");
  9. }
  10. /* Display requested user information */
  11. $rs_pointer = $database->getUserComments($req_user);
  12. ?>
  13.      <br>
  14.      <table width="100%" border="0" class="comments">
  15.      <?php
  16.      while($req_user_postusername = mysql_fetch_array($rs_pointer))
  17.      {
  18.                         echo "<tr><td width=\"24%\">".$req_user_postusername['username']."</td>"; 
  19.          echo "<td width=\"76%\">".$req_user_postusername['comment']."</td></tr>\n";
  20.         }
  21.         ?>
  22.      </table>
  23.  </p>
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 6:41 am

you can add an if statment for the returned value (null or pointer) so that you don't do the display stuff if the return is null ;-)
____________________
My web site[/url] oh sh..!

Post June 22nd, 2006, 6:45 am

thank you it work and also is there a way where somebody can put in multiple of lines in a text area and when they hit enter it goes down to the next line and it put it in the database in there correctly but when i go to show the users information it comes up all on one line what do i do to make it where they dont have to put in < br> for a enter?
Thanks,
Josh --DemonMaestro
www.GothicGreenWire.com
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 7:35 am

If I understood what you want ,yeah no problem,

PHP Code: [ Download ] [ Select ]
//...
 
$msg_to_insert = preg_replace ("#(\n)#","<br />",$_POST["message"]);
 
 
  1. //...
  2.  
  3. $msg_to_insert = preg_replace ("#(\n)#","<br />",$_POST["message"]);
  4.  
  5.  

all the carriage return typed in the texarea are replace by "<br />" before inserting in DB.
so you just do nothing else , that'll show the carrage returns when you will display the message
____________________
My web site[/url] oh sh..!

Post June 22nd, 2006, 7:44 am

so basically where do i put it in if this is my code?

PHP Code: [ Download ] [ Select ]
 
<?
 
 
 
include("session.php");
 
?>
 
<?
 
     $database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
 
                     header("Location: ../userinfo.php?user=$username");
 
?>
  1.  
  2. <?
  3.  
  4.  
  5.  
  6. include("session.php");
  7.  
  8. ?>
  9.  
  10. <?
  11.  
  12.      $database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
  13.  
  14.                      header("Location: ../userinfo.php?user=$username");
  15.  
  16. ?>
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 8:06 am

Depending on wich field of the form is supposed to be a texarea, just do that stuf on the right $_POST juste before updateUserProfile() call.

ex :
PHP Code: [ Download ] [ Select ]
 
include("session.php");
 
$_POST['likes'] = preg_replace ("#(\n)#","<br />",$_POST["likes"]);
 
//do the same stuff for whatelse $_POST[] needs to be checked
 
$database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
 
header("Location: ../userinfo.php?user=$username");
 
 
  1.  
  2. include("session.php");
  3.  
  4. $_POST['likes'] = preg_replace ("#(\n)#","<br />",$_POST["likes"]);
  5.  
  6. //do the same stuff for whatelse $_POST[] needs to be checked
  7.  
  8. $database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
  9.  
  10. header("Location: ../userinfo.php?user=$username");
  11.  
  12.  



If all the fields need to be check you can loop so :
PHP Code: [ Download ] [ Select ]
 
include("session.php");
 
foreach ($_POST as $key=>$value)
 
   $_POST[$key] = preg_replace ("#(\n)#","<br />",$value);
 
$database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
 
header("Location: ../userinfo.php?user=$username");
 
 
  1.  
  2. include("session.php");
  3.  
  4. foreach ($_POST as $key=>$value)
  5.  
  6.    $_POST[$key] = preg_replace ("#(\n)#","<br />",$value);
  7.  
  8. $database->updateUserProfile($username, $_POST['profile'], $_POST['likes'], $_POST['dislikes'], $_POST['music'], $_POST['aim'], $_POST['age'], $_POST['city'], $_POST['state'], $_POST['sex'], $_POST['yahoo'], $_POST['msn'], $_POST['link'], $_POST['sexuality'], $_POST['country']);
  9.  
  10. header("Location: ../userinfo.php?user=$username");
  11.  
  12.  

Post June 22nd, 2006, 8:15 am

ok that work but when the user go to edit the profile again it puts in a extra <br> than nessorly
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 8:28 am

OK you have to fill the texarea before re-edit with <br /> turned back to \n.

<textarea>.preg_replace("#(<br />)#, "\n", $message_to_edit).</textarea>

ask any question ;-)

Post June 22nd, 2006, 8:33 am

PHP Code: [ Download ] [ Select ]
<? echo "<textarea name='profile' cols='75' rows='10' class='textarea' wrap=virtual>".$req_user_profile['profile']."</textarea>";?>
so where would that go in there?
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 8:34 am

an another way, insert the texareas like there are and add the <br /> just before displaying stuff
____________________
My web site[/url] oh sh..!

Post June 22nd, 2006, 8:49 am

users are going to be edit there profile ?
Thanks,
Josh --DemonMaestro
www.GothicGreenWire.com
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 22nd, 2006, 8:59 am

you don't do the preg_replace just before updateUserProfile() so the mess are stored like there are typed(without "<br />".
Like it was before, and you add the <br /> at the last moment just for the displaying stuff :

Code: [ Download ] [ Select ]
 <?
/* Requested Username error checking */
$req_user = $_GET['user'];
if(!$req_user || strlen($req_user) == 0 ||
  !eregi("^([0-9a-z])+$", $req_user) ||
  !$database->usernameTaken($req_user)){
  die("Username not registered ");
}


/* Display requested user information */
$rs_pointer = $database->getUserComments($req_user);
?>
     <br>
     <table width="100%" border="0" class="comments">
     <?php
     while($req_user_postusername = mysql_fetch_array($rs_pointer))
     {
         echo "<tr><td width=\"24%\">".$req_user_postusername['username']."</td>"; 
       echo "<td width=\"76%\">".preg_replace("#(\n)#", "<br />", $req_user_postusername['comment'])."</td></tr>\n";
      }
      ?>
     </table>
 </p>
  1.  <?
  2. /* Requested Username error checking */
  3. $req_user = $_GET['user'];
  4. if(!$req_user || strlen($req_user) == 0 ||
  5.   !eregi("^([0-9a-z])+$", $req_user) ||
  6.   !$database->usernameTaken($req_user)){
  7.   die("Username not registered ");
  8. }
  9. /* Display requested user information */
  10. $rs_pointer = $database->getUserComments($req_user);
  11. ?>
  12.      <br>
  13.      <table width="100%" border="0" class="comments">
  14.      <?php
  15.      while($req_user_postusername = mysql_fetch_array($rs_pointer))
  16.      {
  17.          echo "<tr><td width=\"24%\">".$req_user_postusername['username']."</td>"; 
  18.        echo "<td width=\"76%\">".preg_replace("#(\n)#", "<br />", $req_user_postusername['comment'])."</td></tr>\n";
  19.       }
  20.       ?>
  21.      </table>
  22.  </p>
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 22nd, 2006, 8:59 am

Post Information

  • Total Posts in this topic: 97 posts
  • Users browsing this forum: No registered users and 212 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.