-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
100 lines (81 loc) · 2.71 KB
/
menu.c
File metadata and controls
100 lines (81 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Name: MegaMenuC
* Date: 2022/06/03
* Author: Max Base
* Repository: https://github.com/BaseMax/MegaMenuC
* Description: MegaMenuC is a simple menu system for C that allows you to recursively get menu items and its submenus.
*/
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tree_value_t;
struct tree_t {
int size;
struct tree_value_t **items;
};
struct tree_value_t {
char* id;
char* name;
struct tree_t* children;
};
void db_error_exit(MYSQL *con)
{
fprintf(stderr, "Sorry, Error: %s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
void display(struct tree_t* tree, int space)
{
for (int i = 0; i < tree->size; i++) {
struct tree_value_t* item = tree->items[i];
if (space > 0) {
for (int j = 0; j < space; j++) printf(" ");
}
printf("- %s\n", item->name);
// recursive call
display(item->children, space + 5);
}
}
struct tree_t* menu(MYSQL *con, int parent_id)
{
struct tree_t *items = (struct tree_t*)malloc(sizeof(struct tree_t));
items->size = 0;
const int MAX_CHILD = 124;
items->items = (struct tree_value_t**)malloc(MAX_CHILD * sizeof(struct tree_value_t*));
char *sql = malloc(sizeof(char) * 1024);
char *sql_prefix = "SELECT `menu_item`.* FROM `menu` INNER JOIN `menu_item` ON `menu_item`.`id` = `menu`.`menu_id` WHERE `menu`.`parent_id`";
if (parent_id == 0) // root
sprintf(sql, "%s IS NULL", sql_prefix);
else
sprintf(sql, "%s = %d", sql_prefix, parent_id);
if (mysql_query(con, sql)) db_error_exit(con);
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) db_error_exit(con);
// int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
struct tree_value_t *item = (struct tree_value_t*)malloc(sizeof(struct tree_value_t));
item->id = row[0];
item->name = malloc(sizeof(char) * (strlen(row[1]) + 1));
strcpy(item->name, row[1]);
item->children = menu(con, atoi(row[0]));
items->items[items->size++] = item;
}
mysql_free_result(result);
return items;
}
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
// NOTE: You need to put your own username, password, and database name here
if (mysql_real_connect(con, "localhost", "root", "*************", "*************", 0, NULL, 0) == NULL) db_error_exit(con);
struct tree_t *tree = menu(con, 0); // 0 mean main menu items without parent
display(tree, 0); // 0 mean no spacing from left side
mysql_close(con);
exit(0);
}