So for my website I'm trying to have a page where the user can type in literally anything and the page would output a list of stock symbols that are close to the input. Anything unrealistic (e.g. 3548 random letters) would obviously not work but if the user were to input something like "MS" then the page would output all stock symbols that start with the letters "MS" (i.e. "MSFT", Microsoft, would be one output).

I'm lost in how I can code this. Can anyone steer me in the right directions?

Thank you.

Dani AI

Generated

Since confirmed the symbols are in MySQL (as asked), here is a practical, low-risk plan that builds on 's UI point but focuses on correctness and performance.

For server-side lookups keep the symbol column indexed and normalized (store an uppercase copy if users may type mixed case). Avoid wrapping the column in functions in WHERE clauses because that prevents index use. A fast, index-friendly pattern is a lexicographic range search that returns a small page of results:

SELECT symbol, company_name
FROM stock_symbols
WHERE symbol >= 'MS' AND symbol < 'MT'
ORDER BY symbol
LIMIT 50;

Normalize the input before building the bounds on the server and use prepared statements to prevent injection. If you need to support instant typing in the UI, send debounced queries and always LIMIT results so the DB work stays cheap.

If you want "close" matches (typos or fuzzy results), do a two-stage approach: first use the indexed prefix/range to pull a modest candidate set, then score those candidates in PHP with levenshtein() or similar_text() and return the best matches. For larger scale or richer fuzzy options, consider a dedicated text indexer/search engine. See MySQL indexing guidance and PHP string functions levenshtein().

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

Are these stock symbols stored in a database?

Member Avatar for Member #120589

If terms are in mysql - you can search with the LIKE clause.
If terms are in an array, you could search with preg_match().
Either way, do you want this without a page refresh? If so, you can use ajax to populate a faux dropdown. There are loads of online scripts, e.g. autocomplete, autopopulate...

Yes, these stock symbols are stored in a database--a private school database. And yes, these are in terms of mySQL.

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.