?
Solved

PHP, MySQL, display if in array like

Posted on 2014-04-19
4
Medium Priority
?
536 Views
Last Modified: 2014-05-02
Hello,

I have a website that has hundreds of cars that change every day.

I want to make it so that it displays the years found, although the years are in the title not a specific field, nothing I can do about that at this point.

I have the date range of a specific model and would like to echo only the years found in the db at any given time.

Currently I use this to output the years:
$date_range = '1980-1990';

$result = preg_replace_callback('/(\d+)-(\d+)/', function($m) {
		return implode(',', range($m[1], $m[2]));
	}, $date_range);
		$daterange = explode(',', $result);
// Then a foreach to echo

Open in new window


And here is the code I use when I go to a page with a year defined
$term = '1985';
$result = mysqli_query($con,"SELECT * FROM query_results WHERE active = '1' AND website_id = '$website_id' AND listing_title LIKE '%".$term."%' ORDER BY state ASC, city ASC, date_listed DESC");

Open in new window


I would like to combine this code to only display the years that are found in the db, not the complete range. I know we all hate going to a specific page and there are no results.

I would appreciate any help, and I do plan on running this script and adding the info to a db so it does not do this every pageload.

Thanks!
Matt
0
Comment
Question by:movieprodw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 1

Author Comment

by:movieprodw
ID: 40010502
Example of the page:
http://landroverclassicsearch.com/
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40010721
I think you would want to have the years in a separate column (probably indexed for good performance).  You can use ALTER TABLE to add a column for years, then the queries will be easier to write.  But even if you don't have that, what you can do is make two queries - one to extract the years from the title and a separate one to generate the appropriate results set containing only the years that appeared in the results set for the first query.  To do this you will create a dynamically generated WHERE clause with a lot of LIKE clauses in the query.  It will be slow to run, but given how many of these vehicles exists and are for sale at any one time, "slow" will be a relatively small problem.

Are all of the year-parts of the titles four-digit numbers?  Can you please post the CREATE TABLE statement for the relevant table and the output of a SELECT for the title column?  Once we see the actual data it should be easier to help refine the PHP code into the creation of a dynamic query.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 40038406
Hello,

Yes, they are all 4 digits.

I was thinking I should just run a cron query in the background.

I will fiddle with this and make another question if I can not figure it out.

Thanks for the help
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40038408
Thanks
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question