Hello people, i have the following code though it is not giving me the exact results i want. i would like the code to order by the name but the order by doesnt seem to be executed. here is the code;

$query2 = "select * from jos_categories where id = '".$row['cid']."' ORDER BY name ASC";
		$result2 = mysql_query($query2, $conn);
		$idrow = mysql_fetch_assoc($result2);
		$link = sefRelToAbs('index.php?option=' . $option . '&task=view&limit=0&id=' .$idrow['id'] . '&Itemid=' . $Itemid);

thanx in advance for any help.

Recommended Answers

All 5 Replies

What is your output then? Also there appears to be a mistake in your query. I presume ID is a number, then there is no need for single quotes. So...

where id = '".$row['cid']."'

becomes

where id = ".$row['cid']."

What is your output then? Also there appears to be a mistake in your query. I presume ID is a number, then there is no need for single quotes. So...

where id = '".$row['cid']."'

becomes

where id = ".$row['cid']."

Thank you for your reply,
well, it appears that with or without the quotes, it works ok.
the output is it brings back a list of all the categories i want thou it doesnt order them by name in alphabetical order as i want them. it seems to be ignoring the order by statement.
Output is
• Secondary Schools
• Primary school
• Special Needs Institutions
• Vocational School
• Nursery school

please, any help will be appreciated.

Hello people, i have the following code though it is not giving me the exact results i want. i would like the code to order by the name but the order by doesnt seem to be executed. here is the code;

$query2 = "select * from jos_categories where id = '".$row['cid']."' ORDER BY name ASC";
		$result2 = mysql_query($query2, $conn);
		
		$link = sefRelToAbs('index.php?option=' . $option . '&task=view&limit=0&id=' .$idrow['id'] . '&Itemid=' . $Itemid);

thanx in advance for any help.

can you try these 2 options if there is a difference on the output.

1. $idrow = mysql_fetch_array($result2, MYSQL_ASSOC);

or 

2. $idrow = mysql_fetch_array($result2);

can you try these 2 options if there is a difference on the output.

1. $idrow = mysql_fetch_array($result2, MYSQL_ASSOC);

or 

2. $idrow = mysql_fetch_array($result2);

Thanx for your reply,
however i have tried the two options and non of them changes the out put. perhaps i should post some more code to make it clearer. i have a distinct just before the while statement. maybe it is what is messing things up. the code seems to order by the id instead and overlook the orderby name statement.

$query = "select distinct cid from jos_artband";
	$result = mysql_query($query, $conn);
	$num = mysql_num_rows($result);

	while ($row=mysql_fetch_assoc($result))
	{

		$query2 = "select * from jos_categories where id = ".$row['cid']." ORDER BY name ASC";
		$result2 = mysql_query($query2, $conn);
		$idrow = mysql_fetch_array($result2, MYSQL_ASSOC);
		$link = sefRelToAbs('index.php?option=' . $option . '&task=view&limit=0&id=' .$idrow['id'] . '&Itemid=' . $Itemid);
		
 		if($idrow['name'] != "")
			{
				echo '<tr>';
				echo '<td align="center"><a href="' . $link . '">&bull;&nbsp;&nbsp;&nbsp;'.$idrow['name'] .'</a> </td>  
	 			</tr>';
	  		}
	}
	echo '</table>';

Any help will be highly appreciated.

like what you said....before the output are the ff:
Output is
• Secondary Schools
• Primary school
• Special Needs Institutions
• Vocational School
• Nursery school

is this a "NAME" column inside your database or "SCHOOL"? can you post your database structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.