Jump to content

[SOLVED] Array Logic Problem


ballouta

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.