This question already has an answer here:
I copied a code from the internet and try to modify it for my website but I don't why I can't do it right. I don't know what's missing here.
Upper part, php code:
<?php
$category_id = $_GET['category_id'];
$temp = array();
$result = mysql_query("SELECT SUBSTRING(place_name 1,1) AS alpha, place_name, place_pictures FROM packages P1 INNER JOIN packages_category P2 ON P1.category_id = P2.category_id WHERE category_id = '$category_id' GROUP BY SUBSTRING(place_name, 0, 2), place_name order by 'alpha', 'place_name'");
?>
On my HTML CODE
<form action ="" method ="POST">
<table>
<?php while($row=mysql_fetch_array($result)) {?>
<tr>
<?php $temp[$row['alpha']][] = $row['place_name'];?>
}
?>
</tr>
<?php } ?>
</table>
</form>
I actually don't get it but anyway what I'm trying to do is to group the list of places alphabetically like what I've searched in the internet.
I'm trying to display the list of places in alphabet order.
I have two database, packages and packages_category:
For packages (place_id, place_name, place_picture, category_id) For packages_category (category_id, category_title, category_picture)
I'm trying to get the places under a certain category by joining the two tables. I want to display it this way:
Example output:
A
America
Australia
C
Canada
California
Can someone help me to correct the codes?
</div>
A ,
was missing in SUBSTRING
. The qyery should be -
SELECT SUBSTRING(place_name,1,1) AS alpha, place_name, place_pictures FROM packages P1 INNER JOIN packages_category P2 ON P1.category_id = P2.category_id WHERE category_id = '$category_id' GROUP BY SUBSTRING(place_name, 0, 2), place_name order by 'alpha', 'place_name'
Please use mysqli
or PDO
. mysql
is deprecated.