Solved

How would I do this as an array?

Posted on 2014-10-07
7
84 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 54

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 54

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 109

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 109

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 54

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 60
Insert won't work due to space in column name 5 21
send post values 7 26
how to use a switch statement with heredoc 11 23
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

770 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