Solved

How would I do this as an array?

Posted on 2014-10-07
7
81 Views
Last Modified: 2014-10-07
I've a pulldown that reveals the months of the year. I want only the months that are in my database so I wrote this:

					<option selected>Month</option>
					<?php 
					$cur_month="";
					$the_month="";
					$brice="select DISTINCT posted_day from verizon order by posted_day";
					$brice_query=mysqli_query($cxn, $brice)
					or die("Couldn't make Brice happen");
					while($brice_row=mysqli_fetch_assoc($brice_query))
					{
					extract($brice_row);
					$the_month = date("F", strtotime($brice_row['posted_day']));
						if($the_month=="January" AND $the_month<>$cur_month)
						{
						?>
						<option>January</option>
						<?php
						}
							elseif($the_month=="February" AND $the_month<>$cur_month)
							{
							?>
							<option>February</option>
							<?php
							}
							elseif($the_month=="March" AND $the_month<>$cur_month)
							{
							?>
							<option>March</option>
							<?php
							}
							elseif($the_month=="April" AND $the_month<>$cur_month)
							{
							?>
							<option>April</option>
							<?php
							}
							elseif($the_month=="May" AND $the_month<>$cur_month)
							{
							?>
							<option>May</option>
							<?php
							}
							elseif($the_month=="June" AND $the_month<>$cur_month)
							{
							?>
							<option>June</option>
							<?php
							}
							elseif($the_month=="July" AND $the_month<>$cur_month)
							{
							?>
							<option>July</option>
							<?php
							}
							elseif($the_month=="August" AND $the_month<>$cur_month)
							{
							?>
							<option>August</option>
							<?php
							}
							elseif($the_month=="September" AND $the_month<>$cur_month)
							{
							?>
							<option>September</option>
							<?php
							}
							elseif($the_month=="October" AND $the_month<>$cur_month)
							{
							?>
							<option>October</option>
							<?php
							}
							elseif($the_month=="November" AND $the_month<>$cur_month)
							{
							?>
							<option>November</option>
							<?php
							}
							elseif($the_month=="December" AND $the_month<>$cur_month)
							{
							?>
							<option>December</option>
							<?php
						}
						else
						{
						continue;
						}
					$cur_month=date("F", strtotime($brice_row['posted_day']));
					}
					?>
					<option>_________________________</option>
					</select>

Open in new window


It works fine, but I want to avoid having to loop through the table more than once. I've got a lot of records and I don't want to add an unnecessary burden to the user's experience by making them wait any longer than they would have to (I've got 200 million rows to consider).

I have the posted_day field indexed, so it doesn't take very long at all, but in the interest of keeping things as streamlined as possible, I want to store the results of the query that I've written above as an array and then, if I want to run the same conditional if statements for the day and the year, I can do that without having to do a select statement more than once.

Make sense?

In other words, can I store the months retrieved in the above query so if I had another pulldown that featured the months of the year, I could do all my ifs and elseifs against an array that has already been defined rather than having to loop through all my rows another time?

What do you think?
0
Comment
Question by:brucegust
  • 3
  • 2
  • 2
7 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40366364
If you only want the ones in your database can you not just do the following

SELECT DISTINCT MONTH(posted_day) FROM verizon ORDER BY MONTH(posted_day)

Open in new window

That will give you rows of all the months that exist in your data? You can now loop through them and output the list
0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
ID: 40366379
Or even better
SELECT DISTINCT MONTHNAME(posted_day) FROM verizon ORDER BY MONTH(posted_day)

Open in new window

0
 

Author Comment

by:brucegust
ID: 40366400
Julian, I can smell what you're cooking, but how do I store the output in an array?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 40366462
After the SELECT query, you will have a results set.  You iterate over the results set like this:

$sql = "SELECT monthname... whatever to get THE RIGHT ORDER AND DISTINCT month names";
$res = mysqli_query($cxn, $sql);
if (!$res) trigger_error("FAIL: $sql", E_USER_ERROR);
$months = array();
while($row = mysqli_fetch_object($res))
{
    $months[] = $row->monthname;
}
var_dump($months);

Open in new window

0
 

Author Comment

by:brucegust
ID: 40366476
That's what I was looking for! Thanks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40366503
Suggestion for going forward on this project... Create a test data set (obviously something less than 200MM rows) that is representative of your working data set.  Store it on your server in a .txt file or similar -- just so it is something small, but representative that we can download and use for testing.  Then when you have questions about how to construct the code you can show us the test data and we can give more complete (perhaps even tested) code examples.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40366521
  <option selected>Month</option>
<?php 
  // RETRIEV DISTINC MONTH NAMES IN MONTH ORDER
  $brice="SELECT DISTINCT MONTHNAME(posted_day) AS MonthName FROM verizon ORDER BY MONTH(posted_day)";
  $brice_query=mysqli_query($cxn, $brice) or die("Couldn't make Brice happen");
  // INITIALISE OUR ARRAY
  $months = array()
  // LOOP THROUGH RECORDS
  while($brice_row=mysqli_fetch_assoc($brice_query)) :
    // ADD MONTH NAME TO ARRAY
    $months[] = $brice_row['MonthName'];
    // OUTPUT THE OPTION
?>
  <option><?php echo $brice_row['MonthName'];?></option>
<?php
  endwhile;
?>
  <option>_________________________</option>
</select>

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now