Solved

PHP, MySQL, display if in array like

Posted on 2014-04-19
4
531 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 110

Accepted Solution

by:
Ray Paseur earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 96
Generate Unique ID in VB.NET 21 104
CURL sending XML without spaces PHP 5 35
A responsive image gallery using flexbox 6 22
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
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.

733 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