Jump to content

Recommended Posts

Hello, I am trying to read the first letter of all members in a table, then store them in an Array with duplications, but the problem is that the letters are stored duplicated when i print the array the end.

my code:

 

<?php
$ltr = array ('m'); //this is my username

$query = "SELECT *  FROM members ";
$result = mysql_query($query) or die (mysql_error());
while( $row = mysql_fetch_array($result))
{
$rest = substr("$row[usernme]", 0, 1); //this read the 1st letter of each username
//echo "Rest = $rest <br>"; this is working

	//check if this letter exists in the array, No duplications allowed
	foreach ($ltr as $show)
	{
		if ($show != $rest)
		{$ltr[] = $rest; echo "$rest added<br>";} //ballouta
		else {}
	}//foreach

}//end while

	foreach ($ltr as $show)
	{
	echo "$show /";
	}
?>

 

i added the echo line to check if if is entering to that scope, and it really show that it is adding it many times,

 

Please help

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/160268-solved-array-logic-problem/
Share on other sites

SELECT SUBSTRING(`UserName`,1,1) as letter FROM `users` GROUP BY letter
$result = mysql_query($query) or die (mysql_error());
  $letters = array();
   while( $row = mysql_fetch_array($result))
   {
     $letters[] = $row['letter']
   }

 

Edit: never mind lol

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.