Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array_push and in_array problem 1

Status
Not open for further replies.

brownfox

Programmer
Jan 5, 2003
173
GB
My sql query outputs this:
Code:
Afrikaans
MATTHEWS,Hilda
Tel: 020-7284-4774 ,Mob: 07956-442586 
Arabic
KUZI-SARIG,Sari
Tel: 020-8954-5354 ,Mob: 07956-387835 ,Fax: 020-8954-5354 
Bulgarian
BALDWIN,Milva
Tel: 020-8882-9861 ,Mob: 07989-555151 ,Fax: 00-33-1-48080049 
Bulgarian
COLLINS,Dolly
Tel: 0033472399383 ,Mob: 003310680149344 ,Fax: 0022478562945 
Cantonese
HAUCHU,Tim
Tel: 020-8653-3070 ,Fax: 020-8653-3070 
Cantonese
LOK,Suky
Tel: 020-8505-8088 ,Mob: 07710-235-095 ,Fax: 020-8505-8088
but I want only one heading for each language like this:
Code:
Afrikaans
MATTHEWS,Hilda
Tel: 020-7284-4774 ,Mob: 07956-442586 
Arabic
KUZI-SARIG,Sari
Tel: 020-8954-5354 ,Mob: 07956-387835 ,Fax: 020-8954-5354 
Bulgarian
BALDWIN,Milva
Tel: 020-8882-9861 ,Mob: 07989-555151 ,Fax: 00-33-1-48080049 

COLLINS,Dolly
Tel: 0033472399383 ,Mob: 003310680149344 ,Fax: 0022478562945 
Cantonese
HAUCHU,Tim
Tel: 020-8653-3070 ,Fax: 020-8653-3070 

LOK,Suky
Tel: 020-8505-8088 ,Mob: 07710-235-095 ,Fax: 020-8505-8088
So I thought I'd check if the heading is already in the array, if not then add it to the array and print it. If it's already there return "".
Code:
$old_value = array();
function newValue($value1){
	if (in_array($value1,$old_value)){		
	return "";
	}else{
	array_push($old_value,$value1);
	return $value1;
	}
}
And the sql:
Code:
$qMain = "SELECT * FROM my_table";
$result = mysql_query($qMain) or die (mysql_error());
$num = mysql_num_rows($result);
$i=0;
while($i < $num){
$guide_surname = mysql_result($result,$i,"guide_surname");
$guide_forename = mysql_result($result,$i,"guide_forename");
$code_abbreviation = mysql_result($result,$i,"code_description");
$telephone_type = mysql_result($result,$i,"telephone_type");
echo newValue($code_abbreviation)."<br>$guide_surname,$guide_forename<br>$Tel $Mob $Fax $Pgr<br>";
++$i;
}
But it doesn't work and outputs duplicate headings. Does anyone know why or can suggest and alternate way of doing this? Thanks in advance...
 
You are running into a scope issue. While the variable $old_value is available outside the function newvalue() inside PHP has no knowledge of it. You can either oass it as a parameter or use a global statement to include it inside the functions' scope:
Code:
function newvaleu($param){
  global $old_value;
  # etc.
}

Your handling of the mysql result is quite laborious.
I would suggest to change to a structure that retrieves the row into an array in a while loop. I would also order the result by the criterion of the heading - you are now relying on the physical order of the database.
Code:
$qMain = "SELECT * FROM my_table ORDER by whateverColumnIs theCriterion";
$result = mysql_query($qMain) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
   # all columns are now in an associative array where the key is the column name
   echo newValue($row['code_abbreviation'])."<br>".$row['guide_surname'].",".$row['guide_forename']."<br>."$row['Tel'].' '.$row['Mob'].' ' .$row['Fax'].' '. $row['Pgr']."<br>";
}
The display you might also want to work it a bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top