FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help with PHP + SQL problem.





Diablosblizz
Code:
$select = mysql_query("SELECT * FROM `users` WHERE `name` = '$username'");
while ($selectn = mysql_fetch_array($select)) {
   $select123 = str_replace("<", "", $selectn['friendlist']);
   $friendslist = str_replace(">", "", $select123);
   $total = mysql_num_rows($select);
if($total == 0) {
   echo "Sorry $username you don't have any friends on your console! Go to the hotel and make some!";
} else {
   if($total == 1) {
      $comma = "";
   } else if($total > 1) {
      $comma = ", ";
   }
}
echo "$friendslist$comma - $total";
}


Okay, the above code is supposed to get the users' friends from the Database. It gets the friends, but it doesn't comma them or anything. The friends are stored as:

Quote:
<friendname><nextfriendname><next>


I have the < and the > removed using $select123 and $friendslist. (Don't ask about the < and >, they must be there.) Okay, so in my database my friend list says:

Quote:
<><1><123>


The <> starts off the field, it also needs to be there. The first code above displays my friends as so:

Quote:
1123 - 1


(Don't worry about the - 1 thing) I want it to comma the friends, so instead of having the "1123" it would do something such as: "1, 123" if "123" is the last entry in the field then it will not have a comma. It seems that I am unable to do it, because I don't know how to make it separate the friends. (The < and > separates them, but because I have them removed it makes it difficult.)

So, does anybody know how to comma them out? Many thanks.
woodenbrick
Instead of replacing > with "", maybee you could use ", "?
Then just check the end of the friendslist string and delete the last comma.
I actually did a similar thing to this so i'll put my code up (it was when I just started learning php so it may seem a bit strangely written, but it works). It's to list the people who worked on a certain script.
Code:
if (mysql_num_rows($edit)>0){
   if (mysql_num_rows($edit)==1){ //only one editor
      $editors=mysql_fetch_object($edit);
      $editors=" with help from $editors->editor";
   }
   else {
      $x=0;
      while ($editors=mysql_fetch_object($edit)){
         $editorlist[$x]= $editors->editor;
         $x++;
      }
   for ($x=0; $x<sizeof($editorlist)-2; $x++){//-2 so the last editor doesn't get comma
      $editors.=$editorlist[$x].", ";
   }
   $editors=" with help from $editors".$editorlist[sizeof($editorlist)-2]." and "
   .$editorlist[sizeof($editorlist)-1].".";//prefix last editor with 'and'
   }
}
echo "<p class='time' />Created by $creator->creator $editors";
Diablosblizz
I have tried replacing the < with , and it doesn't work. It has a ", " at the beginning of the echo.

Anybody else have any more ideas? Smile
woodenbrick
not < but >! Very Happy
edit: In case thats not clear, I was suggesting you change the > sign, not the < sign.
Diablosblizz
Ah sorry, I did replace > instead of the <. I apologize, my teacher was right behind me when I posted that.
wmcasino
There`s your site?
wmcasino
Maybe I can help you!!))
Related topics
[HELP] PHP -> Scripting
PHP Login Problem
Needing a Bit Og Help {PHP}!
need some help - php switch code
Embarrassing newbie php/SQL questions
.sql problem
[php/SQL] Help needed
help with pagination problem
PHP Mysql admin area? please help
php msql problem
SQL Problem Help
PHP noob problem
[HELP] PHP (and some MyBB) Administration
PHP Includes Problem
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.