0

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?

ekad
  • 14,056
  • 26
  • 43
  • 45
Larrie
  • 37
  • 5
  • Aside from the syntactical error (and the deprecated code), a GROUP BY clause with no aggregating functions is nonsensical. What are you actually trying to do. Provide a small example of what you have and what you would wish it to look like – Strawberry Dec 10 '14 at 08:20
  • So, this is just an ordered list (SELECT stuff FROM my_table ORDER BY something). Then, in PHP, start a loop. Every time the the first letter changes, start a new table titled with that letter. See, no aggregation required. – Strawberry Dec 10 '14 at 08:52

1 Answers1

0

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.

Sougata Bose
  • 30,871
  • 8
  • 44
  • 87