added pagination to image search
This commit is contained in:
@@ -65,16 +65,29 @@ static char *fetch_images_html(const char *url) {
|
|||||||
int images_handler(UrlParams *params) {
|
int images_handler(UrlParams *params) {
|
||||||
TemplateContext ctx = new_context();
|
TemplateContext ctx = new_context();
|
||||||
char *raw_query = "";
|
char *raw_query = "";
|
||||||
|
int page = 1;
|
||||||
|
|
||||||
if (params) {
|
if (params) {
|
||||||
for (int i = 0; i < params->count; i++) {
|
for (int i = 0; i < params->count; i++) {
|
||||||
if (strcmp(params->params[i].key, "q") == 0) {
|
if (strcmp(params->params[i].key, "q") == 0) {
|
||||||
raw_query = params->params[i].value;
|
raw_query = params->params[i].value;
|
||||||
break;
|
} else if (strcmp(params->params[i].key, "p") == 0) {
|
||||||
|
int parsed = atoi(params->params[i].value);
|
||||||
|
if (parsed > 1) page = parsed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context_set(&ctx, "query", raw_query);
|
||||||
|
|
||||||
|
char page_str[16], prev_str[16], next_str[16];
|
||||||
|
snprintf(page_str, sizeof(page_str), "%d", page);
|
||||||
|
snprintf(prev_str, sizeof(prev_str), "%d", page > 1 ? page - 1 : 0);
|
||||||
|
snprintf(next_str, sizeof(next_str), "%d", page + 1);
|
||||||
|
context_set(&ctx, "page", page_str);
|
||||||
|
context_set(&ctx, "prev_page", prev_str);
|
||||||
|
context_set(&ctx, "next_page", next_str);
|
||||||
|
|
||||||
char *display_query = url_decode_query(raw_query);
|
char *display_query = url_decode_query(raw_query);
|
||||||
context_set(&ctx, "query", display_query);
|
context_set(&ctx, "query", display_query);
|
||||||
|
|
||||||
@@ -103,8 +116,9 @@ int images_handler(UrlParams *params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char url[1024];
|
char url[1024];
|
||||||
|
int first = (page - 1) * 32 + 1;
|
||||||
snprintf(url, sizeof(url),
|
snprintf(url, sizeof(url),
|
||||||
"https://www.bing.com/images/search?q=%s", encoded_query);
|
"https://www.bing.com/images/search?q=%s&first=%d", encoded_query, first);
|
||||||
fprintf(stderr, "[DEBUG] Fetching URL: %s\n", url);
|
fprintf(stderr, "[DEBUG] Fetching URL: %s\n", url);
|
||||||
|
|
||||||
char *html = fetch_images_html(url);
|
char *html = fetch_images_html(url);
|
||||||
|
|||||||
@@ -60,6 +60,15 @@
|
|||||||
</div>
|
</div>
|
||||||
{{endfor}}
|
{{endfor}}
|
||||||
</div>
|
</div>
|
||||||
|
<nav class="pagination">
|
||||||
|
<a class="pagination-btn prev" href="/images?q={{query}}&p={{prev_page}}">
|
||||||
|
← Page {{prev_page}}
|
||||||
|
</a>
|
||||||
|
<span class="pagination-current">Page {{page}}</span>
|
||||||
|
<a class="pagination-btn next" href="/images?q={{query}}&p={{next_page}}">
|
||||||
|
Page {{next_page}} →
|
||||||
|
</a>
|
||||||
|
</nav>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user